///|
/// The place of an app that is installed for one instance. `mise run desktop:install`
/// installs an app there, and `mission-control desktop` finds it there.
#derive(Eq, Debug)
struct InstallPlace {
  instance : @app.InstanceName
  folder : @core.FolderPath
  program : @core.FolderPath
  binary : @core.FolderPath
  instance_file : @core.FolderPath
}

///|
/// The place of the app of an instance, below the base folder.
pub fn InstallPlace::InstallPlace(
  instance : @app.InstanceName,
  base : @core.BaseFolder,
  platform : @core.Platform,
) -> InstallPlace {
  let folder = base.desktop(instance)
  let program = @core.desktop_program(folder)
  // On Linux the program is a script, because Proton finds the engine with variables.
  let binary = if platform is Linux {
    from(from(folder, "bin"), program.name())
  } else {
    program
  }
  {
    instance,
    folder,
    program,
    binary,
    instance_file: installed_instance_path(binary, platform),
  }
}

///|
/// Reads the arguments of an installation: `[--instance NAME]`. An app from the
/// repository is for the instance `dev`, so it does not change the instance of a release.
pub fn InstallPlace::parse(
  args : ArrayView[String],
  base : @core.BaseFolder,
  platform : @core.Platform,
) -> InstallPlace raise ArgumentError {
  let { instance, .. } = read_options(args, [Instance], platform)
  InstallPlace(instance.unwrap_or(@app.InstanceName::dev()), base, platform)
}

///|
/// The instance that the app opens.
pub fn InstallPlace::instance(self : InstallPlace) -> @app.InstanceName {
  self.instance
}

///|
/// The folder that has the app, and nothing else.
pub fn InstallPlace::folder(self : InstallPlace) -> @core.FolderPath {
  self.folder
}

///|
/// The path of the program in the folder that `mission-control desktop` starts.
pub fn InstallPlace::program(self : InstallPlace) -> @core.FolderPath {
  self.program
}

///|
/// The path of the program that runs. On Linux `program` is a script that gives this
/// program its engine. On Windows and macOS it is the same file as `program`.
pub fn InstallPlace::binary(self : InstallPlace) -> @core.FolderPath {
  self.binary
}

///|
/// The path of the file with the name of the instance, in the folder.
pub fn InstallPlace::instance_file(self : InstallPlace) -> @core.FolderPath {
  self.instance_file
}

///|
/// The JSON form has the texts of the name and the paths. `scripts/desktop-install.mbtx`
/// reads it, because a script cannot import a module of the workspace.
pub impl ToJson for InstallPlace with fn to_json(self) {
  {
    "instance": self.instance.to_string(),
    "folder": self.folder.to_string(),
    "program": self.program.to_string(),
    "binary": self.binary.to_string(),
    "instance_file": self.instance_file.to_string(),
  }
}

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

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

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