{"version":3,"file":"pathUtils.mjs","names":[],"sources":["../../src/utils/pathUtils.ts"],"sourcesContent":["import type { Path, RuleGroupTypeAny, RuleType } from '../types';\nimport { isRuleGroup } from './isRuleGroup';\nimport { isPojo } from './misc';\n\n/**\n * Return type for {@link findPath}.\n */\nexport type FindPathReturnType = RuleGroupTypeAny | RuleType | null;\n\n/**\n * Returns the {@link RuleType} or {@link RuleGroupType}/{@link RuleGroupTypeIC}\n * at the given path within a query.\n */\nexport const findPath = (path: Path, query: RuleGroupTypeAny): FindPathReturnType => {\n  let target: FindPathReturnType = query;\n  let level = 0;\n  while (level < path.length && target && isRuleGroup(target)) {\n    const t: RuleGroupTypeAny | RuleType | string = target.rules[path[level]];\n    target = typeof t === 'string' ? null : t;\n    level++;\n  }\n\n  return level < path.length ? null : target;\n};\n\n/**\n * Returns the {@link RuleType} or {@link RuleGroupType}/{@link RuleGroupTypeIC}\n * with the given `id` within a query.\n */\nexport const findID = (id: string, query: RuleGroupTypeAny): FindPathReturnType => {\n  if (query.id === id) {\n    return query;\n  }\n\n  for (const rule of query.rules) {\n    if (typeof rule === 'string') continue;\n    if (rule.id === id) {\n      return rule;\n    } else if (isRuleGroup(rule)) {\n      const subRule = findID(id, rule);\n      if (subRule) {\n        return subRule;\n      }\n    }\n  }\n\n  return null;\n};\n\n/**\n * Returns the {@link Path} of the {@link RuleType} or {@link RuleGroupType}/{@link RuleGroupTypeIC}\n * with the given `id` within a query.\n */\nexport const getPathOfID = (id: string, query: RuleGroupTypeAny): Path | null => {\n  if (query.id === id) return [];\n\n  const idx = query.rules.findIndex(r => !(typeof r === 'string') && r.id === id);\n\n  if (idx >= 0) {\n    return [idx];\n  }\n\n  for (const [i, r] of Object.entries(query.rules)) {\n    if (isRuleGroup(r)) {\n      const subPath = getPathOfID(id, r);\n      if (Array.isArray(subPath)) {\n        return [Number.parseInt(i), ...subPath];\n      }\n    }\n  }\n\n  return null;\n};\n\n/**\n * Truncates the last element of an array and returns the result as a new array.\n */\nexport const getParentPath = (path: Path): Path => path.slice(0, -1);\n\n/**\n * Determines if two paths (each `Path`) are equivalent.\n */\nexport const pathsAreEqual = (path1: Path, path2: Path): boolean => {\n  if (path1.length !== path2.length) return false;\n  for (let i = 0; i < path1.length; i++) {\n    if (path1[i] !== path2[i]) return false;\n  }\n  return true;\n};\n\n/**\n * Determines if the first path is an ancestor of the second path. The first path must\n * be shorter and exactly match the second path up through the length of the first path.\n */\nexport const isAncestor = (maybeAncestor: Path, path: Path): boolean => {\n  if (maybeAncestor.length >= path.length) return false;\n  for (let i = 0; i < maybeAncestor.length; i++) {\n    if (maybeAncestor[i] !== path[i]) return false;\n  }\n  return true;\n};\n\n/**\n * Finds the deepest/longest path that two paths have in common.\n */\nexport const getCommonAncestorPath = (path1: Path, path2: Path): Path => {\n  const commonAncestorPath: Path = [];\n  const parentPath1 = getParentPath(path1);\n  const parentPath2 = getParentPath(path2);\n  let i = 0;\n\n  while (i < parentPath1.length && i < parentPath2.length && parentPath1[i] === parentPath2[i]) {\n    commonAncestorPath.push(parentPath2[i]);\n    i++;\n  }\n\n  return commonAncestorPath;\n};\n\n/**\n * Determines if the rule or group at the specified path is either disabled itself\n * or disabled by an ancestor group.\n */\nexport const pathIsDisabled = (path: Path, query: RuleGroupTypeAny): boolean => {\n  let disabled = !!query.disabled;\n  let target: RuleType | RuleGroupTypeAny = query;\n  let level = 0;\n  while (level < path.length && !disabled && isRuleGroup(target)) {\n    const t: RuleGroupTypeAny | RuleType | string = target.rules[path[level]];\n    if (isPojo(t) && (isRuleGroup(t) || ('field' in t && !!t.field))) {\n      disabled = !!t.disabled;\n      target = t;\n    }\n    level++;\n  }\n  return disabled;\n};\n\n/**\n * Determines if the rule or group at the specified path is disabled by `disabledPaths`—the array\n * form of the `QueryBuilder` `disabled` prop, which disables nodes by position rather than by a\n * `disabled` property on the node itself. A path is disabled if it appears in `disabledPaths` or\n * descends from a path that does.\n */\nexport const pathIsDisabledByPaths = (path: Path, disabledPaths: Path[] = []): boolean =>\n  disabledPaths.some(p => pathsAreEqual(p, path) || isAncestor(p, path));\n\n/** The path of a child rule or group, and whether it is disabled. */ export interface PathInfo {\n  path: Path;\n  disabled: boolean;\n}\n\n/**\n * Builds the {@link PathInfo} for each child of a group at `path`. A child is disabled if its\n * parent is disabled or if its own path appears in `disabledPaths`.\n */\nexport const derivePathInfo = (\n  path: Path,\n  childCount: number,\n  { disabled = false, disabledPaths = [] }: { disabled?: boolean; disabledPaths?: Path[] } = {}\n): PathInfo[] => {\n  const paths: PathInfo[] = [];\n  for (let i = 0; i < childCount; i++) {\n    const thisPath = [...path, i];\n    paths[i] = {\n      path: thisPath,\n      disabled: disabled || disabledPaths.some(p => pathsAreEqual(thisPath, p)),\n    };\n  }\n  return paths;\n};\n"],"mappings":";;;;;;;AAaA,MAAa,YAAY,MAAY,UAAgD;CACnF,IAAI,SAA6B;CACjC,IAAI,QAAQ;CACZ,OAAO,QAAQ,KAAK,UAAU,UAAU,YAAY,MAAM,GAAG;EAC3D,MAAM,IAA0C,OAAO,MAAM,KAAK;EAClE,SAAS,OAAO,MAAM,WAAW,OAAO;EACxC;CACF;CAEA,OAAO,QAAQ,KAAK,SAAS,OAAO;AACtC;;;;;AAMA,MAAa,UAAU,IAAY,UAAgD;CACjF,IAAI,MAAM,OAAO,IACf,OAAO;CAGT,KAAK,MAAM,QAAQ,MAAM,OAAO;EAC9B,IAAI,OAAO,SAAS,UAAU;EAC9B,IAAI,KAAK,OAAO,IACd,OAAO;OACF,IAAI,YAAY,IAAI,GAAG;GAC5B,MAAM,UAAU,OAAO,IAAI,IAAI;GAC/B,IAAI,SACF,OAAO;EAEX;CACF;CAEA,OAAO;AACT;;;;;AAMA,MAAa,eAAe,IAAY,UAAyC;CAC/E,IAAI,MAAM,OAAO,IAAI,OAAO,CAAC;CAE7B,MAAM,MAAM,MAAM,MAAM,WAAU,MAAK,EAAE,OAAO,MAAM,aAAa,EAAE,OAAO,EAAE;CAE9E,IAAI,OAAO,GACT,OAAO,CAAC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