{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-multi-select",
  "type": "registry:lib",
  "title": "core/multi-select",
  "description": "Framework-free behaviour shared by the ui-* components (multi-select); 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": [
    "@kanok-ui/core-dropdown",
    "@kanok-ui/core-listbox",
    "@kanok-ui/core-typeahead"
  ],
  "files": [
    {
      "path": "src/core/multi-select.ts",
      "type": "registry:lib",
      "target": "components/ui/core/multi-select.ts",
      "content": "/**\n * Rules of a multi-select list box (Carbon MultiSelect / FilterableMultiSelect, downshift\n * useSelect + useMultipleSelection, traced on Carbon React): the order of the rows, the \"Select all\"\n * row, and what each key does on the closed field, in the open list and in the filter text box.\n * Rows are indexes into the displayed rows; SELECT_ALL is the \"Select all\" row before them.\n */\nimport { firstEnabled, lastEnabled, stepEnabled, type LabelledOption, type Selectable } from './listbox.ts';\nimport type { KeyInput, Typeahead } from './typeahead.ts';\nimport { PAGE } from './dropdown.ts';\n\nexport const SELECT_ALL = -2;\n\n/** When chosen options move to the top (Carbon `selectionFeedback`). */\nexport type SelectionFeedback = 'top-after-reopen' | 'top' | 'fixed';\n\nexport interface MultiOption extends LabelledOption {\n    value: string;\n}\n\n/**\n * Rows in display order: chosen first per `feedback` (`top`: as they are chosen, `top-after-reopen`:\n * those chosen when the list opened, `pinned`), author order otherwise and always in groups.\n */\nexport function orderRows<O extends MultiOption>(shown: readonly O[], value: readonly string[], pinned: readonly string[], feedback: SelectionFeedback, grouped: boolean): O[] {\n    if (grouped || feedback === 'fixed') return [...shown];\n    const top = feedback === 'top' ? value : pinned;\n    return [...shown.filter((o) => top.includes(o.value)), ...shown.filter((o) => !top.includes(o.value))];\n}\n\n/** How many of the enabled options are chosen: none, some (the box shows \"mixed\") or all. */\nexport function selectAllState(enabled: readonly MultiOption[], value: readonly string[]): 'none' | 'some' | 'all' {\n    const n = enabled.filter((o) => value.includes(o.value)).length;\n    if (n === 0) return 'none';\n    return n === enabled.length ? 'all' : 'some';\n}\n\n/** The value after a row's box is toggled. */\nexport const toggleValue = (value: readonly string[], option: MultiOption) => (value.includes(option.value) ? value.filter((v) => v !== option.value) : [...value, option.value]);\n\n/** \"Select all\": unchecks the enabled options when all are chosen, else adds them; others keep their state. */\nexport function toggleAllValue(value: readonly string[], enabled: readonly MultiOption[]): string[] {\n    const values = enabled.map((o) => o.value);\n    if (selectAllState(enabled, value) === 'all') return value.filter((v) => !values.includes(v));\n    return [...new Set([...value, ...values])];\n}\n\n// ---- moving the highlight over [Select all?, ...rows] ----\n\nconst nav = (rows: readonly Selectable[], selectAll: boolean): readonly Selectable[] => (selectAll ? [{ disabled: false }, ...rows] : rows);\nconst toNav = (active: number, selectAll: boolean) => {\n    if (active === SELECT_ALL) return 0;\n    if (active < 0) return -1;\n    return selectAll ? active + 1 : active;\n};\nconst fromNav = (index: number, selectAll: boolean) => {\n    if (index < 0) return -1;\n    if (!selectAll) return index;\n    return index === 0 ? SELECT_ALL : index - 1;\n};\n\n/** The first row the keyboard reaches (\"Select all\" when shown). */\nexport const multiFirst = (rows: readonly Selectable[], selectAll: boolean) => fromNav(firstEnabled(nav(rows, selectAll)), selectAll);\nexport const multiLast = (rows: readonly Selectable[], selectAll: boolean) => fromNav(lastEnabled(nav(rows, selectAll)), selectAll);\n/** `delta` rows away, stopping at the ends; with nothing highlighted, down goes to \"Select all\" (Carbon). */\nexport const multiStep = (rows: readonly Selectable[], selectAll: boolean, active: number, delta: number) => fromNav(stepEnabled(nav(rows, selectAll), toNav(active, selectAll), delta), selectAll);\n\nexport interface MultiState {\n    open: boolean;\n    /** Highlighted row, SELECT_ALL, or -1. */\n    active: number;\n    /** Something is chosen. */\n    hasValue: boolean;\n}\n\n/** An intent for the renderer. */\nexport interface MultiStep {\n    open?: boolean;\n    /** Opens with this highlight (`undefined`: the first row the keyboard reaches). */\n    active?: number;\n    /** Toggle this row (or SELECT_ALL); the list stays open. */\n    toggle?: number;\n    clearAll?: boolean;\n    /** Filterable: empty the typed text. */\n    clearText?: boolean;\n    close?: boolean;\n    handled: boolean;\n}\n\nfunction moves(rows: readonly Selectable[], selectAll: boolean, active: number, key: string, homeEnd: boolean): number | undefined {\n    switch (key) {\n        case 'ArrowDown':\n            return multiStep(rows, selectAll, active, 1);\n        case 'ArrowUp':\n            return multiStep(rows, selectAll, active, -1);\n        case 'PageDown':\n            return multiStep(rows, selectAll, active, PAGE);\n        case 'PageUp':\n            return multiStep(rows, selectAll, active, -PAGE);\n        case 'Home':\n            return homeEnd ? multiFirst(rows, selectAll) : undefined;\n        case 'End':\n            return homeEnd ? multiLast(rows, selectAll) : undefined;\n        default:\n            return undefined;\n    }\n}\n\n/** Letters jump to the next row starting with them (rows only, not \"Select all\"), opening a closed list. */\nfunction typeaheadStep(state: MultiState, rows: readonly MultiOption[], key: KeyInput, typeahead: Typeahead, now: number): MultiStep | undefined {\n    const match = typeahead.match(key, rows, Math.max(state.active, -1), (o) => o.label, { enabled: (o) => !o.disabled, now });\n    if (match === undefined) return undefined;\n    if (match < 0) return { handled: true };\n    return state.open ? { active: match, handled: true } : { open: true, active: match, handled: true };\n}\n\n/**\n * Keys on the button of a (non-filterable) multi-select. Closed: ↓ / Enter / Space open on the\n * first row, ↑ does nothing, Home / End open on the first / last, Delete / Backspace / Esc clear\n * every choice, letters open on the match. Open: arrows / Page / Home / End move, Enter / Space\n * toggle the row (the list stays open), Esc closes, Tab closes and moves on, letters jump.\n */\nexport function multiSelectKey(state: MultiState, rows: readonly MultiOption[], selectAll: boolean, key: KeyInput, typeahead: Typeahead, now = Date.now()): MultiStep | undefined {\n    if (!state.open) {\n        switch (key.key) {\n            case 'ArrowDown':\n            case 'Enter':\n            case ' ':\n                return { open: true, handled: true };\n            case 'ArrowUp':\n                return { handled: true };\n            case 'Home':\n                return { open: true, active: multiFirst(rows, selectAll), handled: true };\n            case 'End':\n                return { open: true, active: multiLast(rows, selectAll), handled: true };\n            case 'Delete':\n            case 'Backspace':\n            case 'Escape':\n                return { clearAll: state.hasValue, handled: true };\n            default:\n                return typeaheadStep(state, rows, key, typeahead, now);\n        }\n    }\n    const moved = moves(rows, selectAll, state.active, key.key, true);\n    if (moved !== undefined) return { active: moved, handled: true };\n    if (key.key === 'Enter' || key.key === ' ') return { toggle: state.active, handled: true };\n    if (key.key === 'Escape') return { close: true, handled: true };\n    if (key.key === 'Tab') return { close: true, handled: false };\n    return typeaheadStep(state, rows, key, typeahead, now);\n}\n\n/**\n * Keys in the text box of a filterable multi-select (Home / End / Space / Backspace / Delete edit\n * the text). Closed: ↓ / Enter open, Esc clears the choices and the text. Open: arrows / Page move,\n * Enter toggles the row (or closes with nothing highlighted), Esc closes and keeps the text. Keys of an input method\n * that is composing are left to it.\n */\nexport function multiFilterKey(state: MultiState, rows: readonly MultiOption[], selectAll: boolean, key: KeyInput & { isComposing?: boolean; keyCode?: number }): MultiStep | undefined {\n    // an input method still composing a word owns its keys (its Enter confirms the word; Safari's last one is keyCode 229)\n    if (key.isComposing || key.keyCode === 229) return undefined;\n    if (!state.open) {\n        if (key.key === 'ArrowDown' || key.key === 'Enter') return { open: true, handled: true };\n        if (key.key === 'Escape') return { clearAll: state.hasValue, clearText: true, handled: true };\n        return undefined;\n    }\n    const moved = moves(rows, selectAll, state.active, key.key, false);\n    if (moved !== undefined) return { active: moved, handled: true };\n    if (key.key === 'Enter') return state.active === -1 ? { close: true, handled: true } : { toggle: state.active, handled: true };\n    if (key.key === 'Escape') return { close: true, handled: true };\n    return undefined;\n}\n"
    }
  ]
}
