"use strict";
import { paint } from "./scene.js";
import { applyPatch } from "./diff.js";
import { intentFromEmit } from "./intent.js";
import { postIntent, openSession } from "./session.js";
const SESSION_RESOURCE = "demo";
const SESSION_PANE = "pane-main";
const BOOTSTRAP_SCENE = {
kind: "scene/stack",
dir: "column",
children: [
{
kind: "scene/box",
role: "summary",
children: [
{ kind: "scene/text", text: "SIM Web-UI shell" },
{ kind: "scene/badge", status: "ok", label: "interpreter loaded" },
],
},
],
};
function applyReducedMotion() {
const prefersReduced =
typeof window.matchMedia === "function" &&
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (prefersReduced || window.__SIM_REDUCED_MOTION__) {
document.body.dataset.reducedMotion = "true";
}
}
function installKeyboardSpine(mount) {
document.addEventListener("keydown", (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === "k") {
e.preventDefault();
const focusable = mount.querySelector(
'input, button, [tabindex="0"]',
);
if (focusable && typeof focusable.focus === "function") {
focusable.focus();
}
}
});
}
function boot() {
const mount = document.getElementById("shell");
if (!mount) return;
applyReducedMotion();
installKeyboardSpine(mount);
let scene = window.__SIM_SCENE__ || BOOTSTRAP_SCENE;
let tick = 0;
const emit = (event) => {
tick += 1;
const intent = intentFromEmit(event, "pane-main", "human", tick);
if (intent) {
document.dispatchEvent(new CustomEvent("sim-intent", { detail: intent }));
}
};
const repaint = () => paint(document, mount, scene, emit);
repaint();
document.addEventListener("sim-scene-patch", (e) => {
scene = applyPatch(scene, e.detail);
repaint();
});
document.addEventListener("sim-intent", async (e) => {
const patches = await postIntent(e.detail);
for (const patch of patches) {
document.dispatchEvent(new CustomEvent("sim-scene-patch", { detail: patch }));
}
});
openSession(SESSION_RESOURCE, SESSION_PANE).then((opened) => {
if (opened) {
scene = opened;
repaint();
}
});
console.log("sim-web-shell: scene painter booted");
}
if (typeof document !== "undefined") {
boot();
}
export { BOOTSTRAP_SCENE, boot };