{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-typeahead",
  "type": "registry:lib",
  "title": "core/typeahead",
  "description": "Framework-free behaviour shared by the ui-* components (typeahead); 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/typeahead.ts",
      "type": "registry:lib",
      "target": "components/ui/core/typeahead.ts",
      "content": "/** The parts of a key press typeahead reads — a DOM `KeyboardEvent` fits, and so does React's. */\nexport interface KeyInput {\n    key: string;\n    ctrlKey?: boolean;\n    metaKey?: boolean;\n    altKey?: boolean;\n}\n\n/** Quiet time after which the typed prefix starts over (APG). */\nexport const TYPEAHEAD_RESET_MS = 500;\n\n/**\n * First item at or after `from` (wrapping) whose text starts with `prefix`, skipping items that\n * aren't `enabled`; -1 when none does. A one-letter prefix starts after `from`, so typing the same\n * letter again cycles through the items that start with it; a longer prefix may still match `from`.\n * Case-insensitive, leading / trailing spaces of the text ignored.\n */\nexport function findByPrefix<T>(items: readonly T[], prefix: string, from: number, text: (item: T) => string, enabled: (item: T) => boolean = () => true): number {\n    const n = items.length;\n    const p = prefix.toLocaleLowerCase();\n    const start = p.length === 1 ? from + 1 : Math.max(from, 0);\n    for (let k = 0; k < n; k++) {\n        const i = (((start + k) % n) + n) % n;\n        if (enabled(items[i]) && text(items[i]).trim().toLocaleLowerCase().startsWith(p)) return i;\n    }\n    return -1;\n}\n\n/**\n * APG typeahead for lists, menus and trees: printable keys typed in quick succession build a\n * prefix (it starts over after {@link TYPEAHEAD_RESET_MS} of quiet), and typing one letter\n * repeatedly cycles through the items that start with it. No timers: pass the time of each key\n * (defaults to now), so it runs anywhere and tests can step the clock.\n */\nexport class Typeahead {\n    private buffer = '';\n    private last = Number.NEGATIVE_INFINITY;\n    private readonly resetMs: number;\n\n    constructor(resetMs = TYPEAHEAD_RESET_MS) {\n        this.resetMs = resetMs;\n    }\n\n    /**\n     * Feeds a key: the prefix to search for, or undefined when the key isn't typeahead (not one\n     * printable character, a Ctrl / ⌘ / Alt chord, or a Space that would start a prefix — a lone\n     * Space activates the item instead).\n     */\n    prefix(key: KeyInput, now = Date.now()): string | undefined {\n        if (key.key.length !== 1 || key.ctrlKey || key.metaKey || key.altKey) return undefined;\n        if (now - this.last > this.resetMs) this.buffer = '';\n        if (key.key === ' ' && !this.buffer) return undefined;\n        this.last = now;\n        this.buffer += key.key.toLocaleLowerCase();\n        const repeated = [...this.buffer].every((c) => c === this.buffer[0]);\n        return repeated ? this.buffer[0] : this.buffer;\n    }\n\n    /**\n     * {@link prefix} then {@link findByPrefix} from `current`: the index to move to, -1 when nothing\n     * matches, or undefined when the key isn't typeahead (leave it to the caller).\n     */\n    match<T>(key: KeyInput, items: readonly T[], current: number, text: (item: T) => string, options: { enabled?: (item: T) => boolean; now?: number } = {}): number | undefined {\n        const prefix = this.prefix(key, options.now);\n        if (prefix === undefined) return undefined;\n        return findByPrefix(items, prefix, current, text, options.enabled);\n    }\n\n    /** Forget the typed prefix (e.g. when the list closes). */\n    reset() {\n        this.buffer = '';\n        this.last = Number.NEGATIVE_INFINITY;\n    }\n}\n"
    }
  ]
}
