///|
fn load_entry_after_create(
  window : @native.Window,
  entry : @manifest.AppEntry,
  command_proxy_script : String?,
) -> Result[Unit, String] {
  match entry {
    @manifest.AppEntry::Html(html) =>
      native_unit_result(
        "load html",
        window.load_html(
          inject_command_proxy_script(html, command_proxy_script),
          "proton://app/",
        ),
      )
    @manifest.AppEntry::Url(url) =>
      native_unit_result("load url", window.load_url(url))
    @manifest.AppEntry::File(path) =>
      load_file_entry_after_create(
        window, "load file", path, command_proxy_script,
      )
    @manifest.AppEntry::Asset(path) =>
      load_file_entry_after_create(
        window, "load asset", path, command_proxy_script,
      )
  }
}

///|
fn inject_command_proxy_script(html : String, script : String?) -> String {
  match script {
    None => html
    Some(value) => inject_html_bootstrap(html, script_tag(value))
  }
}

///|
fn load_file_entry_after_create(
  window : @native.Window,
  action : String,
  path : String,
  command_proxy_script : String?,
) -> Result[Unit, String] {
  match command_proxy_script {
    None => native_unit_result(action, window.load_url(file_url(path)))
    Some(script) =>
      match read_file_entry_html(path, action) {
        Err(error) => Err(error)
        Ok(html) => {
          let bootstrap = base_tag(file_base_url(path)) + script_tag(script)
          native_unit_result(
            action,
            window.load_html(
              inject_html_bootstrap(html, bootstrap),
              "proton://app/",
            ),
          )
        }
      }
  }
}

///|
fn read_file_entry_html(
  path : String,
  action : String,
) -> Result[String, String] {
  try @mbfs.read_file_to_string(path) catch {
    error =>
      Err(
        action +
        " failed: failed to read HTML entry: " +
        @debug.render(@debug.to_repr(error)),
      )
  } noraise {
    text => Ok(text)
  }
}

///|
fn inject_html_bootstrap(html : String, bootstrap : String) -> String {
  if html.contains("") {
    html.replace(old="", new="\n" + bootstrap)
  } else if html.contains("") {
    html.replace(old="", new=bootstrap + "")
  } else {
    bootstrap + html
  }
}

///|
fn script_tag(script : String) -> String {
  "\n"
}

///|
fn base_tag(href : String) -> String {
  "\n"
}

///|
fn html_attribute_escape(value : String) -> String {
  value
  .replace_all(old="&", new="&")
  .replace_all(old="\"", new=""")
  .replace_all(old="<", new="<")
  .replace_all(old=">", new=">")
}

///|
fn native_unit_result(
  action : String,
  result : Result[Unit, @native.NativeError],
) -> Result[Unit, String] {
  match result {
    Ok(_) => Ok(())
    Err(error) => Err(native_error_message(action, error))
  }
}

///|
fn native_error_message(action : String, error : @native.NativeError) -> String {
  action +
  " failed: native error " +
  error.status.to_string() +
  ": " +
  error.message
}

///|
fn file_url(path : String) -> String {
  if path.has_prefix("file://") {
    path
  } else {
    let file_path : @mbpath.Path = path
    let normalized = file_path
      .resolve()
      .to_string()
      .replace_all(old="\\", new="/")
    if normalized.has_prefix("/") {
      "file://" + normalized
    } else {
      "file:///" + normalized
    }
  }
}

///|
fn file_base_url(path : String) -> String {
  if path.has_prefix("file://") {
    let index = path.rev_find("/")
    match index {
      Some(value) => path[:value + 1].to_owned()
      None => path
    }
  } else {
    let file_path : @mbpath.Path = path
    let dir = file_path.resolve().dirname().to_string()
    let url = file_url(dir)
    if url.has_suffix("/") {
      url
    } else {
      url + "/"
    }
  }
}

///|
fn debug_level_from_bool(enabled : Bool) -> Int {
  if enabled {
    1
  } else {
    0
  }
}

///|
fn debug_port(debug : Int) -> Int {
  if debug > 0 {
    match @env.get_env_var("PROTON_REMOTE_DEBUGGING_PORT") {
      Some(value) => {
        let port = @string.parse_int(value[:]) catch { _ => 9222 }
        if port > 0 {
          port
        } else {
          9222
        }
      }
      None => 9222
    }
  } else {
    0
  }
}

///|
fn size_hint_from_resizable(resizable : Bool) -> @manifest.WindowSizeHint {
  if resizable {
    @manifest.WindowSizeHint::None
  } else {
    @manifest.WindowSizeHint::Fixed
  }
}