{"version":3,"file":"defaultRuleGroupProcessorJSONata.mjs","names":[],"sources":["../../../src/utils/formatQuery/defaultRuleGroupProcessorJSONata.ts"],"sourcesContent":["import type { RuleGroupProcessor, RuleGroupTypeAny } from '../../types';\nimport { isRuleGroup, isRuleGroupType } from '../isRuleGroup';\nimport { isRuleOrGroupValid } from '../isRuleOrGroupValid';\nimport { getOption } from '../optGroupUtils';\n\n/**\n * Rule group processor used by {@link formatQuery} for \"jsonata\" format.\n *\n * @group Export\n */\nexport const defaultRuleGroupProcessorJSONata: RuleGroupProcessor<string> = (\n  ruleGroup,\n  options\n) => {\n  const {\n    fields,\n    fallbackExpression,\n    getParseNumberBoolean,\n    placeholderFieldName,\n    placeholderOperatorName,\n    placeholderValueName,\n    ruleProcessor,\n    validateRule,\n    validationMap,\n  } = options;\n\n  const processRuleGroup = (rg: RuleGroupTypeAny, outermost?: boolean): string => {\n    if (\n      !isRuleOrGroupValid(\n        rg,\n        validationMap[\n          rg.id ?? /* v8 ignore start -- @preserve */ '' /* v8 ignore stop -- @preserve */\n        ]\n      )\n    ) {\n      return outermost ? fallbackExpression : '';\n    }\n\n    const processedRules = [];\n    let precedingCombinator = '';\n    let firstRule = true;\n\n    for (const rule of rg.rules) {\n      // Independent combinators\n      if (typeof rule === 'string') {\n        precedingCombinator = rule;\n        continue;\n      }\n\n      // Groups\n      if (isRuleGroup(rule)) {\n        const processedGroup = processRuleGroup(rule);\n        if (processedGroup) {\n          if (!firstRule && precedingCombinator) {\n            processedRules.push(precedingCombinator);\n            precedingCombinator = '';\n          }\n          firstRule = false;\n          processedRules.push(processedGroup);\n        }\n        continue;\n      }\n\n      // Rules\n      const [validationResult, fieldValidator] = validateRule(rule);\n      if (\n        !isRuleOrGroupValid(rule, validationResult, fieldValidator) ||\n        rule.field === placeholderFieldName ||\n        rule.operator === placeholderOperatorName ||\n        /* v8 ignore next -- @preserve */\n        (placeholderValueName !== undefined && rule.value === placeholderValueName)\n      ) {\n        continue;\n      }\n\n      const fieldData = getOption(fields, rule.field);\n      const processedRule = ruleProcessor(rule, {\n        ...options,\n        parseNumbers: getParseNumberBoolean(fieldData?.inputType),\n        escapeQuotes: (rule.valueSource ?? 'value') === 'value',\n        fieldData,\n      });\n\n      if (processedRule) {\n        if (!firstRule && precedingCombinator) {\n          processedRules.push(precedingCombinator);\n          precedingCombinator = '';\n        }\n        firstRule = false;\n        processedRules.push(processedRule);\n      }\n    }\n\n    const expression = processedRules.join(isRuleGroupType(rg) ? ` ${rg.combinator} ` : ' ');\n\n    const [prefix, suffix] = rg.not || !outermost ? [`${rg.not ? '$not' : ''}(`, ')'] : ['', ''];\n\n    return expression ? `${prefix}${expression}${suffix}` : fallbackExpression;\n  };\n\n  return processRuleGroup(ruleGroup, true);\n};\n"],"mappings":";;;;;;;;;AAUA,MAAa,oCACX,WACA,YACG;CACH,MAAM,EACJ,QACA,oBACA,uBACA,sBACA,yBACA,sBACA,eACA,cACA,kBACE;CAEJ,MAAM,oBAAoB,IAAsB,cAAgC;EAC9E,IACE,CAAC,mBACC,IACA,cACE,GAAG,yCAAyC,GAEhD,GAEA,OAAO,YAAY,qBAAqB;EAG1C,MAAM,iBAAiB,CAAC;EACxB,IAAI,sBAAsB;EAC1B,IAAI,YAAY;EAEhB,KAAK,MAAM,QAAQ,GAAG,OAAO;GAE3B,IAAI,OAAO,SAAS,UAAU;IAC5B,sBAAsB;IACtB;GACF;GAGA,IAAI,YAAY,IAAI,GAAG;IACrB,MAAM,iBAAiB,iBAAiB,IAAI;IAC5C,IAAI,gBAAgB;KAClB,IAAI,CAAC,aAAa,qBAAqB;MACrC,eAAe,KAAK,mBAAmB;MACvC,sBAAsB;KACxB;KACA,YAAY;KACZ,eAAe,KAAK,cAAc;IACpC;IACA;GACF;GAGA,MAAM,CAAC,kBAAkB,kBAAkB,aAAa,IAAI;GAC5D,IACE,CAAC,mBAAmB,MAAM,kBAAkB,cAAc,KAC1D,KAAK,UAAU,wBACf,KAAK,aAAa,2BAEjB,yBAAyB,KAA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