///|
pub struct AppRuntime {
  priv state : RuntimeState
  priv program_runtime_snapshot_fn : () -> ProgramRuntimeSnapshot
  priv destroy_program_runtime_fn : Ref[() -> Unit]
}

///|
fn program_message_drain_limit() -> Int32 {
  1024
}

///|
fn[Msg] compact_pending_program_messages(
  pending_messages : Array[Msg],
  start : Int32,
) -> Unit {
  let remaining : Array[Msg] = []
  for index in start.. Unit)? = None,
) -> AppRuntime {
  {
    state: RuntimeState::new_view(root~, size~, environment~, on_message~),
    program_runtime_snapshot_fn: () => program_runtime_snapshot_empty(),
    destroy_program_runtime_fn: Ref(() => ()),
  }
}

///|
pub fn[Model, Msg] AppRuntime::new_program(
  program~ : @core.Program[Model, Msg],
  size~ : @core.Size,
  environment~ : @core.Environment = @core.Environment::neutral(),
  state_transport~ : @core.ProgramStateTransport? = None,
  transient_transport~ : @core.ProgramStateTransport? = None,
) -> AppRuntime {
  let (initial_model, initial_effect) = program.runtime_init()
  let model = Ref(
    program_restore_model(program, state_transport, initial_model),
  )
  let program_runtime = ProgramRuntimeDiagnostics::new()
  let pending_messages : Array[Msg] = []
  let pending_index = Ref(0)
  let dispatching = Ref(false)
  let syncing_subscriptions = Ref(false)
  let message_batch_depth = Ref(0)
  let program_active = Ref(true)
  let runtime_ref : Ref[AppRuntime?] = Ref(None)
  let process_ref : Ref[(() -> Unit)?] = Ref(None)
  let pending_count = () => pending_messages.length() - pending_index.val
  let dispatch = message => {
    if program_active.val {
      pending_messages.push(message)
      program_runtime.record_message_enqueued(pending_count())
      if !syncing_subscriptions.val && message_batch_depth.val == 0 {
        match process_ref.val {
          Some(process) => process()
          None => ()
        }
      }
    } else {
      program_runtime.record_ignored_program_dispatch()
    }
  }
  let sync_subscriptions = () => {
    syncing_subscriptions.val = true
    program_runtime.sync_subscriptions(
      program.runtime_subscriptions(model.val),
      dispatch,
    )
    syncing_subscriptions.val = false
  }
  let command_sync = Ref(ProgramCommandSync::new())
  let sync_commands = () => {
    match runtime_ref.val {
      Some(runtime) =>
        match
          command_sync.val.sync(program.runtime_commands(model.val), dispatch) {
          Some(map) => runtime.state.set_action_commands(map)
          None => ()
        }
      None => ()
    }
  }
  let begin_message_batch = () => {
    message_batch_depth.val = message_batch_depth.val + 1
  }
  let end_message_batch = () => {
    message_batch_depth.val = message_batch_depth.val - 1
    if message_batch_depth.val == 0 && !syncing_subscriptions.val {
      match process_ref.val {
        Some(process) => process()
        None => ()
      }
    }
  }
  let process_messages = () => {
    if !dispatching.val && !syncing_subscriptions.val {
      program_runtime.record_message_drain_started(pending_count())
      dispatching.val = true
      let mut processed_this_drain = 0
      while pending_index.val < pending_messages.length() &&
            processed_this_drain < program_message_drain_limit() {
        let message = pending_messages[pending_index.val]
        pending_index.val = pending_index.val + 1
        processed_this_drain = processed_this_drain + 1
        program_runtime.record_dispatch()
        let (next_model, effect) = program.runtime_update(model.val, message)
        program_runtime.record_update()
        model.val = next_model
        program_save_model(program, state_transport, next_model)
        match runtime_ref.val {
          Some(runtime) => runtime.state.request_root_rebuild()
          None => ()
        }
        sync_commands()
        // Single traversal for execution + diagnostics summary (D3): the
        // separate per-message `effect_plan_summary` walk is folded into the
        // run, so the hot path pays no extra diagnostic traversal.
        let effect_run = EffectPlanRunSummary::new()
        effect_run.run(effect, 1, dispatch, (descriptor, start) => {
          program_runtime.start_effect_task(descriptor, start, dispatch)
        })
        program_runtime.record_effects(
          effect_run.finish(),
          effect_run.executed_count(),
        )
        sync_subscriptions()
        program_runtime.record_message_processed(pending_count())
      }
      if pending_index.val < pending_messages.length() {
        compact_pending_program_messages(pending_messages, pending_index.val)
        pending_index.val = 0
        program_runtime.record_message_processed(pending_count())
      } else {
        pending_messages.clear()
        pending_index.val = 0
        program_runtime.record_message_queue_cleared()
      }
      dispatching.val = false
    }
  }
  process_ref.val = Some(process_messages)
  let initial_effect_run = EffectPlanRunSummary::new()
  initial_effect_run.run(initial_effect, 1, dispatch, (descriptor, start) => {
    program_runtime.start_effect_task(descriptor, start, dispatch)
  })
  program_runtime.record_effects(
    initial_effect_run.finish(),
    initial_effect_run.executed_count(),
  )
  sync_subscriptions()
  match process_ref.val {
    Some(process) => process()
    None => ()
  }
  let runtime = AppRuntime::{
    state: RuntimeState::new_view(
      root=program.runtime_view(
        model.val,
        @core.ViewEnvironment::new(viewport_size=size, environment~),
      ),
      size~,
      environment~,
      on_message=Some(dispatch),
      begin_message_batch~,
      end_message_batch~,
    ),
    program_runtime_snapshot_fn: () => program_runtime.snapshot(),
    destroy_program_runtime_fn: Ref(() => ()),
  }
  runtime_ref.val = Some(runtime)
  match transient_transport {
    Some(transport) => {
      runtime.state.dev_transient_transport = Some(transport)
      match transport.read() {
        Some(payload) => runtime.restore_dev_transient(payload)
        None => ()
      }
    }
    None => ()
  }
  runtime.state.set_root_builder(() => {
    runtime_view_node_with_overlays(
      program.runtime_view(
        model.val,
        @core.ViewEnvironment::new(
          viewport_size=runtime.state.size,
          environment=runtime.state.environment,
        ),
      ),
      dispatch,
      overlays=Some(runtime.state.host_overlay_slot),
    )
  })
  sync_commands()
  runtime.destroy_program_runtime_fn.val = () => {
    if program_active.val {
      program_active.val = false
      program_runtime.cancel_all_effect_tasks(EffectTaskRuntimeDestroyed)
      program_runtime.cancel_all_subscriptions(SubscriptionRuntimeDestroyed)
    }
  }
  runtime
}

///|
pub fn[Msg] new_view(
  root~ : @core.View[Msg],
  size~ : @core.Size,
  environment~ : @core.Environment = @core.Environment::neutral(),
  on_message~ : ((Msg) -> Unit)? = None,
) -> AppRuntime {
  AppRuntime::new_view(root~, size~, environment~, on_message~)
}

///|
pub fn[Model, Msg] new_program(
  program~ : @core.Program[Model, Msg],
  size~ : @core.Size,
  environment~ : @core.Environment = @core.Environment::neutral(),
  state_transport~ : @core.ProgramStateTransport? = None,
  transient_transport~ : @core.ProgramStateTransport? = None,
) -> AppRuntime {
  AppRuntime::new_program(
    program~,
    size~,
    environment~,
    state_transport~,
    transient_transport~,
  )
}

///|
pub fn[Model, Msg] new_program_with_dimensions(
  program~ : @core.Program[Model, Msg],
  width~ : Double,
  height~ : Double,
  environment~ : @core.Environment = @core.Environment::neutral(),
  state_transport~ : @core.ProgramStateTransport? = None,
  transient_transport~ : @core.ProgramStateTransport? = None,
) -> AppRuntime {
  AppRuntime::new_program(
    program~,
    size=@core.Size::new(width~, height~),
    environment~,
    state_transport~,
    transient_transport~,
  )
}

///|
pub fn AppRuntime::environment(self : AppRuntime) -> @core.Environment {
  self.state.environment()
}

///|
pub fn AppRuntime::text_system(self : AppRuntime) -> @core.TextSystem {
  self.state.text_system()
}

///|
pub fn AppRuntime::set_text_system(
  self : AppRuntime,
  text_system : @core.TextSystem,
) -> Unit {
  self.state.set_text_system(text_system)
}

///|
pub fn AppRuntime::register_font_data(
  self : AppRuntime,
  family : @core.FontFamily,
  data : Bytes,
) -> Unit {
  self.state.register_font_data(family, data)
}

///|
pub fn AppRuntime::set_environment(
  self : AppRuntime,
  environment : @core.Environment,
) -> Unit {
  self.state.set_environment(environment)
}

///|
pub fn AppRuntime::advance_time(self : AppRuntime, time_ms : Double) -> Unit {
  self.state.advance_time(time_ms)
  self.state.maybe_save_dev_transient()
  self.state.maybe_sample_devtools()
}

///|
pub fn AppRuntime::destroy(self : AppRuntime) -> Unit {
  self.state.request_destroy(self.destroy_program_runtime_fn.val)
}

///|
pub fn AppRuntime::layout(self : AppRuntime) -> @core.LayoutResult {
  self.state.layout()
}

///|
pub fn AppRuntime::read_semantics(
  self : AppRuntime,
  since? : @core.SemanticsGeneration,
) -> Result[SemanticsRead, SemanticsReadError] {
  self.state.read_semantics(since)
}

///|
pub fn AppRuntime::perform_semantics_action(
  self : AppRuntime,
  request : PerformSemanticsActionRequest,
) -> Result[SemanticsActionReceipt, SemanticsActionDispatchError] {
  match self.state.perform_semantics_action(request) {
    Ok(receipt) => {
      let program = self.program_runtime_snapshot()
      Ok({
        ..receipt,
        pending_work: receipt.pending_work ||
        program.pending_message_count > 0 ||
        program.active_effect_task_count > 0,
      })
    }
    Err(error) => Err(error)
  }
}

///|
pub fn AppRuntime::focused_text_input(
  self : AppRuntime,
) -> @core.TextInputState {
  self.state.focused_text_input()
}

///|
pub fn AppRuntime::focus_next(self : AppRuntime) -> Bool {
  self.state.move_focus()
}

///|
pub fn AppRuntime::focus_previous(self : AppRuntime) -> Bool {
  self.state.move_focus(reverse=true)
}

///|
pub fn AppRuntime::focus_key(self : AppRuntime, key : String) -> Bool {
  self.state.focus_key(key)
}

///|
/// Legacy command-only stream with layout placements folded into screen-space
/// geometry. Prefer `draw_frame()` for renderers: its commands keep the
/// transform scopes needed to replay content without rebasing every node.
pub fn AppRuntime::draw_commands(self : AppRuntime) -> Array[@core.DrawCommand] {
  self.state.build_draw_commands()
}

///|
pub fn AppRuntime::draw_frame(self : AppRuntime) -> @core.DrawFrame {
  self.state.build_draw_frame()
}

///|
/// Opt this host into semantics-carrying draw frames.
///
/// A non-pixel renderer (terminal output) projects the committed tree from the
/// frame payload rather than reading the runtime mid-paint. Off by default, so
/// pixel hosts build no payload and their frames are unchanged.
pub fn AppRuntime::set_semantics_frames_enabled(
  self : AppRuntime,
  enabled : Bool,
) -> Unit {
  self.state.semantics.frames_carry_semantics = enabled
}

///|
pub fn AppRuntime::size(self : AppRuntime) -> @core.Size {
  self.state.size
}

///|
pub fn AppRuntime::needs_redraw(self : AppRuntime) -> Bool {
  self.state.dirty.needs_redraw
}

///|
pub fn AppRuntime::needs_rebuild(self : AppRuntime) -> Bool {
  self.state.dirty.needs_rebuild
}

///|
pub fn AppRuntime::needs_layout(self : AppRuntime) -> Bool {
  self.state.dirty.needs_layout
}

///|
pub fn AppRuntime::needs_paint(self : AppRuntime) -> Bool {
  self.state.dirty.needs_paint
}

///|
pub fn AppRuntime::dirty_count(self : AppRuntime) -> Int32 {
  self.state.dirty_count()
}

///|
pub fn AppRuntime::dirty_element_ids(self : AppRuntime) -> Array[Int32] {
  self.state.dirty_element_ids().map(id => id.value())
}

///|
pub fn AppRuntime::inspector_snapshot(self : AppRuntime) -> InspectorSnapshot {
  self.inspector_snapshot_from_state()
}

///|
pub fn AppRuntime::program_runtime_snapshot(
  self : AppRuntime,
) -> ProgramRuntimeSnapshot {
  (self.program_runtime_snapshot_fn)()
}

///|
pub fn AppRuntime::set_redraw_callback(
  self : AppRuntime,
  callback : () -> Unit,
) -> Unit {
  self.state.set_redraw_callback(callback)
}

///|
/// Installs an observer invoked with every `AppEvent` before it is processed.
/// Replaces any previously installed observer. Passing a no-op effectively
/// disables observation. This is the additive hook the agent layer consumes;
/// it is inert unless explicitly called.
pub fn AppRuntime::set_event_observer(
  self : AppRuntime,
  observer : (@core.AppEvent) -> Unit,
) -> Unit {
  self.state.event_observer = observer
}

///|
pub fn AppRuntime::action_commands(
  self : AppRuntime,
) -> Array[@core.ActionCommand] {
  self.state.action_commands()
}

///|
pub fn AppRuntime::dispatch_command(
  self : AppRuntime,
  intent : @core.CommandIntent,
) -> Bool {
  self.state.dispatch_command(intent)
}

///|
pub fn AppRuntime::dispatch_action_command(
  self : AppRuntime,
  command : @core.ActionCommand,
) -> Bool {
  self.state.dispatch_action_command(command)
}

///|
pub fn AppRuntime::dispatch_focused_text_command(
  self : AppRuntime,
  intent : @core.CommandIntent,
  paste_text~ : String? = None,
) -> @core.TextCommandResult {
  self.state.dispatch_focused_text_command(intent, paste_text~)
}

///|
/// Select the whole text of the focused text control, the same edit a host
/// performs for Ctrl/Cmd+A. Returns whether the control handled it.
///
/// The pre-fill case this exists for: a rename prompt opens with the current
/// name already in the field, and the first keystroke must replace it instead
/// of prefixing it. `select_focused_text_all()` before typing does that
/// without the host simulating a key chord.
pub fn AppRuntime::select_focused_text_all(self : AppRuntime) -> Bool {
  self.state.dispatch_focused_text_command(@core.CommandIntent::SelectAll).changed
}

///|
/// Move the caret of the focused text control to `offset` (byte offset in the
/// control's text; out-of-range offsets clamp, and an offset inside a
/// multi-code-point grapheme snaps to the nearest boundary). Returns whether
/// the control changed.
pub fn AppRuntime::place_focused_text_caret(
  self : AppRuntime,
  offset : Int32,
) -> Bool {
  self.state.dispatch_focused_text_input(
    @core.TextInputEvent::SetSelection(
      @core.TextRange::new(start=offset, end=offset),
    ),
  )
}

///|
/// Select `[start, end)` in the focused text control and put the caret at
/// `end`; a collapsed range moves the caret instead. Offsets normalize to
/// grapheme boundaries the same way pointer selection does, and the control
/// keeps owning the text (no `on_input` fires for a pure selection change).
pub fn AppRuntime::set_focused_text_selection(
  self : AppRuntime,
  start : Int32,
  end : Int32,
) -> Bool {
  self.state.dispatch_focused_text_input(
    @core.TextInputEvent::SetSelection(@core.TextRange::new(start~, end~)),
  )
}

///|
/// Last pointer position the runtime observed, in viewport coordinates.
/// `None` before the pointer has entered the window. The Linux context-menu
/// fallback anchors the view-level menu at this point.
pub fn AppRuntime::pointer_position(self : AppRuntime) -> @core.Point? {
  self.state.last_pointer_position
}

///|
/// Mount (`Some`) or clear (`None`) a host-owned overlay layer above the app
/// root. See `HostOverlay`: the layer survives program rebuilds, gets the
/// surface rect, paints last and hit-tests first. A layer built with
/// `keeps_focus=true` also leaves the app's focus owner untouched on press.
pub fn AppRuntime::set_host_overlay(
  self : AppRuntime,
  overlay : HostOverlay?,
) -> Unit {
  match overlay {
    Some(layer) =>
      self.state.set_host_overlay_nodes(
        [layer.node],
        keeps_focus=layer.keeps_focus,
      )
    None => self.state.set_host_overlay_nodes([])
  }
}