///|
type BackendTerminal

///|
#borrow(path)
extern "C" fn ghostty_mbt_set_library_path(path : Bytes) -> Unit = "ghostty_mbt_set_library_path"

///|
extern "C" fn ghostty_mbt_load() -> Int32 = "ghostty_mbt_load"

///|
extern "C" fn ghostty_mbt_loaded() -> Int32 = "ghostty_mbt_loaded"

///|
extern "C" fn ghostty_mbt_last_error() -> Bytes = "ghostty_mbt_last_error"

///|
extern "C" fn ghostty_mbt_terminal_new(
  cols : Int32,
  rows : Int32,
  max_scrollback : Int64,
) -> BackendTerminal = "ghostty_mbt_terminal_new"

///|
#borrow(terminal)
extern "C" fn ghostty_mbt_terminal_is_null(terminal : BackendTerminal) -> Int32 = "ghostty_mbt_terminal_is_null"

///|
#borrow(terminal)
extern "C" fn ghostty_mbt_terminal_free(terminal : BackendTerminal) -> Unit = "ghostty_mbt_terminal_free"

///|
#borrow(terminal, value)
extern "C" fn ghostty_mbt_terminal_write(
  terminal : BackendTerminal,
  value : Bytes,
) -> Unit = "ghostty_mbt_terminal_write"

///|
#borrow(terminal)
extern "C" fn ghostty_mbt_terminal_reset(terminal : BackendTerminal) -> Unit = "ghostty_mbt_terminal_reset"

///|
#borrow(terminal)
extern "C" fn ghostty_mbt_terminal_resize(
  terminal : BackendTerminal,
  cols : Int32,
  rows : Int32,
  cell_width_px : Int32,
  cell_height_px : Int32,
) -> Int32 = "ghostty_mbt_terminal_resize"

///|
#borrow(terminal)
extern "C" fn ghostty_mbt_terminal_cols(terminal : BackendTerminal) -> Int32 = "ghostty_mbt_terminal_cols"

///|
#borrow(terminal)
extern "C" fn ghostty_mbt_terminal_rows(terminal : BackendTerminal) -> Int32 = "ghostty_mbt_terminal_rows"

///|
#borrow(terminal)
extern "C" fn ghostty_mbt_terminal_title(terminal : BackendTerminal) -> Bytes = "ghostty_mbt_terminal_title"

///|
#borrow(terminal)
extern "C" fn ghostty_mbt_terminal_pwd(terminal : BackendTerminal) -> Bytes = "ghostty_mbt_terminal_pwd"

///|
#borrow(terminal)
extern "C" fn ghostty_mbt_terminal_format(
  terminal : BackendTerminal,
  format : Int32,
  trim : Int32,
  unwrap : Int32,
) -> Bytes = "ghostty_mbt_terminal_format"

///|
extern "C" fn ghostty_mbt_build_info_bool(field : Int32) -> Int32 = "ghostty_mbt_build_info_bool"

///|
extern "C" fn ghostty_mbt_build_info_string(field : Int32) -> Bytes = "ghostty_mbt_build_info_string"

///|
async fn backend_load(config : RuntimeConfig) -> Bool {
  match config.native_library_path {
    Some(path) =>
      if path != "" {
        ghostty_mbt_set_library_path(string_to_utf8_bytes(path))
      }
    None => ()
  }
  ghostty_mbt_load() == 1
}

///|
fn backend_loaded() -> Bool {
  ghostty_mbt_loaded() == 1
}

///|
fn backend_name() -> String {
  "native"
}

///|
fn backend_last_error() -> String {
  utf8_bytes_to_string(ghostty_mbt_last_error())
}

///|
fn backend_build_info() -> BuildInfo? {
  if !backend_loaded() {
    return None
  }
  Some({
    simd: ghostty_mbt_build_info_bool(1) == 1,
    kitty_graphics: ghostty_mbt_build_info_bool(2) == 1,
    tmux_control_mode: ghostty_mbt_build_info_bool(3) == 1,
    version: utf8_bytes_to_string(ghostty_mbt_build_info_string(5)),
  })
}

///|
fn backend_terminal_new(options : TerminalOptions) -> BackendTerminal? {
  if !backend_loaded() {
    return None
  }
  let terminal = ghostty_mbt_terminal_new(
    options.cols,
    options.rows,
    options.max_scrollback.to_int64(),
  )
  if ghostty_mbt_terminal_is_null(terminal) == 1 {
    None
  } else {
    Some(terminal)
  }
}

///|
fn backend_terminal_close(backend : BackendTerminal) -> Unit {
  ghostty_mbt_terminal_free(backend)
}

///|
fn backend_terminal_write(backend : BackendTerminal, value : Bytes) -> Unit {
  ghostty_mbt_terminal_write(backend, value)
}

///|
fn backend_terminal_reset(backend : BackendTerminal) -> Unit {
  ghostty_mbt_terminal_reset(backend)
}

///|
fn backend_terminal_resize(
  backend : BackendTerminal,
  cols : Int32,
  rows : Int32,
  cell_width_px : Int32,
  cell_height_px : Int32,
) -> Bool {
  ghostty_mbt_terminal_resize(
    backend, cols, rows, cell_width_px, cell_height_px,
  ) ==
  1
}

///|
fn backend_terminal_cols(backend : BackendTerminal) -> Int32 {
  ghostty_mbt_terminal_cols(backend)
}

///|
fn backend_terminal_rows(backend : BackendTerminal) -> Int32 {
  ghostty_mbt_terminal_rows(backend)
}

///|
fn backend_terminal_title(backend : BackendTerminal) -> String {
  utf8_bytes_to_string(ghostty_mbt_terminal_title(backend))
}

///|
fn backend_terminal_pwd(backend : BackendTerminal) -> String {
  utf8_bytes_to_string(ghostty_mbt_terminal_pwd(backend))
}

///|
fn backend_terminal_format(
  backend : BackendTerminal,
  options : FormatOptions,
) -> String {
  let format = match options.format {
    Plain => 0
    Vt => 1
    Html => 2
  }
  utf8_bytes_to_string(
    ghostty_mbt_terminal_format(
      backend,
      format,
      if options.trim {
        1
      } else {
        0
      },
      if options.unwrap {
        1
      } else {
        0
      },
    ),
  )
}