{"version":3,"file":"workspacesStore.cjs","names":["traceStore","Store","defaultHostId","asWorkspace","qualifyHostId","splitHostId"],"sources":["../../../src/web/stores/workspacesStore.ts"],"sourcesContent":["import { Store } from '@tanstack/store';\n\nimport type { WorkspaceSummary } from '../../types/hub';\nimport { defaultHostId, qualifyHostId, splitHostId } from '../lib/hostIdentity';\nimport { traceStore } from '../lib/uiTrace';\nimport { asWorkspace } from '../lib/workspaceSummary';\n\nexport interface WorkspacesState {\n  order: string[];\n  byId: Record<string, WorkspaceSummary>;\n  selectedId: string | null;\n  hydrated: boolean;\n}\n\nconst initialState: WorkspacesState = { order: [], byId: {}, selectedId: null, hydrated: false };\n\nexport const workspacesStore = traceStore('workspaces', new Store<WorkspacesState>(initialState));\n\n/** Display names supplied by each browser host's composition root. IDs still own routing. */\nexport const workspaceHostLabelsStore = new Store<Readonly<Record<string, string>>>({});\nexport function setWorkspaceHostLabel(hostId: string, label: string | undefined): void {\n  workspaceHostLabelsStore.setState((state) => {\n    const next = { ...state };\n    if (label === undefined) delete next[hostId];\n    else next[hostId] = label;\n    return next;\n  });\n}\n/** A host-less frame belongs to the default host: the local hub, or a Worker shell's only host. */\nexport function applyWorkspacesSnapshot(frame: Record<string, unknown>, hostId = defaultHostId()): void {\n  if (!Array.isArray(frame.workspaces)) return;\n  const workspaces = frame.workspaces\n    .map(asWorkspace)\n    .filter((entry): entry is WorkspaceSummary => entry !== undefined)\n    .map((workspace) => (hostId === undefined ? workspace : { ...workspace, id: qualifyHostId(hostId, workspace.id) }));\n  workspacesStore.setState((state) => {\n    const retained = Object.entries(state.byId).filter(([id]) => splitHostId(id)?.hostId !== hostId);\n    const byId = Object.fromEntries([\n      ...retained,\n      ...workspaces.map((workspace) => [\n        workspace.id,\n        {\n          ...(state.byId[workspace.id]?.instructions === undefined\n            ? {}\n            : { instructions: state.byId[workspace.id]!.instructions }),\n          ...workspace,\n        },\n      ]),\n    ]);\n    const selectedId =\n      state.selectedId !== null && byId[state.selectedId] !== undefined\n        ? state.selectedId\n        : (workspaces[0]?.id ?? null);\n    return {\n      order: [\n        ...state.order.filter((id) => splitHostId(id)?.hostId !== hostId),\n        ...workspaces.map((workspace) => workspace.id),\n      ],\n      byId,\n      selectedId,\n      hydrated: true,\n    };\n  });\n}\n\nexport function applyWorkspaceUpsert(frame: Record<string, unknown>, hostId = defaultHostId()): void {\n  const parsed = asWorkspace(frame.workspace);\n  if (parsed === undefined) return;\n  const workspace = hostId === undefined ? parsed : { ...parsed, id: qualifyHostId(hostId, parsed.id) };\n  workspacesStore.setState((state) => ({\n    ...state,\n    order: state.byId[workspace.id] === undefined ? [...state.order, workspace.id] : state.order,\n    byId: {\n      ...state.byId,\n      [workspace.id]: {\n        ...(state.byId[workspace.id]?.instructions === undefined\n          ? {}\n          : { instructions: state.byId[workspace.id]!.instructions }),\n        ...workspace,\n      },\n    },\n    selectedId: state.selectedId ?? workspace.id,\n    hydrated: true,\n  }));\n}\n\nexport function applyWorkspaceRemoved(frame: Record<string, unknown>, hostId = defaultHostId()): void {\n  if (typeof frame.workspaceId !== 'string') return;\n  const workspaceId = hostId === undefined ? frame.workspaceId : qualifyHostId(hostId, frame.workspaceId);\n  workspacesStore.setState((state) => {\n    if (state.byId[workspaceId] === undefined) return state;\n    const byId = { ...state.byId };\n    delete byId[workspaceId];\n    return {\n      ...state,\n      order: state.order.filter((id) => id !== workspaceId),\n      byId,\n      selectedId: state.selectedId === workspaceId ? null : state.selectedId,\n    };\n  });\n}\n\nexport function selectWorkspace(workspaceId: string | null): void {\n  workspacesStore.setState((state) =>\n    state.selectedId === workspaceId ? state : { ...state, selectedId: workspaceId },\n  );\n}\n\nexport function resetWorkspaces(): void {\n  workspacesStore.setState(() => initialState);\n}\n"],"mappings":";;;;;AAcA,MAAM,eAAgC;CAAE,OAAO,CAAC;CAAG,MAAM,CAAC;CAAG,YAAY;CAAM,UAAU;AAAM;AAE/F,MAAa,kBAAkBA,gBAAAA,WAAW,cAAc,IAAIC,gBAAAA,MAAuB,YA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