///|
/// A platform that has a desktop app. Proton has an engine for these only, and MoonBit can
/// build the app for them.
#derive(Eq, Hash, Debug)
pub(all) enum AppPlatform {
  WindowsX64
  DarwinArm64
  LinuxX64
}

///|
/// Every platform with an app, in a fixed order.
pub let app_platforms : ReadOnlyArray[AppPlatform] = [
  WindowsX64,
  DarwinArm64,
  LinuxX64,
]

///|
/// The name of the platform in the files of a release, such as `windows-x64`.
pub fn AppPlatform::name(self : AppPlatform) -> String {
  match self {
    WindowsX64 => "windows-x64"
    DarwinArm64 => "darwin-arm64"
    LinuxX64 => "linux-x64"
  }
}

///|
/// The platform with this name.
pub fn AppPlatform::of_name(name : String) -> AppPlatform? {
  app_platforms.iter().find_first(platform => platform.name() == name)
}

///|
/// The operating system and the processor of a machine.
#derive(Eq, Debug)
struct Machine {
  name : String
  app : AppPlatform?
}

///|
/// The machine with this operating system. `processor` is the text that the system gives,
/// such as `AMD64` or `aarch64`. Windows on arm64 runs programs for x64.
pub fn Machine::Machine(system : @core.Platform, processor : String) -> Machine {
  let processor = match processor.trim().to_owned().to_lower() {
    "amd64" | "x86_64" | "x64" => "x64"
    "aarch64" | "arm64" => "arm64"
    // Free text for a message. No app has this processor.
    other => other
  }
  let app = match (system, processor) {
    (Windows, "x64" | "arm64") => Some(AppPlatform::WindowsX64)
    (MacOs, "arm64") => Some(DarwinArm64)
    (Linux, "x64") => Some(LinuxX64)
    _ => None
  }
  let system = match system {
    Windows => "windows"
    MacOs => "darwin"
    Linux => "linux"
  }
  { name: "\{system}-\{processor}", app, }
}

///|
/// The operating system and the processor for people, such as `linux-arm64`.
pub fn Machine::name(self : Machine) -> String {
  self.name
}

///|
/// The app that runs on this machine, if there is one.
pub fn Machine::app(self : Machine) -> AppPlatform? {
  self.app
}

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

///|
pub extend AppPlatform with Hash::{hash, hash_combine}

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

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

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