open Import
open Dune_rpc

module Status = struct
  module Menu = struct
    type t =
      | Uninitialized
      | Menu of (Method.Name.t * int) list

    let sexp =
      let open Conv in
      let menu = constr "menu" (list (pair Method.Name.sexp int)) (fun m -> Menu m) in
      let uninitialized = constr "stage1" unit (fun () -> Uninitialized) in
      let variants = [ econstr menu; econstr uninitialized ] in
      sum variants (function
        | Uninitialized -> case () uninitialized
        | Menu m -> case m menu)
    ;;
  end

  type t = { clients : (Id.t * Menu.t) list }

  let sexp =
    let open Conv in
    let to_ clients = { clients } in
    let from { clients } = clients in
    iso (list (pair Id.sexp Menu.sexp)) to_ from
  ;;

  let v1 = Decl.Request.make_current_gen ~req:Conv.unit ~resp:sexp ~version:1

  let decl =
    Decl.Request.make ~method_:(Method.Name.of_string "status") ~generations:[ v1 ]
  ;;
end

module Build = struct
  let v1 =
    Decl.Request.make_current_gen
      ~req:(Conv.list Conv.string)
      ~resp:Dune_rpc.Build_outcome_with_diagnostics.sexp_v1
      ~version:1
  ;;

  let v2 =
    Decl.Request.make_current_gen
      ~req:(Conv.list Conv.string)
      ~resp:Dune_rpc.Build_outcome_with_diagnostics.sexp_v2
      ~version:2
  ;;

  let decl =
    Decl.Request.make ~method_:(Method.Name.of_string "build") ~generations:[ v1; v2 ]
  ;;
end

let build = Build.decl
let status = Status.decl
