///|
/// The entry of an installed app in the application menu of Linux, and the place of its
/// icon.
#derive(Eq, Debug)
struct MenuEntry {
  file : @core.FolderPath
  text : String
  icon_theme : @core.FolderPath
  icon_file : String
}

///|
/// The entry of an app in the data folder of the user, which these variables give.
///
/// The name of the file and of the icon is the identifier of the app. Its windows have the
/// same class, so the desktop knows which entry a window belongs to.
fn MenuEntry::of_env(
  place : InstallPlace,
  env : Map[String, String],
) -> MenuEntry raise @core.BaseFolderError {
  let data = @core.linux_data_folder(env)
  let id = app_identifier(place.instance())
  let program = place.program().to_string()
  let text =
    $|[Desktop Entry]
    $|Type=Application
    $|Name=\{window_title(place.instance())}
    $|Comment=List and open the projects on this machine
    $|TryExec=\{escaped(program)}
    $|Exec=\{exec_argument(program)}
    $|Icon=\{id}
    $|Terminal=false
    $|Categories=Development;
    $|StartupWMClass=\{id}
    $|StartupNotify=false
    $|
  {
    file: from(data, "applications/\{id}.desktop"),
    text,
    icon_theme: from(data, "icons/hicolor"),
    icon_file: "\{id}.png",
  }
}

///|
/// The text in the form of a string value of a desktop entry.
fn escaped(text : String) -> String {
  text
  .replace_all(old="\\", new="\\\\")
  .replace_all(old="\n", new="\\n")
  .replace_all(old="\r", new="\\r")
  .replace_all(old="\t", new="\\t")
}

///|
/// The text as one argument of the key `Exec`. The menu first reads the string value, then
/// the quotes, and then the codes with a percent sign.
fn exec_argument(text : String) -> String {
  let quoted = text
    .replace_all(old="\\", new="\\\\")
    .replace_all(old="\"", new="\\\"")
    .replace_all(old="`", new="\\`")
    .replace_all(old="$", new="\\$")
    .replace_all(old="%", new="%%")
  escaped("\"\{quoted}\"")
}

///|
/// The path of the file of the entry.
pub fn MenuEntry::file(self : MenuEntry) -> @core.FolderPath {
  self.file
}

///|
/// The text of the file of the entry.
pub fn MenuEntry::text(self : MenuEntry) -> String {
  self.text
}

///|
/// The folder of the icon theme. An icon of 48 pixels is at `48x48/apps/` in it.
pub fn MenuEntry::icon_theme(self : MenuEntry) -> @core.FolderPath {
  self.icon_theme
}

///|
/// The name of the file of each icon.
pub fn MenuEntry::icon_file(self : MenuEntry) -> String {
  self.icon_file
}

///|
/// How a user opens an installed app without a terminal.
#derive(Eq, Debug)
pub(all) enum AppEntry {
  /// The base folder is the one of `MISSION_CONTROL_HOME`. A menu does not set this
  /// variable, so the app would use the default folder.
  NoEntry
  /// A shortcut with this name in the Start menu of the user.
  StartMenu(name~ : String)
  /// The app is this bundle in the folder `Applications` of the user, and the folder of
  /// the instance has a link to it. Spotlight does not show a link.
  Applications(bundle~ : @core.FolderPath)
  ApplicationMenu(MenuEntry)
}

///|
/// The entry of an app that is installed with these variables.
pub fn AppEntry::of_env(
  place : InstallPlace,
  platform : @core.Platform,
  env : Map[String, String],
) -> AppEntry raise @core.BaseFolderError {
  guard env.get("MISSION_CONTROL_HOME") is (None | Some("")) else { NoEntry }
  let name = window_title(place.instance())
  match platform {
    Windows => StartMenu(name~)
    MacOs => {
      guard env.get("HOME") is Some(home) && home != "" else {
        raise MissingVariable("HOME")
      }
      let home = @core.FolderPath(home, platform) catch {
        _ => raise NotAbsolute(variable="HOME", value=home)
      }
      Applications(bundle=from(home, "Applications/\{name}.app"))
    }
    Linux => ApplicationMenu(MenuEntry::of_env(place, env))
  }
}

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

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

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

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