{"version":3,"file":"convertQuery-BeJJH9BI.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;CAAM;AAE7C,MAAM,gBAAgB,GAAY,MAAc,GAAG,EAAE,KAAK;AAE1D,MAAM,gDACJ,IACA,sBAA8B,MACV;CACpB,MAAM,iBAAiB,iBAAiB;AAGxC,KAAI,CAAC,GAAG,MAAM,SAAS,eAAsB,CAG3C,QAAO,sBAAsB,iBAAiB,SAAS,IACnD,6CAA6C,IAAI,sBAAsB,EAAE,GACzE;CAGN,MAAM,WAAW,CAAC,GAAG,GAAG,MAAM;CAC9B,IAAI,SAAS;AAGb,QAAO,SAAS,SAAS,SAAS,GAAG;AACnC,MAAI,aAAa,SAAS,SAAS,IAAI,eAAe,EAAE;AACtD,aAAU;AACV;;EAGF,IAAI,0BAA0B;AAC9B,OAAK,IAAI,IAAI,SAAS,GAAG,IAAI,SAAS,QAAQ,IAC5C,KAAI,OAAO,SAAS,OAAO,YAAY,GAAG,SAAS,GAAG,KAAK,gBAAgB;AACzE,6BAA0B;AAC1B;;AAIJ,MAAI,4BAA4B,IAAI;AAElC,YAAS,OACP,QACA,SAAS,QACT,6CAEE,EAAE,OAAO,SAAS,MAAM,OAAO,EAAS,EACxC,sBAAsB,EACvB,CACF;AACD;QAGA,UAAS,OACP,QACA,0BAA0B,QAC1B,6CAEE,EAAE,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