{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-tree",
  "type": "registry:lib",
  "title": "core/tree",
  "description": "Framework-free behaviour shared by the ui-* components (tree); 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/tree.ts",
      "type": "registry:lib",
      "target": "components/ui/core/tree.ts",
      "content": "/**\n * Carbon `TreeView` rules and the APG tree keyboard, on any node type: the renderer says how to\n * find a node's parent and whether it is expanded (DOM elements in `<ui-tree-view>`, React's own\n * records), the core decides indents, what is visible, where the arrows go and what is selected.\n */\n\n/** Carbon calcOffset (rem → px): where a row's content starts — branches depth + 1 (+ 0.5·depth with icons), leaves depth + 2.5 (depth + 2 + 0.5·depth with icons). */\nexport function treeNodeOffset(depth: number, branch: boolean, icon: boolean): number {\n    if (branch) return icon ? 16 * (depth + 1) + 8 * depth : 16 * (depth + 1);\n    return icon ? 16 * (depth + 2) + 8 * depth : 16 * (depth + 2.5);\n}\n\n/** How far a node's children are indented, and the row's own indent: 16px for branches, 40px for leaves (32px with an icon). */\nexport function treeNodeIndent(branch: boolean, icon: boolean): number {\n    if (branch) return 16;\n    return icon ? 32 : 40;\n}\n\n/** How deep `node` is (0 at the top level). */\nexport function treeDepth<T>(node: T, parentOf: (node: T) => T | null): number {\n    let depth = 0;\n    for (let p = parentOf(node); p; p = parentOf(p)) depth++;\n    return depth;\n}\n\n/** The nodes whose every ancestor is expanded — what the user can see and arrow through — in the given (document) order. */\nexport function visibleTreeNodes<T>(nodes: readonly T[], parentOf: (node: T) => T | null, expanded: (node: T) => boolean): T[] {\n    return nodes.filter((node) => {\n        for (let p = parentOf(node); p; p = parentOf(p)) if (!expanded(p)) return false;\n        return true;\n    });\n}\n\n/** Up / Down / Home / End: the visible node to focus (undefined for other keys or at an end). */\nexport function treeMoveTarget<T>(key: string, node: T, visible: readonly T[]): T | undefined {\n    if (key === 'Home') return visible[0];\n    if (key === 'End') return visible.at(-1);\n    if (key !== 'ArrowDown' && key !== 'ArrowUp') return undefined;\n    const at = visible.indexOf(node);\n    if (at < 0) return undefined;\n    return visible[key === 'ArrowDown' ? at + 1 : at - 1];\n}\n\nexport interface TreeNodeState<T> {\n    branch: boolean;\n    expanded: boolean;\n    disabled: boolean;\n    /** The first child (none yet while a lazy branch loads). */\n    firstChild?: T;\n    parent: T | null;\n}\n\n/**\n * Right / Left (APG): Right expands a closed branch or moves to its first child; Left collapses an\n * open branch or moves to the parent; right-to-left (`rtl`, the tree indented from the right) the two\n * swap. `handled` says whether the key belongs to the tree (prevent its default) even when nothing changes.\n */\nexport function treeSideStep<T>(key: string, node: TreeNodeState<T>, rtl = false): { handled: boolean; expand?: boolean; focus?: T } {\n    if (rtl && (key === 'ArrowRight' || key === 'ArrowLeft')) key = key === 'ArrowRight' ? 'ArrowLeft' : 'ArrowRight';\n    if (key === 'ArrowRight') {\n        if (!node.branch || node.disabled) return { handled: false };\n        if (!node.expanded) return { handled: true, expand: true };\n        return { handled: true, focus: node.firstChild };\n    }\n    if (key === 'ArrowLeft') {\n        if (node.branch && node.expanded && !node.disabled) return { handled: true, expand: false };\n        return { handled: true, focus: node.parent ?? undefined };\n    }\n    return { handled: false };\n}\n\n/** The selection after choosing `value`: just it, or with `toggle` (Ctrl / ⌘ + click, Space in a multiselect tree) it added or removed. */\nexport function nextTreeSelection(selected: readonly string[], value: string, toggle: boolean): string[] {\n    if (!toggle) return [value];\n    return selected.includes(value) ? selected.filter((v) => v !== value) : [...selected, value];\n}\n\n/** Same values in the same order. */\nexport const sameTreeSelection = (a: readonly string[], b: readonly string[]) => a.length === b.length && a.every((v, i) => v === b[i]);\n\n/**\n * The tree's one Tab stop (APG): among the visible, enabled nodes, the one that had it, else the\n * first selected, else the first.\n */\nexport function treeTabStop<T>(reachable: readonly T[], hadIt: (node: T) => boolean, selected: (node: T) => boolean): T | undefined {\n    return reachable.find(hadIt) ?? reachable.find(selected) ?? reachable[0];\n}\n"
    }
  ]
}
