{"version":3,"file":"leaderTree.cjs","names":[],"sources":["../../../src/web/lib/leaderTree.ts"],"sourcesContent":["import type { LeaderBindingContribution } from '@agimon-ai/po-core/web';\n\nexport interface LeaderOption {\n  key: string;\n  label: string;\n  detail: string;\n  /** A group opens the next level; a leaf carries the binding that fires. */\n  binding?: LeaderBindingContribution;\n  /** The keys one level down, for a group's preview; empty for a leaf. */\n  children: LeaderOption[];\n}\n\nexport interface LeaderGroup {\n  /** The label of the segment that opened this level; 'leader' at the root. */\n  label: string;\n  options: LeaderOption[];\n}\n\ninterface LeaderNode {\n  key: string;\n  label: string;\n  detail: string;\n  binding?: LeaderBindingContribution;\n  children: Map<string, LeaderNode>;\n}\n\nconst ROOT_LABEL = 'leader';\n\n/**\n * Inserts one binding the way the TUI registry does: a group segment keeps\n * the label of whoever registered it first (adopting a detail if it had\n * none), and a leaf that is already bound is taken over by the newcomer,\n * along with any group that stood where the leaf now goes.\n */\nfunction insert(root: LeaderNode, binding: LeaderBindingContribution): void {\n  let current = root;\n  for (const [index, segment] of binding.path.entries()) {\n    const isLeaf = index === binding.path.length - 1;\n    let node = current.children.get(segment.key);\n    if (!node) {\n      node = { key: segment.key, label: segment.label, detail: segment.detail ?? '', children: new Map() };\n      current.children.set(segment.key, node);\n    } else if (!node.detail && segment.detail) {\n      node.detail = segment.detail;\n    }\n    if (isLeaf) {\n      node.children.clear();\n      node.binding = binding;\n      node.label = segment.label;\n      node.detail = segment.detail ?? '';\n    } else if (node.binding) {\n      delete node.binding;\n    }\n    current = node;\n  }\n}\n\nexport interface LeaderConflict {\n  /** A later binding took a bound leaf (or a whole group) over, or a group segment was worded differently. */\n  kind: 'leaf-override' | 'group-label';\n  /** The keys of the contested segment, space separated. */\n  path: string;\n  /** The binding the tree keeps: the later one for a leaf, the first namer for a group label. */\n  winner: LeaderBindingContribution;\n  loser: LeaderBindingContribution;\n}\n\nfunction leavesUnder(node: LeaderNode): LeaderBindingContribution[] {\n  const leaves: LeaderBindingContribution[] = [];\n  if (node.binding) leaves.push(node.binding);\n  for (const child of node.children.values()) leaves.push(...leavesUnder(child));\n  return leaves;\n}\n\n/**\n * Every place `insert` would silently prefer one binding over another, in\n * the same order insert walks them: a leaf landing on a bound leaf or over a\n * group displaces those bindings, a group segment crossing a bound leaf\n * displaces it, and a group segment worded differently from the first namer\n * keeps the first wording.\n */\nexport function leaderConflicts(bindings: readonly LeaderBindingContribution[]): LeaderConflict[] {\n  const conflicts: LeaderConflict[] = [];\n  const root: LeaderNode = { key: '', label: ROOT_LABEL, detail: '', children: new Map() };\n  const namers = new Map<LeaderNode, LeaderBindingContribution>();\n  for (const binding of bindings) {\n    let current = root;\n    const keys: string[] = [];\n    for (const [index, segment] of binding.path.entries()) {\n      keys.push(segment.key);\n      const path = keys.join(' ');\n      const isLeaf = index === binding.path.length - 1;\n      let node = current.children.get(segment.key);\n      if (node) {\n        if (isLeaf) {\n          for (const loser of leavesUnder(node)) {\n            if (loser !== binding) conflicts.push({ kind: 'leaf-override', path, winner: binding, loser });\n          }\n        } else {\n          if (node.binding && node.binding !== binding) {\n            conflicts.push({ kind: 'leaf-override', path, winner: binding, loser: node.binding });\n          }\n          const namer = namers.get(node);\n          if (namer !== undefined && namer !== binding && node.label !== segment.label) {\n            conflicts.push({ kind: 'group-label', path, winner: namer, loser: binding });\n          }\n        }\n      } else {\n        node = { key: segment.key, label: segment.label, detail: '', children: new Map() };\n        current.children.set(segment.key, node);\n        namers.set(node, binding);\n      }\n      if (isLeaf) {\n        node.children.clear();\n        node.binding = binding;\n        node.label = segment.label;\n        namers.set(node, binding);\n      } else if (node.binding) {\n        delete node.binding;\n      }\n      current = node;\n    }\n  }\n  return conflicts;\n}\n\nfunction toOption(node: LeaderNode): LeaderOption {\n  const children = [...node.children.values()]\n    .sort((left, right) => left.key.localeCompare(right.key))\n    .map((child) => ({ key: child.key, label: child.label, detail: child.detail, children: [] }));\n  return {\n    key: node.key,\n    label: node.label,\n    detail: node.detail,\n    ...(node.binding ? { binding: node.binding } : {}),\n    children,\n  };\n}\n\n/**\n * The options one level of Leader Space offers, keyed by what the reader\n * would press, sorted by key. Undefined when the path runs off the tree or\n * lands on a leaf, which is how the palette knows a path is no longer a group.\n */\nexport function leaderGroup(\n  bindings: readonly LeaderBindingContribution[],\n  path: readonly string[],\n): LeaderGroup | undefined {\n  const root: LeaderNode = { key: '', label: ROOT_LABEL, detail: '', children: new Map() };\n  for (const binding of bindings) insert(root, binding);\n  let current = root;\n  for (const key of path) {\n    const next = current.children.get(key);\n    if (!next || next.binding) return undefined;\n    current = next;\n  }\n  const options = [...current.children.values()].sort((left, right) => left.key.localeCompare(right.key)).map(toOption);\n  return { label: current.label, options };\n}\n"],"mappings":";AA0BA,MAAM,aAAa;;;;;;;AAQnB,SAAS,OAAO,MAAkB,SAA0C;CAC1E,IAAI,UAAU;CACd,KAAK,MAAM,CAAC,OAAO,YAAY,QAAQ,KAAK,QAAQ,GAAG;EACrD,MAAM,SAAS,UAAU,QAAQ,KAAK,SAAS;EAC/C,IAAI,OAAO,QAAQ,SAAS,IAAI,QAAQ,GAAG;EAC3C,IAAI,CAAC,MAAM;GACT,OAAO;IAAE,KAAK,QAAQ;IAAK,OAAO,QAAQ;IAAO,QAAQ,QAAQ,UAAU;IAAI,0BAAU,IAAI,IAAI;GAAE;GACnG,QAAQ,SAAS,IAAI,QAAQ,KAAK,IAAI;EACxC,OAAO,IAAI,CAAC,KAAK,UAAU,QAAQ,QACjC,KAAK,SAAS,QAAQ;EAExB,IAAI,QAAQ;GACV,KAAK,SAAS,MAAM;GACpB,KAAK,UAAU;GACf,KAAK,QAAQ,QAAQ;GACrB,KAAK,SAAS,QAAQ,UAAU;EAClC,OAAO,IAAI,KAAK,SACd,OAAO,KAAK;EAEd,UAAU;CACZ;AACF;AAYA,SAAS,YAAY,MAA+C;CAClE,MAAM,SAAsC,CAAC;CAC7C,IAAI,KAAK,SAAS,OAAO,KAAK,KAAK,OAAO;CAC1C,KAAK,MAAM,SAAS,KAAK,SAAS,OAA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