{"version":3,"file":"convertQuery-DaXv3h7S.mjs","names":[],"sources":["../src/utils/convertQuery.ts"],"sourcesContent":["import type {\n  RuleGroupArray,\n  RuleGroupICArray,\n  RuleGroupType,\n  RuleGroupTypeAny,\n  RuleGroupTypeIC,\n  RuleType,\n  ToRuleGroupType,\n  ToRuleGroupTypeIC,\n} from '../types';\nimport { isRuleGroup, isRuleGroupType, isRuleGroupTypeIC } from './isRuleGroup';\nimport { lc } from './misc';\n\nconst combinatorLevels = ['or', 'xor', 'and'] as const;\n\nconst isSameString = (a: unknown, b: string) => lc(a) === b;\n\nconst generateRuleGroupICWithConsistentCombinators = (\n  rg: RuleGroupTypeIC,\n  baseCombinatorLevel: number = 0\n): RuleGroupTypeIC => {\n  const baseCombinator = combinatorLevels[baseCombinatorLevel];\n\n  // oxlint-disable-next-line typescript/no-explicit-any\n  if (!rg.rules.includes(baseCombinator as any)) {\n    // No instances of this combinator, so group based on the next\n    // combinator level if at least two levels remain\n    return baseCombinatorLevel < combinatorLevels.length - 2\n      ? generateRuleGroupICWithConsistentCombinators(rg, baseCombinatorLevel + 1)\n      : rg;\n  }\n\n  const newRules = [...rg.rules] as RuleGroupICArray;\n  let cursor = 0;\n\n  // Group all chains of combinators in the rule array that are not the base combinator\n  while (cursor < newRules.length - 2) {\n    if (isSameString(newRules[cursor + 1], baseCombinator)) {\n      cursor += 2;\n      continue;\n    }\n\n    let nextBaseCombinatorIndex = -1;\n    for (let i = cursor + 2; i < newRules.length; i++) {\n      if (typeof newRules[i] === 'string' && lc(newRules[i]) === baseCombinator) {\n        nextBaseCombinatorIndex = i;\n        break;\n      }\n    }\n\n    if (nextBaseCombinatorIndex === -1) {\n      // No more instances of this combinator, so group all remaining rules and exit the loop\n      newRules.splice(\n        cursor,\n        newRules.length,\n        generateRuleGroupICWithConsistentCombinators(\n          // oxlint-disable-next-line typescript/no-explicit-any\n          { rules: newRules.slice(cursor) as any },\n          baseCombinatorLevel + 1\n        )\n      );\n      break;\n    } else {\n      // Group all rules between the current cursor and the next instance of the base combinator\n      newRules.splice(\n        cursor,\n        nextBaseCombinatorIndex - cursor,\n        generateRuleGroupICWithConsistentCombinators(\n          // oxlint-disable-next-line typescript/no-explicit-any\n          { rules: newRules.slice(cursor, nextBaseCombinatorIndex) as any },\n          baseCombinatorLevel + 1\n        )\n      );\n    }\n  }\n\n  return { ...rg, rules: newRules };\n};\n\n/**\n * Converts a {@link RuleGroupTypeIC} to {@link RuleGroupType}.\n *\n * This function is idempotent: {@link RuleGroupType} queries will be\n * returned as-is.\n *\n * @group Query Tools\n */\nexport const convertFromIC = <RG extends RuleGroupTypeAny>(rg: RG): ToRuleGroupType<RG> => {\n  if (isRuleGroupType(rg)) {\n    return rg as ToRuleGroupType<RG>;\n  }\n  const processedRG = generateRuleGroupICWithConsistentCombinators(rg);\n  const rules: RuleGroupArray = [];\n  let combinator = 'and';\n  for (const [idx, r] of processedRG.rules.entries()) {\n    if (typeof r === 'string') {\n      if (idx === 1) combinator = r;\n    } else {\n      rules.push(isRuleGroup(r) ? convertFromIC(r) : r);\n    }\n  }\n  return { ...processedRG, combinator, rules } as ToRuleGroupType<RG>;\n};\n\n/**\n * Converts a {@link RuleGroupType} to {@link RuleGroupTypeIC}.\n *\n * This function is idempotent: {@link RuleGroupTypeIC} queries will be\n * returned as-is.\n *\n * @group Query Tools\n */\nexport const convertToIC = <RG extends RuleGroupTypeAny>(rg: RG): ToRuleGroupTypeIC<RG> => {\n  if (isRuleGroupTypeIC(rg)) {\n    return rg as ToRuleGroupTypeIC<RG>;\n  }\n  const { combinator, ...queryWithoutCombinator } = rg;\n  const rules: (RuleGroupTypeIC | RuleType | string)[] = [];\n  const { length } = rg.rules;\n  for (let idx = 0; idx < length; idx++) {\n    const r = rg.rules[idx];\n    if (isRuleGroup(r)) {\n      rules.push(convertToIC(r));\n    } else {\n      rules.push(r);\n    }\n    if (combinator && idx < length - 1) {\n      rules.push(combinator);\n    }\n  }\n  return { ...queryWithoutCombinator, rules } as ToRuleGroupTypeIC<RG>;\n};\n\n/**\n * Converts a {@link RuleGroupType} to {@link RuleGroupTypeIC}. For a more explicit\n * operation, use {@link convertToIC}.\n *\n * @group Query Tools\n */\nfunction convertQuery(query: RuleGroupType): RuleGroupTypeIC;\n/**\n * Converts a {@link RuleGroupTypeIC} to {@link RuleGroupType}. For a more explicit\n * operation, use {@link convertFromIC}.\n *\n * @group Query Tools\n */\nfunction convertQuery(query: RuleGroupTypeIC): RuleGroupType;\nfunction convertQuery(query: RuleGroupType | RuleGroupTypeIC): RuleGroupType | RuleGroupTypeIC {\n  return isRuleGroupTypeIC(query) ? convertFromIC(query) : convertToIC(query);\n}\n\nexport { convertQuery };\n"],"mappings":";;AAaA,MAAM,mBAAmB;CAAC;CAAM;CAAO;AAAK;AAE5C,MAAM,gBAAgB,GAAY,MAAc,GAAG,CAAC,MAAM;AAE1D,MAAM,gDACJ,IACA,sBAA8B,MACV;CACpB,MAAM,iBAAiB,iBAAiB;CAGxC,IAAI,CAAC,GAAG,MAAM,SAAS,cAAqB,GAG1C,OAAO,sBAAsB,iBAAiB,SAAS,IACnD,6CAA6C,IAAI,sBAAsB,CAAC,IACxE;CAGN,MAAM,WAAW,CAAC,GAAG,GAAG,KAAK;CAC7B,IAAI,SAAS;CAGb,OAAO,SAAS,SAAS,SAAS,GAAG;EACnC,IAAI,aAAa,SAAS,SAAS,IAAI,cAAc,GAAG;GACtD,UAAU;GACV;EACF;EAEA,IAAI,0BAA0B;EAC9B,KAAK,IAAI,IAAI,SAAS,GAAG,IAAI,SAAS,QAAQ,KAC5C,IAAI,OAAO,SAAS,OAAO,YAAY,GAAG,SAAS,EAAE,MAAM,gBAAgB;GACzE,0BAA0B;GAC1B;EACF;EAGF,IAAI,4BAA4B,IAAI;GAElC,SAAS,OACP,QACA,SAAS,QACT,6CAEE,EAAE,OAAO,SAAS,MAAM,MAAM,EAAS,GACvC,sBAAsB,CACxB,CACF;GACA;EACF,OAEE,SAAS,OACP,QACA,0BAA0B,QAC1B,6C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