///|
/// The kind of a view of the UI.
#derive(Eq, Debug)
pub(all) enum Surface {
  /// A page in a browser.
  Browser
  /// The window of the desktop app.
  Desktop
}

///|
/// The command that opens a view of this kind. It names the instance, if that is not
/// `stable`.
pub fn Surface::open_command(self : Surface, instance : InstanceName) -> String {
  let command = match self {
    Browser => "mission-control web"
    Desktop => "mission-control desktop"
  }
  if instance == InstanceName::of_channel(Stable) {
    command
  } else {
    "\{command} --instance \{instance.to_string()}"
  }
}

///|
fn Surface::json_name(self : Surface) -> String {
  match self {
    Browser => "browser"
    Desktop => "desktop"
  }
}

///|
pub impl ToJson for Surface with fn to_json(self) {
  Json::string(self.json_name())
}

///|
pub impl @json.FromJson for Surface with fn from_json(json, path) {
  case_of_name(json, path, [Browser, Desktop], Surface::json_name)
}

///|
/// Reads the JSON name of one of the cases.
fn[T] case_of_name(
  json : Json,
  path : @json.DecodeContext,
  cases : ArrayView[T],
  json_name : (T) -> String,
) -> T raise @json.JsonDecodeError {
  let text : String = @json.from_json(json, path~)
  for value in cases {
    if json_name(value) == text {
      return value
    }
  }
  let names = cases.iter().map(json_name).collect().join(", ")
  raise path.error("\"\{text}\" is not one of: \{names}")
}

///|
pub extend Surface with Eq::{not_equal, equal}

///|
pub extend Surface with @debug.Debug::{to_repr}

///|
pub extend Surface with ToJson::{to_json}

///|
pub extend Surface with @json.FromJson::{from_json}