{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "queue-web",
  "title": "Queue (Web Components)",
  "description": "The agent's to-do list, queued and completed, that people can reorder, send now, edit and clear, or the messages queued in a Composer.",
  "dependencies": [
    "@kind-ui/agent-ui@0.9.0",
    "@ui-sdk/core@0.1.0-beta.17",
    "@ui-sdk/web@0.1.0-beta.17"
  ],
  "registryDependencies": [
    "@kind-ui/agent-ui-setup"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/queue.ts",
      "content": "import {\n  type QueueApi,\n  type QueueItemActionDetails,\n  type QueueOptions,\n  type QueueSection,\n  type QueueState,\n  queueInstructions,\n  queuePrimitive,\n  queueRows,\n  queueSection,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts, type UiActionDetail, wrappedButton } from \"@ui-sdk/web\";\nimport type {\n  AttributeProperties,\n  UiDetailedChangeEvent,\n  UiElement,\n  UiPartElement,\n} from \"@ui-sdk/web/events\";\n\ntype QueueItem = QueueState[\"items\"][number];\n\nconst SECTIONS: QueueSection[] = [\"pending\", \"complete\"];\n\nconst section = (value: QueueSection) =>\n  `<kind-queue-section value=\"${value}\"><kind-queue-section-trigger><button><span data-title></span><span data-count></span></button></kind-queue-section-trigger><kind-queue-section-content><kind-queue-list></kind-queue-list></kind-queue-section-content></kind-queue-section>`;\nconst DEFAULT_PARTS = `${SECTIONS.map(section).join(\"\")}<kind-queue-instructions></kind-queue-instructions>`;\nconst ITEM_PARTS = `<kind-queue-item-icon></kind-queue-item-icon><span data-text><kind-queue-item-title></kind-queue-item-title><kind-queue-item-description></kind-queue-item-description></span><span data-queue-actions><kind-queue-item-send><button></button></kind-queue-item-send><kind-queue-item-edit><button></button></kind-queue-item-edit><kind-queue-item-remove><button></button></kind-queue-item-remove></span>`;\n\nconst write = (el: Element | null, text: string) => {\n  if (el && el.textContent !== text) el.textContent = text;\n};\n\ntype Movable = Element & { moveBefore?: (node: Node, child: Node | null) => void };\n\n// `moveBefore` keeps a focused item focused where the browser has it; `insertBefore` blurs it.\nfunction move(list: Movable, item: Element, before: Element | null): void {\n  if (list.moveBefore && item.isConnected) list.moveBefore(item, before);\n  else list.insertBefore(item, before);\n}\n\n// Each list keeps only its own section's rows, so an item that completes gets a new element under Completed, as in\n// React.\nfunction drawList(list: Movable, items: QueueItem[], drawn: WeakMap<Element, QueueItem>): void {\n  const existing = new Map([...list.children].map((child) => [child.getAttribute(\"value\"), child]));\n  items.forEach((item, index) => {\n    let child = existing.get(item.id);\n    existing.delete(item.id);\n    if (!child) {\n      child = list.ownerDocument.createElement(\"kind-queue-item\");\n      child.setAttribute(\"value\", item.id);\n      child.innerHTML = ITEM_PARTS;\n    }\n    if (drawn.get(child) !== item) {\n      write(child.querySelector(\"kind-queue-item-title\"), item.title);\n      write(child.querySelector(\"kind-queue-item-description\"), item.description ?? \"\");\n      drawn.set(child, item);\n    }\n    if (list.children[index] !== child) move(list, child, list.children[index] ?? null);\n  });\n  for (const child of existing.values()) child.remove();\n}\n\n/**\n * `<kind-queue label=\"…\">` fills in its Queued and Completed sections and renders a `kind-queue-item` for each\n * entry of its `items` property. The `can-reorder`, `can-remove`, `can-send` and `can-edit` attributes let people\n * move, remove, send now and edit items, firing `reorder` (with `event.detail.index`), `remove`, `send` and `edit`,\n * each with the item's id as `event.detail.value`. `send` and `edit` listeners may call\n * `event.detail.waitUntil(promise)`. Inside a `kind-composer`, it docks to the composer's top edge.\n */\nexport function defineQueue(): void {\n  if (customElements.get(\"kind-queue\")) return;\n  createParts(queuePrimitive, {\n    root: {\n      tag: \"kind-queue\",\n      kinds: { canReorder: \"boolean\", canRemove: \"boolean\", canSend: \"boolean\", canEdit: \"boolean\" },\n      // Without the attribute the callback is left out; with it, `@ui-sdk/web` fills it in with an event.\n      options: (el) => {\n        const { canReorder, canRemove, canSend, canEdit } = el as KindQueueElement;\n        return {\n          ...(!canReorder && { onReorder: undefined }),\n          ...(!canRemove && { onRemove: undefined }),\n          ...(!canSend && { onSend: undefined }),\n          ...(!canEdit && { onEdit: undefined }),\n        };\n      },\n      state: {\n        openSections: {\n          get: (api) => api.getOpenSections(),\n          set: (api, openSections) => api.setOpenSections(openSections as QueueSection[]),\n        },\n      },\n      api: [\"getItems\", \"getOpenSections\", \"setOpenSections\", \"toggleSection\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = DEFAULT_PARTS;\n        const drawn = new WeakMap<Element, QueueItem>();\n        const draw = () => {\n          const state = instance.get() as QueueState;\n          for (const value of SECTIONS) {\n            const part = el.querySelector(`kind-queue-section[value=\"${value}\"]`);\n            if (!part) continue;\n            const { title, count } = queueSection(state, value);\n            write(part.querySelector(\"[data-title]\"), title);\n            write(part.querySelector(\"[data-count]\"), String(count));\n            const list = part.querySelector(\"kind-queue-list\");\n            const rows = queueRows(state.rows, state.moving, value).map((row) => row.item);\n            if (list) drawList(list, rows, drawn);\n          }\n          write(el.querySelector(\"kind-queue-instructions\"), queueInstructions(state));\n        };\n        draw();\n        const unsubscribe = instance.subscribe(draw);\n        // Sections written after the element connects get their titles, counts and items.\n        const arrivals = new MutationObserver((records) => {\n          if (records.some((record) => [...record.addedNodes].some((node) => node instanceof Element)))\n            draw();\n        });\n        arrivals.observe(el, { childList: true, subtree: true });\n        return () => {\n          unsubscribe();\n          arrivals.disconnect();\n        };\n      },\n    },\n    section: \"kind-queue-section\",\n    \"section-trigger\": { tag: \"kind-queue-section-trigger\", target: wrappedButton, display: \"contents\" },\n    \"section-content\": \"kind-queue-section-content\",\n    list: \"kind-queue-list\",\n    item: \"kind-queue-item\",\n    \"item-icon\": \"kind-queue-item-icon\",\n    \"item-title\": \"kind-queue-item-title\",\n    \"item-description\": \"kind-queue-item-description\",\n    \"item-send\": { tag: \"kind-queue-item-send\", target: wrappedButton, display: \"contents\" },\n    \"item-edit\": { tag: \"kind-queue-item-edit\", target: wrappedButton, display: \"contents\" },\n    \"item-remove\": { tag: \"kind-queue-item-remove\", target: wrappedButton, display: \"contents\" },\n    instructions: \"kind-queue-instructions\",\n  });\n}\n\nexport type KindQueueEvents = {\n  reorder: UiDetailedChangeEvent<string, Parameters<NonNullable<QueueOptions[\"onReorder\"]>>[1]>;\n  remove: UiDetailedChangeEvent<string, Parameters<NonNullable<QueueOptions[\"onRemove\"]>>[1]>;\n  send: CustomEvent<{ value: string } & QueueItemActionDetails & Pick<UiActionDetail, \"waitUntil\">>;\n  edit: CustomEvent<{ value: string } & QueueItemActionDetails & Pick<UiActionDetail, \"waitUntil\">>;\n};\n\nexport interface KindQueueElement\n  extends UiElement<KindQueueEvents>,\n    AttributeProperties<typeof queuePrimitive, \"api\" | \"items\" | \"openSections\"> {\n  canReorder: boolean;\n  canRemove: boolean;\n  /** Whether queued items show Send now, firing `send`. */\n  canSend: boolean;\n  /** Whether queued items show Edit, firing `edit`. */\n  canEdit: boolean;\n  items?: QueueOptions[\"items\"];\n  openSections: QueueSection[];\n  api: Pick<QueueApi, \"getItems\" | \"getOpenSections\" | \"setOpenSections\" | \"toggleSection\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-queue\": KindQueueElement;\n    \"kind-queue-section\": UiPartElement<typeof queuePrimitive, \"section\", \"value\">;\n    \"kind-queue-section-trigger\": HTMLElement;\n    \"kind-queue-section-content\": HTMLElement;\n    \"kind-queue-list\": HTMLElement;\n    \"kind-queue-item\": UiPartElement<typeof queuePrimitive, \"item\", \"value\">;\n    \"kind-queue-item-icon\": HTMLElement;\n    \"kind-queue-item-title\": HTMLElement;\n    \"kind-queue-item-description\": HTMLElement;\n    \"kind-queue-item-send\": HTMLElement;\n    \"kind-queue-item-edit\": HTMLElement;\n    \"kind-queue-item-remove\": HTMLElement;\n    \"kind-queue-instructions\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/queue.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
