{"version":3,"file":"menuStore.mjs","names":[],"sources":["../../../src/web/stores/menuStore.ts"],"sourcesContent":["import { Store } from '@tanstack/store';\n\nimport { traceStore } from '../lib/uiTrace';\n\n/** A selection-bar axis name: the built-in 'preset'/'domains'/'mode', or a plugin-declared axis such as 'profile'. */\nexport type MenuKind = string;\n\nexport interface MenuClaim {\n  /** The agent dialog rendered as the bar's popover instead of the modal. */\n  dialogId: string;\n  menu: MenuKind;\n}\n\nexport interface MenuState {\n  /** Selection-bar button whose command is in flight, if any. */\n  pending: MenuKind | null;\n  /** When it was clicked; a stale anchor must not restyle an unrelated dialog. */\n  at: number;\n  /** The dialog the last click claimed, so the bar and the overlay agree who renders it. */\n  claimed: MenuClaim | null;\n}\n\nconst MENU_FRESH_MS = 10_000;\n\nexport const menuStore = traceStore('menu', new Store<MenuState>({ pending: null, at: 0, claimed: null }));\n\nlet expiry: ReturnType<typeof setTimeout> | undefined;\n\n/**\n * Remembers which selection-bar button asked the agent a question.\n *\n * The agent answers with an ordinary select dialog; this is the only signal\n * that lets the bar render it as its popover menu rather than a centered\n * modal. The button shows a pending state until the dialog arrives, the run\n * settles, or the anchor goes stale.\n */\nexport function setPendingMenu(kind: MenuKind): void {\n  menuStore.setState((state) => ({ ...state, pending: kind, at: Date.now() }));\n  if (expiry !== undefined) clearTimeout(expiry);\n  expiry = setTimeout(clearPendingMenu, MENU_FRESH_MS);\n}\n\nexport function clearPendingMenu(): void {\n  if (expiry !== undefined) clearTimeout(expiry);\n  expiry = undefined;\n  menuStore.setState((state) => (state.pending === null ? state : { ...state, pending: null, at: 0 }));\n}\n\n/** The menu behind an arriving dialog, or null when none was asked for recently. */\nexport function pendingMenuFor(now: number): MenuKind | null {\n  const { pending, at } = menuStore.state;\n  if (pending === null || now - at > MENU_FRESH_MS) return null;\n  return pending;\n}\n\n/**\n * Spends the pending anchor on an arriving select dialog. Called once per\n * dialog, at frame time, so every surface reads the same verdict on its\n * first render and the modal never flashes before the popover takes over.\n */\nexport function claimDialogMenu(dialogId: string, now = Date.now()): MenuClaim | null {\n  const menu = pendingMenuFor(now);\n  const claimed = menu === null ? null : { dialogId, menu };\n  clearPendingMenu();\n  menuStore.setState((state) => ({ ...state, claimed }));\n  return claimed;\n}\n\n/** Forgets a claim once its dialog is gone. */\nexport function releaseDialogMenu(dialogId: string): void {\n  menuStore.setState((state) =>\n    state.claimed !== null && state.claimed.dialogId === dialogId ? { ...state, claimed: null } : state,\n  );\n}\n\n/** Test seam. */\nexport function resetMenuStore(): void {\n  clearPendingMenu();\n  menuStore.setState(() => ({ pending: null, at: 0, claimed: null }));\n}\n"],"mappings":";;;AAsBA,MAAM,gBAAgB;AAEtB,MAAa,YAAY,WAAW,QAAQ,IAAI,MAAiB;CAAE,SAAS;CAAM,IAAI;CAAG,SAAS;AAAK,CAAC,CAAC;AAEzG,IAAI;;;;;;;;;AAUJ,SAAgB,eAAe,MAAsB;CACnD,UAAU,UAAU,WAAW;EAAE,GAAG;EAAO,SAAS;EAAM,IAAI,KAAK,IAAI;CAAE,EAAE;CAC3E,IAAI,WAAW,KAAA,GAAW,aAAa,MAAM;CAC7C,SAAS,WAAW,kBAAkB,aAAa;AACrD;AAEA,SAAgB,mBAAyB;CACvC,IAAI,WAAW,KAAA,GAAW,aAAa,MAAM;CAC7C,SAAS,KAAA;CACT,UAAU,UAAU,UAAW,MAAM,YAAY,OAAO,QAAQ;EAAE,GAAG;EAAO,SAAS;EAAM,IAAI;CAAE,CAAE;AACrG;;AAGA,SAAgB,eAAe,KAA8B;CAC3D,MAAM,EAAE,SAAS,OAAO,UAAU;CAClC,IAAI,YAAY,QAAQ,MAAM,KAAK,eAAe,OAAO;CACzD,OAAO;AACT;;;;;;AAOA,SAAgB,gBAAgB,UAAkB,MAAM,KAAK,IAAI,GAAqB;CACpF,MAAM,OAAO,eAAe,GAAG;CAC/B,MAAM,UAAU,SAAS,OAAO,OAAO;EAAE;EAAU;CAAK;CACxD,iBAAiB;CACjB,UAAU,UAAU,WAAW;EAAE,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