{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-combobox",
  "type": "registry:lib",
  "title": "core/combobox",
  "description": "Framework-free behaviour shared by the ui-* components (combobox); 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-listbox",
    "@kanok-ui/core-typeahead"
  ],
  "files": [
    {
      "path": "src/core/combobox.ts",
      "type": "registry:lib",
      "target": "components/ui/core/combobox.ts",
      "content": "/**\n * Rules of an editable combobox (Carbon ComboBox / downshift useCombobox, traced on Carbon React):\n * the text shown for a value, which options the typed text keeps, what a key does, and what\n * leaving the field commits. Results are intents; the renderer applies them.\n */\nimport { filterGroups, findText, firstEnabled, lastEnabled, matchesText, stepEnabled, type LabelledOption, type TextFilter } from './listbox.ts';\nimport type { KeyInput } from './typeahead.ts';\n\nexport interface ComboOption extends LabelledOption {\n    value: string;\n}\n\n/** The text that names `value`: its option's label, else (free text allowed) the value itself, else ''. */\nexport function committedText(options: readonly ComboOption[], value: string, allowCustomValue: boolean): string {\n    const chosen = options.find((o) => o.value === value);\n    if (chosen) return chosen.label;\n    return allowCustomValue ? value : '';\n}\n\n/** What the text box showed, and for which value and options (`syncedComboText`). */\nexport interface ComboTextSync {\n    text: string;\n    value: string;\n    /** The value the text was last filled for (null: never, or after a reset). */\n    syncedValue: string | null;\n    /** The options before this change. */\n    previousOptions: readonly ComboOption[];\n}\n\n/**\n * The options lifecycle of the text box: the text to show after the value or the options changed, or undefined\n * to keep what is there. An outside value change fills in its option's label. Otherwise, text the user hasn't\n * touched (it still names the value with the old options) follows the new options: options that arrive after the\n * value (an async list) and a renamed option fill the box; what the user typed stays.\n */\nexport function syncedComboText({ text, value, syncedValue, previousOptions }: ComboTextSync, options: readonly ComboOption[], allowCustomValue: boolean): string | undefined {\n    const next = committedText(options, value, allowCustomValue);\n    if (value !== syncedValue) return next;\n    if (text === committedText(previousOptions, value, allowCustomValue) && text !== next) return next;\n    return undefined;\n}\n\n/**\n * The groups the typed text keeps. Text still equal to the committed choice doesn't filter:\n * opening the list to change a choice shows every option (MUI / React Aria; Carbon would show one row).\n */\nexport function comboFilter<G extends { readonly options: readonly ComboOption[] }>(groups: readonly G[], text: string, committed: string, filter: TextFilter): readonly G[] {\n    if (filter === 'none' || text.trim() === '' || text === committed) return groups;\n    return filterGroups(groups, (o) => matchesText(o.label, text, filter));\n}\n\n/** What leaving the field (or closing without a pick) does with the typed text (Carbon's blur rule). */\nexport function commitText(text: string, options: readonly ComboOption[], value: string, allowCustomValue: boolean): { value: string; text: string } {\n    const typed = text.trim();\n    const exact = options.find((o) => !o.disabled && o.label === typed);\n    if (exact) return { value: exact.value, text: exact.label };\n    if (allowCustomValue) return { value: typed, text: typed };\n    return { value, text: committedText(options, value, allowCustomValue) }; // snap back\n}\n\nexport interface ComboState {\n    open: boolean;\n    active: number;\n    text: string;\n    value: string;\n    /** The options are being fetched: the list shows only \"Searching…\", so there is no row to move to or pick. */\n    loading?: boolean;\n}\n\n/** A key, and whether it belongs to an input method still composing text (Safari's Enter that ends a composition is keyCode 229). */\nexport interface ComboKey extends KeyInput {\n    isComposing?: boolean;\n    keyCode?: number;\n}\n\n/** An intent for the renderer: open / highlight, pick a listed row, commit the text, clear. */\nexport interface ComboStep {\n    open?: boolean;\n    active?: number;\n    /** Pick this row of the listed (filtered) options and close. */\n    pick?: number;\n    /** Pick this option of all options (Enter with no highlight takes the option the text names, else the first containing it). */\n    pickOption?: ComboOption;\n    /** Commit the typed text (blur rule) and close. */\n    commit?: boolean;\n    /** Close without a pick. */\n    close?: boolean;\n    /** Empty the value (and, with `clearText`, the text). */\n    clearValue?: boolean;\n    clearText?: boolean;\n    /** Prevent the key's default. */\n    handled: boolean;\n}\n\n/** Alt+↓ opens without moving the highlight; Alt+↑ commits the highlight and closes. */\nfunction altArrowStep(state: ComboState, dir: 1 | -1): ComboStep {\n    if (dir === 1 && !state.open) return { open: true, active: -1, handled: true };\n    if (dir === -1 && state.open) return { pick: state.active >= 0 ? state.active : undefined, close: true, handled: true };\n    return { handled: true };\n}\n\n/** ↓ / ↑: open on the chosen option (or the edge), else move the highlight. */\nfunction arrowStep(state: ComboState, listed: readonly ComboOption[], dir: 1 | -1): ComboStep {\n    const edge = dir === 1 ? firstEnabled(listed) : lastEnabled(listed);\n    if (!state.open) {\n        const chosen = listed.findIndex((o) => o.value === state.value && !o.disabled);\n        return { open: true, active: chosen >= 0 ? chosen : edge, handled: true };\n    }\n    return { active: state.active < 0 ? edge : stepEnabled(listed, state.active, dir), handled: true };\n}\n\nfunction enterStep(state: ComboState, options: readonly ComboOption[], allowCustomValue: boolean): ComboStep | undefined {\n    if (state.open && state.active >= 0) return { pick: state.active, close: true, handled: true };\n    if (allowCustomValue) return { commit: true, close: true, handled: state.open };\n    // the text still names the choice: Enter keeps it (the first option containing \"Bangkok\" may be \"North Bangkok\")\n    if (state.value && state.text !== '' && state.text === committedText(options, state.value, false)) return state.open ? { close: true, handled: true } : undefined;\n    // Carbon: with no highlight, Enter takes the first enabled option containing the text; an option it names exactly comes first\n    const typed = state.text.trim().toLowerCase();\n    const match = options.find((o) => !o.disabled && typed !== '' && o.label.toLowerCase() === typed) ?? options[findText(options, state.text)];\n    if (match) return { pickOption: match, close: true, handled: true };\n    if (state.value && state.text !== committedText(options, state.value, allowCustomValue)) return { clearValue: true, handled: true }; // the text no longer names the choice\n    if (!state.open) return { open: true, active: -1, handled: true };\n    return undefined;\n}\n\n/** The step for `key`; undefined when the key isn't the combobox's (typing goes to the text box, a composing key to the input method). */\nexport function comboboxKey(state: ComboState, listed: readonly ComboOption[], options: readonly ComboOption[], key: ComboKey, allowCustomValue: boolean): ComboStep | undefined {\n    if (key.isComposing || key.keyCode === 229) return undefined;\n    const dir = { ArrowDown: 1, ArrowUp: -1 }[key.key] as 1 | -1 | undefined;\n    // while loading: the arrows only open the list (to show \"Searching…\"), Enter picks nothing from options the user can't see\n    if (state.loading && (dir || key.key === 'Enter')) return dir && !state.open ? { open: true, active: -1, handled: true } : { handled: true };\n    if (dir) return key.altKey ? altArrowStep(state, dir) : arrowStep(state, listed, dir);\n    if (key.key === 'Enter') return enterStep(state, options, allowCustomValue);\n    if (key.key === 'Escape') return state.open ? { close: true, handled: true } : { clearValue: true, clearText: true, handled: true };\n    return undefined; // Tab: the blur rule (commitText) on focus out, never the highlight\n}\n\n/** Typing highlights the first enabled listed option that contains the text (downshift findHighlightedIndex), opening the list. */\nexport const highlightForText = (listed: readonly ComboOption[], text: string) => findText(listed, text);\n"
    }
  ]
}
