///|
/// App-level IPC bridge shared by all app windows.
struct AppIpcBackend {
  bridge_script : String
  op_names : Array[String]
  extensions : Array[@ipc.IpcExtensionDescriptor]
}

///|
/// Installs a generic IPC bridge across every current and future app window.
///
/// The runtime does not own sockets, files, or child-process state here. It only
/// installs the bridge script generated by the selected IPC implementation.
pub fn App::install_ipc_bridge(
  self : App,
  bridge_script : String,
  op_names : Array[String],
  extensions? : Array[@ipc.IpcExtensionDescriptor] = [],
) -> Unit {
  guard self.installed_ipc_backends().length() == 0 else {
    abort("App::install_ipc_bridge: IPC bridge already installed")
  }
  let backend = AppIpcBackend::new(bridge_script, op_names, extensions)
  match validate_app_ipc_backend(self, backend) {
    Ok(_) => ()
    Err(error) => abort(error)
  }
  for window in self.windows() {
    install_app_ipc_backend_on_window(window, backend)
  }
  self.installed_ipc_backends().push(backend)
}

///|
fn AppIpcBackend::new(
  bridge_script : String,
  op_names : Array[String],
  extensions : Array[@ipc.IpcExtensionDescriptor],
) -> AppIpcBackend {
  AppIpcBackend::{
    bridge_script,
    op_names: op_names.map(fn(op_name) { op_name }),
    extensions: copy_ipc_extension_descriptors(extensions),
  }
}

///|
fn AppIpcBackend::bridge_script(self : AppIpcBackend) -> String {
  self.bridge_script
}

///|
fn AppIpcBackend::op_names(self : AppIpcBackend) -> Array[String] {
  self.op_names.map(fn(op_name) { op_name })
}

///|
fn AppIpcBackend::extensions(
  self : AppIpcBackend,
) -> Array[@ipc.IpcExtensionDescriptor] {
  copy_ipc_extension_descriptors(self.extensions)
}

///|
fn install_existing_ipc_backends(
  window : AppWindow,
  backends : Array[AppIpcBackend],
) -> Unit {
  for backend in backends {
    install_app_ipc_backend_on_window(window, backend)
  }
}

///|
fn install_app_ipc_backend_on_window(
  window : AppWindow,
  backend : AppIpcBackend,
) -> Unit {
  @core.install_ipc_bridge_runtime(
    window.webview,
    backend.bridge_script(),
    backend.op_names(),
    extensions=backend.extensions(),
  )
}

///|
fn App::installed_ipc_backends(self : App) -> Array[AppIpcBackend] {
  self.ipc_backends
}

///|
fn App::close_ipc_backends(self : App) -> Unit {
  self.installed_ipc_backends().clear()
}

///|
fn validate_app_ipc_backend(
  app : App,
  backend : AppIpcBackend,
) -> Result[Unit, String] {
  validate_app_ipc_backend_config(app.installed_extension_namespaces(), backend)
}

///|
fn validate_app_ipc_backend_config(
  local_namespaces : Map[String, Bool],
  backend : AppIpcBackend,
) -> Result[Unit, String] {
  guard backend.bridge_script().trim().to_owned() != "" else {
    return Err("App::install_ipc_bridge: bridge script must not be empty")
  }
  for descriptor in backend.extensions() {
    guard local_namespaces.get(descriptor.js_namespace) is None else {
      return Err(
        "App::install_ipc_bridge: extension namespace already installed: " +
        descriptor.js_namespace,
      )
    }
  }
  Ok(())
}

///|
fn ipc_extension_namespace_is_installed(
  backends : Array[AppIpcBackend],
  js_namespace : String,
) -> Bool {
  for backend in backends {
    for descriptor in backend.extensions() {
      if descriptor.js_namespace == js_namespace {
        return true
      }
    }
  }
  false
}

///|
fn copy_ipc_extension_descriptors(
  values : Array[@ipc.IpcExtensionDescriptor],
) -> Array[@ipc.IpcExtensionDescriptor] {
  values.map(fn(value) {
    @ipc.IpcExtensionDescriptor::new(
      value.id,
      value.js_namespace,
      value.apis,
      scripts=value.scripts,
    )
  })
}