{"version":3,"file":"prepareQueryObjects.mjs","names":[],"sources":["../../src/utils/prepareQueryObjects.ts"],"sourcesContent":["import type {\n  RuleGroupArray,\n  RuleGroupICArray,\n  RuleGroupType,\n  RuleGroupTypeAny,\n  RuleGroupTypeIC,\n  RuleType,\n} from '../types';\nimport { processMatchMode } from './formatQuery/utils';\nimport { generateID } from './generateID';\nimport { isRuleGroup } from './isRuleGroup';\n\n/**\n * Options for {@link prepareRule}/{@link prepareRuleGroup}.\n */\nexport interface PreparerOptions {\n  idGenerator?: () => string;\n}\n\n/**\n * Ensures that a rule is valid by adding an `id` property if it does not already exist.\n */\nexport const prepareRule = (\n  rule: RuleType,\n  { idGenerator = generateID }: PreparerOptions = {}\n): RuleType => {\n  const needsId = !rule.id;\n  const hasMatchMode = processMatchMode(rule);\n\n  if (!needsId && !hasMatchMode) {\n    return rule;\n  }\n\n  return {\n    ...rule,\n    ...(needsId && { id: idGenerator() }),\n    ...(hasMatchMode && { value: prepareRuleGroup(rule.value, { idGenerator }) }),\n  };\n};\n\n/**\n * Ensures that a rule group is valid by recursively adding an `id` property to the group itself\n * and all its rules and subgroups where one does not already exist.\n */\nexport const prepareRuleGroup = <RG extends RuleGroupTypeAny>(\n  queryObject: RG,\n  { idGenerator = generateID }: PreparerOptions = {}\n): RG => {\n  const needsId = !queryObject.id;\n  let rulesChanged = false;\n  const newRules: (RuleGroupTypeAny | RuleType | string)[] = [];\n\n  for (let i = 0; i < queryObject.rules.length; i++) {\n    const r = queryObject.rules[i];\n    if (typeof r === 'string') {\n      newRules.push(r);\n    } else {\n      const prepared = isRuleGroup(r)\n        ? prepareRuleGroup(r, { idGenerator })\n        : prepareRule(r, { idGenerator });\n      newRules.push(prepared);\n      if (prepared !== r) {\n        rulesChanged = true;\n      }\n    }\n  }\n\n  if (!needsId && !rulesChanged) {\n    return queryObject;\n  }\n\n  return {\n    ...queryObject,\n    ...(needsId && { id: idGenerator() }),\n    rules: newRules as RuleGroupArray | RuleGroupICArray,\n  };\n};\n\n/**\n * Ensures that a rule or group is valid. See {@link prepareRule} and {@link prepareRuleGroup}.\n */\nexport const prepareRuleOrGroup = (\n  rg: RuleGroupTypeAny | RuleType,\n  { idGenerator = generateID }: PreparerOptions = {}\n): RuleGroupType | RuleGroupTypeIC | RuleType =>\n  isRuleGroup(rg) ? prepareRuleGroup(rg, { idGenerator }) : prepareRule(rg, { idGenerator });\n\n/**\n * Resolves the query a query builder should render from the available sources, in precedence\n * order: the controlled `query`, then whatever is already in the store, then the uncontrolled\n * `defaultQuery`, then a freshly created empty group.\n *\n * The result is prepared with {@link prepareRuleGroup} unless it already has an `id`, which is\n * taken to mean it has been prepared before—most often because the caller is passing back the\n * object it received from `onQueryChange`.\n *\n * @group Query Tools\n */\nexport const resolveCandidateQuery = <RG extends RuleGroupTypeAny>(\n  sources: {\n    query?: RG;\n    storeQuery?: RG;\n    defaultQuery?: RG;\n    fallbackQuery: RG;\n  },\n  options?: { idGenerator?: () => string }\n): RG => {\n  const candidateQuery =\n    sources.query ?? sources.storeQuery ?? sources.defaultQuery ?? sources.fallbackQuery;\n\n  return candidateQuery.id ? candidateQuery : prepareRuleGroup(candidateQuery, options);\n};\n"],"mappings":";;;;;;;AAsBA,MAAa,eACX,MACA,EAAE,cAAc,eAAgC,CAAC,MACpC;CACb,MAAM,UAAU,CAAC,KAAK;CACtB,MAAM,eAAe,iBAAiB,IAAI;CAE1C,IAAI,CAAC,WAAW,CAAC,cACf,OAAO;CAGT,OAAO;EACL,GAAG;EACH,GAAI,WAAW,EAAE,IAAI,YAAY,EAAE;EACnC,GAAI,gBAAgB,EAAE,OAAO,iBAAiB,KAAK,OAAO,EAAE,YAAY,CAAC,EAAE;CAC7E;AACF;;;;;AAMA,MAAa,oBACX,aACA,EAAE,cAAc,eAAgC,CAAC,MAC1C;CACP,MAAM,UAAU,CAAC,YAAY;CAC7B,IAAI,eAAe;CACnB,MAAM,WAAqD,CAAC;CAE5D,KAAK,IAAI,IAAI,GAAG,IAAI,YAAY,MA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