{"version":3,"file":"defaultRuleProcessorNL.mjs","names":[],"sources":["../../../src/utils/formatQuery/defaultRuleProcessorNL.ts"],"sourcesContent":["import type {\n  DefaultOperatorName,\n  ExportOperatorMap,\n  FormatQueryFinalOptions,\n  FullOption,\n  RuleProcessor,\n} from '../../types';\nimport { toArray } from '../arrayUtils';\nimport { lc } from '../misc';\nimport { getOption, toFullOptionList } from '../optGroupUtils';\nimport { defaultRuleGroupProcessorNL } from './defaultRuleGroupProcessorNL';\nimport { defaultValueProcessorNL } from './defaultValueProcessorNL';\nimport {\n  getQuotedFieldName,\n  isRegexOperator,\n  normalizeConstituentWordOrder,\n  processMatchMode,\n  sanitizeRegexFlags,\n} from './utils';\n\n/**\n * Default operator map used by {@link formatQuery} for \"natural_language\" format.\n *\n * @group Export\n */\nexport const defaultExportOperatorMap: ExportOperatorMap = {\n  '=': ['is', 'is the same as the value in'],\n  '!=': ['is not', 'is not the same as the value in'],\n  '<': ['is less than', 'is less than the value in'],\n  '>': ['is greater than', 'is greater than the value in'],\n  '<=': ['is less than or equal to', 'is less than or equal to the value in'],\n  '>=': ['is greater than or equal to', 'is greater than or equal to the value in'],\n  contains: ['contains', 'contains the value in'],\n  beginswith: ['starts with', 'starts with the value in'],\n  endswith: ['ends with', 'ends with the value in'],\n  doesnotcontain: ['does not contain', 'does not contain the value in'],\n  doesnotbeginwith: ['does not start with', 'does not start with the value in'],\n  doesnotendwith: ['does not end with', 'does not end with the value in'],\n  null: 'is null',\n  notnull: 'is not null',\n  in: ['is one of the values', 'is the same as a value in'],\n  notin: ['is not one of the values', 'is not the same as any value in'],\n  between: ['is between', 'is between the values in'],\n  notbetween: ['is not between', 'is not between the values in'],\n  matches: ['matches regular expression', 'matches the regular expression in'],\n  doesnotmatch: ['does not match regular expression', 'does not match the regular expression in'],\n};\n\n/* v8 ignore next -- @preserve */\nconst defaultGetOperators = () => [];\n\n/**\n * Default operator processor used by {@link formatQuery} for \"natural_language\" format.\n *\n * @group Export\n */\nexport const defaultOperatorProcessorNL: RuleProcessor = (\n  rule,\n  // v8 ignore next\n  opts = {}\n) => {\n  const { field, operator, valueSource = 'value' } = rule;\n  // v8 ignore next\n  const {\n    getOperators = defaultGetOperators,\n    operatorMap: operatorMapParam = defaultExportOperatorMap,\n  } = opts;\n\n  // Build the merged operator map (default + custom overrides)\n  const mergedOperatorMap = new Map<string, string | [string, string]>(\n    Object.entries(defaultExportOperatorMap)\n  );\n  for (const [key, value] of Object.entries(operatorMapParam)) {\n    mergedOperatorMap.set(lc(key), value);\n  }\n  const operatorMap = Object.fromEntries(mergedOperatorMap);\n\n  // For single-valued in/notin, use the = or != operator labels for clarity\n  const operatorLC = lc(operator);\n\n  var normalizedOperator = operator;\n\n  const hasSingleValue = toArray(rule.value).length === 1;\n\n  if (operatorLC === 'in' && hasSingleValue) {\n    normalizedOperator = '=';\n  } else if (operatorLC === 'notin' && hasSingleValue) {\n    normalizedOperator = '!=';\n  }\n\n  const { value: operatorNL, label } = getOption(\n    toFullOptionList(\n      getOperators(field, {\n        fieldData: opts.fieldData ?? {\n          name: field,\n          value: field,\n          label: field,\n        },\n      }) ?? /* v8 ignore start -- @preserve */ [] /* v8 ignore stop -- @preserve */\n    ) as FullOption[],\n    normalizedOperator\n  ) ?? {\n    name: normalizedOperator,\n    value: normalizedOperator,\n    label: normalizedOperator,\n  };\n\n  // Retrieve the natural language translation for the operator\n  const operatorTL = operatorMap[operatorNL as DefaultOperatorName] ??\n    operatorMap[lc(operatorNL) as Lowercase<DefaultOperatorName>] ?? [label, label];\n\n  // Return field-aware translation (e.g., \"is\" vs \"is the same as the value in\")\n  return typeof operatorTL === 'string' ? operatorTL : operatorTL[valueSource === 'field' ? 1 : 0];\n};\n\n/**\n * Default rule processor used by {@link formatQuery} for \"natural_language\" format.\n *\n * @group Export\n */\nexport const defaultRuleProcessorNL: RuleProcessor = (rule, opts) => {\n  const { field, operator } = rule;\n  // v8 ignore next\n  const {\n    fieldData,\n    quoteFieldNamesWith = ['', ''] as [string, string],\n    fieldIdentifierSeparator = '',\n    quoteValuesWith = `'`,\n    operatorProcessor = defaultOperatorProcessorNL,\n    valueProcessor = defaultValueProcessorNL,\n    concatOperator = '||',\n    wordOrder = 'SVO',\n  } = opts ?? /* v8 ignore start -- @preserve */ {}; /* v8 ignore stop -- @preserve */\n\n  const processedField = getQuotedFieldName(\n    fieldData?.label ?? field,\n    {\n      quoteFieldNamesWith,\n      fieldIdentifierSeparator,\n    },\n    false\n  );\n\n  const matchEval = processMatchMode(rule);\n\n  if (matchEval === false) {\n    return '';\n  } else if (matchEval) {\n    const { mode, threshold } = matchEval;\n\n    const nestedArrayFilter = defaultRuleGroupProcessorNL(rule.value, {\n      ...(opts as FormatQueryFinalOptions),\n      fields: toFullOptionList(fieldData?.subproperties ?? []),\n      // Post-process once, at top level only\n      nlPostProcessor: undefined,\n    });\n\n    // (H)as (S)ub(P)roperties\n    const hsp = (fieldData?.subproperties?.length ?? 0) > 0;\n\n    switch (mode) {\n      case 'all':\n        return `(${hsp ? 'for ' : ''}every item in ${processedField}${hsp ? ',' : ''} ${nestedArrayFilter})`;\n\n      case 'none':\n        return `(${hsp ? 'for ' : ''}no item in ${processedField}${hsp ? ',' : ''} ${nestedArrayFilter})`;\n\n      case 'some':\n        return `(${hsp ? 'for ' : ''}at least one item in ${processedField}${hsp ? ',' : ''} ${nestedArrayFilter})`;\n\n      case 'atleast':\n      case 'atmost':\n      case 'exactly': {\n        const mm = mode.replace('at', 'at ');\n        if (threshold > 0 && threshold < 1) {\n          return `(${hsp ? 'for ' : ''}${mm} ${threshold * 100}% of the items in ${processedField}${hsp ? ',' : ''} ${nestedArrayFilter})`;\n        }\n        return `(${hsp ? 'for ' : ''}${mm} ${threshold} of the items in ${processedField}${hsp ? ',' : ''} ${nestedArrayFilter})`;\n      }\n    }\n  }\n\n  const value = valueProcessor(rule, {\n    ...opts,\n    quoteFieldNamesWith,\n    fieldIdentifierSeparator,\n    quoteValuesWith,\n    concatOperator,\n  });\n\n  const operatorLC = lc(operator);\n  if (\n    (operatorLC === 'in' ||\n      operatorLC === 'notin' ||\n      operatorLC === 'between' ||\n      operatorLC === 'notbetween') &&\n    !value\n  ) {\n    return '';\n  }\n\n  const processedOperator = operatorProcessor(rule, opts);\n\n  const flags = isRegexOperator(operator) ? sanitizeRegexFlags(rule.regexFlags, 'imsu') : '';\n\n  const wordOrderMap = {\n    S: processedField,\n    V: processedOperator,\n    O: flags ? `${value} with flags ${quoteValuesWith}${flags}${quoteValuesWith}` : value,\n  };\n\n  const translations = opts?.translations ?? {};\n  const suffixMap: Record<string, string> = {\n    S: translations.afterSubject ?? ' ',\n    V: translations.afterVerb ?? ' ',\n    O: translations.afterObject ?? ' ',\n  };\n\n  const wordOrderTuple = normalizeConstituentWordOrder(wordOrder).filter(\n    term => wordOrderMap[term] !== ''\n  );\n  return wordOrderTuple\n    .map(\n      (term, i) => `${wordOrderMap[term]}${i < wordOrderTuple.length - 1 ? suffixMap[term] : ''}`\n    )\n    .join('')\n    .trim();\n};\n"],"mappings":";;;;;;;;;;;;AAyBA,MAAa,2BAA8C;CACzD,KAAK,CAAC,MAAM,6BAA6B;CACzC,MAAM,CAAC,UAAU,iCAAiC;CAClD,KAAK,CAAC,gBAAgB,2BAA2B;CACjD,KAAK,CAAC,mBAAmB,8BAA8B;CACvD,MAAM,CAAC,4BAA4B,uCAAuC;CAC1E,MAAM,CAAC,+BAA+B,0CAA0C;CAChF,UAAU,CAAC,YAAY,uBAAuB;CAC9C,YAAY,CAAC,eAAe,0BAA0B;CACtD,UAAU,CAAC,aAAa,wBAAwB;CAChD,gBAAgB,CAAC,oBAAoB,+BAA+B;CACpE,kBAAkB,CAAC,uBAAuB,kCAAkC;CAC5E,gBAAgB,CAAC,qBAAqB,gCAAgC;CACtE,MAAM;CACN,SAAS;CACT,IAAI,CAAC,wBAAwB,2BAA2B;CACxD,OAAO,CAAC,4BAA4B,iCAAiC;CACrE,SAAS,CAAC,cAAc,0BAA0B;CAClD,YAAY,CAAC,kBAA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