{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-listbox",
  "type": "registry:lib",
  "title": "core/listbox",
  "description": "Framework-free behaviour shared by the ui-* components (listbox); no DOM, no lit. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "src/core/listbox.ts",
      "type": "registry:lib",
      "target": "components/ui/core/listbox.ts",
      "content": "/**\n * Highlight movement and text filtering for single- and multi-select lists (dropdown, combobox,\n * multi-select): plain data in, indexes out, no DOM. -1 means \"nothing highlighted\".\n */\n\n/** Anything with an optional `disabled` flag: an option, a row, a menu item. */\nexport interface Selectable {\n    readonly disabled?: boolean;\n}\n\n/** An option with the text that filtering and typeahead read. */\nexport interface LabelledOption extends Selectable {\n    readonly label: string;\n}\n\n/** How typed text narrows a list (Carbon combobox / filterable multi-select). */\nexport type TextFilter = 'contains' | 'starts-with' | 'none';\n\n/** Index of the first item that isn't disabled, or -1. */\nexport function firstEnabled(items: readonly Selectable[]): number {\n    return items.findIndex((item) => !item.disabled);\n}\n\n/** Index of the last item that isn't disabled, or -1. */\nexport function lastEnabled(items: readonly Selectable[]): number {\n    for (let i = items.length - 1; i >= 0; i--) if (!items[i].disabled) return i;\n    return -1;\n}\n\n/**\n * The item `delta` enabled steps away from `from` (±1 for the arrows, ±10 for Page Up / Down),\n * stopping at the ends (APG listbox: no wrap) and skipping disabled items; stays on `from` when\n * there is nowhere to go. With nothing highlighted (`from` < 0), Down-wards goes to the first\n * enabled item and Up-wards to the last (Carbon / downshift).\n */\nexport function stepEnabled(items: readonly Selectable[], from: number, delta: number): number {\n    // a highlight past the end (the options shrank under it) counts as none, so the arrows still move\n    if (from < 0 || from >= items.length) return delta > 0 ? firstEnabled(items) : lastEnabled(items);\n    const dir = Math.sign(delta);\n    let remaining = Math.abs(delta);\n    let reached = from;\n    for (let i = from + dir; remaining > 0 && i >= 0 && i < items.length; i += dir) {\n        if (items[i].disabled) continue;\n        reached = i;\n        remaining--;\n    }\n    return reached;\n}\n\n/** How the options lifecycle finds an option again in a changed list (`reconcileActive`). */\nexport interface ReconcileOptions<T> {\n    /** The list is open (closed, there is no highlight). */\n    open: boolean;\n    /** The options are being fetched: nothing is listed, so nothing is highlighted. */\n    loading?: boolean;\n    /** The options just arrived (`loading` ended): highlight as on opening (as when an empty list gets rows). */\n    arrived?: boolean;\n    /** Whether two options are the same one (by value). */\n    same: (a: T, b: T) => boolean;\n    /** The row the list opens on (the chosen option, else the first enabled; a combobox: the text's match). */\n    opening: (options: readonly T[]) => number;\n}\n\n/**\n * The options lifecycle of the highlight: the row to highlight after the listed options changed while the list\n * is open (they arrived, or a live filter, a server search or a permission change replaced them). Rows that come\n * into an empty list are highlighted as on opening; the option highlighted before stays highlighted where it\n * still is and enabled, else the list highlights as on opening; nothing highlighted stays so. Closed, loading or\n * empty: -1. So the arrows, Enter and aria-activedescendant always point at a row that exists.\n */\nexport function reconcileActive<T extends Selectable>(next: readonly T[], previous: readonly T[], active: number, { open, loading = false, arrived = false, same, opening }: ReconcileOptions<T>): number {\n    if (!open || loading || next.length === 0) return -1;\n    if (arrived || previous.length === 0) return opening(next);\n    if (active < 0) return -1;\n    const before = previous[active];\n    const again = before ? next.findIndex((o) => !o.disabled && same(o, before)) : -1;\n    return again >= 0 ? again : opening(next);\n}\n\n/** Whether `label` passes the typed `query` (trimmed, case-insensitive); empty text or `none` passes everything. */\nexport function matchesText(label: string, query: string, filter: TextFilter = 'contains'): boolean {\n    const q = query.trim().toLocaleLowerCase();\n    if (filter === 'none' || q === '') return true;\n    const text = label.toLocaleLowerCase();\n    return filter === 'starts-with' ? text.startsWith(q) : text.includes(q);\n}\n\n/**\n * Index of the first enabled option whose label contains `query`, or -1 (also for empty text) —\n * what Carbon's combobox highlights while typing and takes on Enter (downshift findHighlightedIndex).\n */\nexport function findText(options: readonly LabelledOption[], query: string): number {\n    if (query.trim() === '') return -1;\n    return options.findIndex((o) => !o.disabled && matchesText(o.label, query, 'contains'));\n}\n\n/** Groups narrowed to the options that `keep` accepts; groups left empty are dropped. */\nexport function filterGroups<G extends { readonly options: readonly unknown[] }>(groups: readonly G[], keep: (option: G['options'][number]) => boolean): G[] {\n    return groups.map((g) => ({ ...g, options: g.options.filter(keep) })).filter((g) => g.options.length > 0);\n}\n"
    }
  ]
}
