{"version":3,"file":"defaultRuleGroupProcessorGremlin.mjs","names":[],"sources":["../../../src/utils/formatQuery/defaultRuleGroupProcessorGremlin.ts"],"sourcesContent":["import type { RuleGroupProcessor, RuleGroupTypeAny, RuleGroupTypeIC } from '../../types';\nimport { convertFromIC } from '../convertQuery';\nimport { isRuleGroup, isRuleGroupTypeIC } from '../isRuleGroup';\nimport { isRuleOrGroupValid } from '../isRuleOrGroupValid';\nimport { lc } from '../misc';\nimport { getOption } from '../optGroupUtils';\n\n/**\n * Rule group processor used by {@link formatQuery} for \"gremlin\" format.\n *\n * At the top level, filter rules produce chained `.has()` steps (implicit AND) unless the group\n * is negated, uses \"or\", or uses independent combinators (then nested form is used).\n * Nested groups use `.and()` / `.or()` / `.not()` compound traversals with\n * `__` anonymous traversal prefixes. Since `not()` accepts exactly one\n * traversal, negated groups with multiple predicates wrap them in a single\n * `__.and()`/`__.or()` sub-traversal.\n *\n * @group Export\n */\nexport const defaultRuleGroupProcessorGremlin: 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 validateAndProcess = (rule: (typeof ruleGroup.rules)[number]) => {\n    // v8 ignore next -- @preserve\n    if (typeof rule === 'string' || isRuleGroup(rule)) return undefined;\n\n    const [validationResult, fieldValidator] = validateRule(rule);\n    if (\n      !isRuleOrGroupValid(rule, validationResult, fieldValidator) ||\n      /* v8 ignore next 2 -- @preserve */\n      rule.field === placeholderFieldName ||\n      rule.operator === placeholderOperatorName ||\n      /* v8 ignore next -- @preserve */\n      (placeholderValueName !== undefined && rule.value === placeholderValueName)\n    ) {\n      return undefined;\n    }\n\n    const fieldData = getOption(fields, rule.field);\n    return ruleProcessor(rule, {\n      ...options,\n      parseNumbers: getParseNumberBoolean(fieldData?.inputType),\n      escapeQuotes: (rule.valueSource ?? 'value') === 'value',\n      fieldData,\n    });\n  };\n\n  /** Drops invalid/empty IC entries before grouping; keeps combinator preceding each kept entry. */\n  const dropInvalidIC = (rg: RuleGroupTypeIC): RuleGroupTypeIC => {\n    const rules: RuleGroupTypeIC['rules'][number][] = [];\n    let precedingCombinator = '';\n    for (const r of rg.rules) {\n      if (typeof r === 'string') {\n        precedingCombinator = r;\n        continue;\n      }\n      if (isRuleGroup(r) ? !processNested(r) : !validateAndProcess(r)) continue;\n      if (rules.length > 0) rules.push(precedingCombinator);\n      rules.push(r);\n    }\n    return { ...rg, rules: rules as RuleGroupTypeIC['rules'] };\n  };\n\n  /** Recursively processes a nested group into `.and()`/`.or()`/`.not()` form. */\n  const processNested = (rgAny: RuleGroupTypeAny): string => {\n    const rg = convertFromIC(isRuleGroupTypeIC(rgAny) ? dropInvalidIC(rgAny) : rgAny);\n    if (\n      !isRuleOrGroupValid(\n        rg,\n        validationMap[\n          rg.id ?? /* v8 ignore start -- @preserve */ '' /* v8 ignore stop -- @preserve */\n        ]\n      )\n    ) {\n      return '';\n    }\n\n    const predicates: string[] = [];\n\n    for (const rule of rg.rules) {\n      if (typeof rule === 'string') continue;\n\n      if (isRuleGroup(rule)) {\n        const nested = processNested(rule);\n        if (nested) predicates.push(nested);\n        continue;\n      }\n\n      const processed = validateAndProcess(rule);\n      if (processed) predicates.push(processed);\n    }\n\n    if (predicates.length === 0) return '';\n\n    const combinator = lc(rg.combinator);\n\n    if (predicates.length === 1 && !rg.not) return predicates[0];\n\n    // Wrap each step with `__` anonymous traversal prefix when it starts with `.`\n    const args = predicates.map(p => (p.startsWith('.') ? `__${p}` : p)).join(', ');\n\n    // `not()` accepts exactly one traversal, so multiple predicates must be\n    // combined into a single `__.and()`/`__.or()` sub-traversal first.\n    if (rg.not) {\n      return predicates.length === 1 ? `.not(${args})` : `.not(__.${combinator}(${args}))`;\n    }\n\n    return `.${combinator}(${args})`;\n  };\n\n  // Top level: chain steps directly (implicit AND for outermost group)\n  if (\n    !isRuleOrGroupValid(\n      ruleGroup,\n      validationMap[\n        ruleGroup.id ?? /* v8 ignore start -- @preserve */ '' /* v8 ignore stop -- @preserve */\n      ]\n    )\n  ) {\n    return fallbackExpression;\n  }\n\n  if (ruleGroup.not || isRuleGroupTypeIC(ruleGroup) || lc(ruleGroup.combinator) !== 'and') {\n    return processNested(ruleGroup) || fallbackExpression;\n  }\n\n  const steps: string[] = [];\n\n  for (const rule of ruleGroup.rules) {\n    if (typeof rule === 'string') continue;\n\n    if (isRuleGroup(rule)) {\n      const compound = processNested(rule);\n      if (compound) steps.push(compound);\n      continue;\n    }\n\n    const processed = validateAndProcess(rule);\n    if (processed) steps.push(processed);\n  }\n\n  if (steps.length === 0) return fallbackExpression;\n\n  return steps.join('');\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmBA,MAAa,oCACX,WACA,YACG;CACH,MAAM,EACJ,QACA,oBACA,uBACA,sBACA,yBACA,sBACA,eACA,cACA,kBACE;CAEJ,MAAM,sBAAsB,SAA2C;;EAErE,IAAI,OAAO,SAAS,YAAY,YAAY,IAAI,GAAG,OAAO,KAAA;EAE1D,MAAM,CAAC,kBAAkB,kBAAkB,aAAa,IAAI;EAC5D,IACE,CAAC,mBAAmB,MAAM,kBAAkB,cAAc;;EAE1D,KAAK,UAAU,wBACf,KAAK,aAAa,2BAEjB,yBAAyB,KAAA,KAAa,KAAK,UAAU,sBAEtD;EAGF,MAAM,YAAY,UAAU,QAAQ,KAAK,KAAK;EAC9C,OAAO,cAAc,MAAM;GACzB,GAAG;GACH,cAAc,sBAAsB,WAAW,SAAS;GACxD,eAAe,KAAK,eAAe,aAAa;GAChD;EACF,CAAC;CACH;;CAGA,MAAM,iBAAiB,OAAyC;EAC9D,MAAM,QAA4C,CAAC;EACnD,IAAI,sBAAsB;EAC1B,KAAK,MAAM,KAAK,GAAG,OAAO;GACxB,IAAI,OAAO,MAAM,UAAU;IACzB,sBAAsB;IACtB;GACF;GACA,IAAI,YAAY,CAAC,IAAI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