{"version":3,"file":"deriveClassNames.mjs","names":[],"sources":["../../src/utils/deriveClassNames.ts"],"sourcesContent":["import type { ClassValue } from 'clsx';\nimport { standardClassnames } from '../defaults';\nimport type { Classname, Classnames, ValidationMap } from '../types';\nimport { clsx } from './clsx';\n\n/** Classname keys that have both a standard class and a `controlClassnames` entry. */\ntype StandardClassnameKey = keyof typeof standardClassnames & keyof Classnames;\n\n/**\n * A classname applied only when `when` returns true for the current state.\n *\n * `key` is used to look up both the standard classname and the corresponding `controlClassnames`\n * entry. Set `standardOnly` when the standard class is conditional but no custom class\n * corresponds to it.\n */\nexport interface ClassnameCondition<S> {\n  key: StandardClassnameKey;\n  when: (state: S) => boolean | undefined;\n  standardOnly?: boolean;\n}\n\n/**\n * How one derived classname is composed: the `controlClassnames` keys that contribute to it (in\n * application order, after the standard classname), plus any state-dependent classes.\n */\nexport interface ClassnameSpec<S> {\n  sources: readonly (keyof Classnames)[];\n  conditions?: readonly ClassnameCondition<S>[];\n}\n\ntype ClassnameSpecMap<K extends string, S> = Record<\n  K,\n  readonly (keyof Classnames)[] | ClassnameSpec<S>\n>;\n\n/** Keys of the `classNames` object returned for a rule. */\nexport type RuleClassnameKey =\n  | 'shiftActions'\n  | 'dragHandle'\n  | 'fields'\n  | 'matchMode'\n  | 'matchThreshold'\n  | 'operators'\n  | 'valueSource'\n  | 'value'\n  | 'cloneRule'\n  | 'lockRule'\n  | 'muteRule'\n  | 'removeRule'\n  | 'valueListItem';\n\n/** Keys of the `classNames` object returned for a rule group. */\nexport type RuleGroupClassnameKey =\n  | 'header'\n  | 'shiftActions'\n  | 'undoRedoActions'\n  | 'undoAction'\n  | 'redoAction'\n  | 'dragHandle'\n  | 'combinators'\n  | 'notToggle'\n  | 'addRule'\n  | 'addGroup'\n  | 'cloneGroup'\n  | 'ungroup'\n  | 'lockGroup'\n  | 'muteGroup'\n  | 'removeGroup'\n  | 'body';\n\n/** Drag-and-drop state that can contribute conditional classnames. */\nexport interface DndClassNameState {\n  isDragging?: boolean;\n  isOver?: boolean;\n  dropEffect?: 'move' | 'copy';\n  groupItems?: boolean;\n  dropNotAllowed?: boolean;\n}\n\n/** State that contributes conditional classnames to a rule's wrapper element. */\nexport interface RuleClassNameState extends DndClassNameState {\n  disabled?: boolean;\n  muted?: boolean;\n  hasSubQuery?: boolean;\n}\n\nconst ruleClassnameSources: ClassnameSpecMap<RuleClassnameKey, RuleClassNameState> = {\n  shiftActions: ['shiftActions'],\n  dragHandle: ['dragHandle'],\n  fields: ['valueSelector', 'fields'],\n  matchMode: ['valueSelector', 'matchMode'],\n  matchThreshold: ['valueSelector', 'matchThreshold'],\n  operators: ['valueSelector', 'operators'],\n  valueSource: ['valueSelector', 'valueSource'],\n  value: ['value'],\n  cloneRule: ['actionElement', 'cloneRule'],\n  lockRule: ['actionElement', 'lockRule'],\n  muteRule: ['actionElement', 'muteRule'],\n  removeRule: ['actionElement', 'removeRule'],\n  valueListItem: ['valueListItem'],\n};\n\nconst ruleGroupClassnameSources: ClassnameSpecMap<RuleGroupClassnameKey, DndClassNameState> = {\n  header: {\n    sources: ['header'],\n    conditions: [\n      // The standard `dndOver` class has no custom counterpart on the header element.\n      { key: 'dndOver', when: s => s.isOver, standardOnly: true },\n      { key: 'dndCopy', when: s => s.isOver && s.dropEffect === 'copy' },\n      { key: 'dndDropNotAllowed', when: s => s.dropNotAllowed },\n    ],\n  },\n  shiftActions: ['shiftActions'],\n  undoRedoActions: ['undoRedoActions'],\n  undoAction: ['actionElement', 'undoAction'],\n  redoAction: ['actionElement', 'redoAction'],\n  dragHandle: ['dragHandle'],\n  combinators: ['valueSelector', 'combinators'],\n  notToggle: ['notToggle'],\n  addRule: ['actionElement', 'addRule'],\n  addGroup: ['actionElement', 'addGroup'],\n  cloneGroup: ['actionElement', 'cloneGroup'],\n  ungroup: ['actionElement', 'ungroup'],\n  lockGroup: ['actionElement', 'lockGroup'],\n  muteGroup: ['actionElement', 'muteGroup'],\n  removeGroup: ['actionElement', 'removeGroup'],\n  body: ['body'],\n};\n\n/**\n * Conditional classes applied to a rule's wrapper element.\n *\n * Note that this is deliberately _not_ the same set as {@link ruleGroupOuterConditions}: a rule\n * reflects more drag-and-drop states than a group does. Declaring each set separately is what\n * keeps an implementation from assuming they're symmetric.\n */\nconst ruleOuterConditions: readonly ClassnameCondition<RuleClassNameState>[] = [\n  { key: 'disabled', when: s => s.disabled },\n  { key: 'muted', when: s => s.muted },\n  { key: 'dndDragging', when: s => s.isDragging },\n  { key: 'dndOver', when: s => s.isOver },\n  { key: 'dndCopy', when: s => s.isOver && s.dropEffect === 'copy' },\n  { key: 'dndGroup', when: s => s.isOver && s.groupItems },\n  { key: 'dndDropNotAllowed', when: s => s.dropNotAllowed },\n  { key: 'hasSubQuery', when: s => s.hasSubQuery },\n];\n\n/** Conditional classes applied to a rule group's wrapper element. */\nconst ruleGroupOuterConditions: readonly ClassnameCondition<RuleClassNameState>[] = [\n  { key: 'disabled', when: s => s.disabled },\n  { key: 'muted', when: s => s.muted },\n  { key: 'dndDragging', when: s => s.isDragging },\n  { key: 'dndGroup', when: s => s.isOver && s.groupItems },\n];\n\n/** State that contributes conditional classnames to the query builder's wrapper element. */\nexport interface QueryBuilderClassNameState {\n  disabled?: boolean;\n  validationResult?: boolean | ValidationMap;\n}\n\n/**\n * Conditional classes applied to the query builder's wrapper element.\n *\n * Only a `boolean` validation result contributes a class; a {@link ValidationMap} describes\n * individual rules and groups, not the query as a whole.\n */\nconst queryBuilderOuterConditions: readonly ClassnameCondition<QueryBuilderClassNameState>[] = [\n  { key: 'disabled', when: s => s.disabled },\n  { key: 'valid', when: s => typeof s.validationResult === 'boolean' && s.validationResult },\n  { key: 'invalid', when: s => typeof s.validationResult === 'boolean' && !s.validationResult },\n];\n\n/** Options common to every classname derivation. */\nexport interface DeriveClassNamesOptions {\n  /** The merged `controlClassnames` for the query builder. */\n  classNames: Partial<Classnames> | undefined;\n  /** When `true`, standard (`rule`, `ruleGroup-*`, etc.) classnames are omitted. */\n  suppressStandardClassnames?: boolean;\n}\n\n/**\n * Expands conditions into `clsx` arguments: every custom class first (in declaration order),\n * then a single object of standard classes.\n */\nconst conditionArgs = <S>(\n  conditions: readonly ClassnameCondition<S>[],\n  state: S,\n  { classNames, suppressStandardClassnames }: DeriveClassNamesOptions\n): ClassValue[] => {\n  const evaluated = conditions.map(condition => [condition, !!condition.when(state)] as const);\n\n  return [\n    ...evaluated\n      .filter(([condition]) => !condition.standardOnly)\n      .map(([condition, active]) => active && classNames?.[condition.key]),\n    suppressStandardClassnames ||\n      Object.fromEntries(\n        evaluated.map(([condition, active]) => [standardClassnames[condition.key], active])\n      ),\n  ];\n};\n\n/** Composes the classname for a single entry of a spec map. */\nconst deriveFromSpec = <S>(\n  key: string,\n  spec: readonly (keyof Classnames)[] | ClassnameSpec<S>,\n  state: S,\n  options: DeriveClassNamesOptions\n): string => {\n  const { sources, conditions }: ClassnameSpec<S> = Array.isArray(spec)\n    ? { sources: spec }\n    : (spec as ClassnameSpec<S>);\n\n  // `classNames` is optional chained because React Native passes a partial schema.\n  return clsx(\n    options.suppressStandardClassnames ||\n      standardClassnames[key as keyof typeof standardClassnames],\n    ...sources.map(source => options.classNames?.[source]),\n    ...(conditions ? conditionArgs(conditions, state, options) : [])\n  );\n};\n\nconst deriveFromSpecs = <K extends string, S>(\n  specs: ClassnameSpecMap<K, S>,\n  state: S,\n  options: DeriveClassNamesOptions\n): Record<K, string> => {\n  const result = {} as Record<K, string>;\n\n  for (const [key, spec] of Object.entries(specs) as [\n    K,\n    readonly (keyof Classnames)[] | ClassnameSpec<S>,\n  ][]) {\n    result[key] = deriveFromSpec(key, spec, state, options);\n  }\n\n  return result;\n};\n\n/**\n * Classnames for each element rendered by a rule. This is the framework-agnostic core of the\n * `classNames` object returned by the `useRule` hook.\n *\n * @group Query Tools\n */\nexport const deriveRuleClassNames = (\n  options: DeriveClassNamesOptions\n): Record<RuleClassnameKey, string> => deriveFromSpecs(ruleClassnameSources, {}, options);\n\n/**\n * The classname for a single element of a rule, composed from the same table as\n * {@link deriveRuleClassNames}. Useful where only one is needed, such as the items of a\n * multi-value editor.\n *\n * @group Query Tools\n */\nexport const deriveRuleClassName = (\n  key: RuleClassnameKey,\n  options: DeriveClassNamesOptions\n): string => deriveFromSpec(key, ruleClassnameSources[key], {}, options);\n\n/**\n * Classnames for each element rendered by a rule group, including its conditionally-classed\n * `header`. This is the framework-agnostic core of the `classNames` object returned by the\n * `useRuleGroup` hook.\n *\n * @group Query Tools\n */\nexport const deriveRuleGroupClassNames = (\n  options: DeriveClassNamesOptions & DndClassNameState\n): Record<RuleGroupClassnameKey, string> =>\n  deriveFromSpecs(ruleGroupClassnameSources, options, options);\n\n/** Inputs common to every outer (wrapper) classname derivation. */\nexport interface OuterClassNameOptionsBase extends DeriveClassNamesOptions {\n  /**\n   * Classnames contributed by the rule or group itself and its configuration, applied first.\n   * For a rule that is the rule/field/operator classnames; for a group, the group and combinator\n   * classnames.\n   */\n  leadingClassNames?: (Classname | null)[];\n  /** Appended last, as produced by `getValidationClassNames`. */\n  validationClassName?: Classname;\n}\n\n/** Inputs to {@link deriveRuleOuterClassName} and {@link deriveRuleGroupOuterClassName}. */\nexport interface OuterClassNameOptions extends OuterClassNameOptionsBase, RuleClassNameState {}\n\nconst deriveOuterClassName = <S extends OuterClassNameOptionsBase>(\n  standardKey: 'rule' | 'ruleGroup' | 'queryBuilder',\n  conditions: readonly ClassnameCondition<S>[],\n  options: S\n): string => {\n  const {\n    classNames,\n    suppressStandardClassnames,\n    leadingClassNames = [],\n    validationClassName,\n  } = options;\n\n  return clsx(\n    ...leadingClassNames,\n    suppressStandardClassnames || standardClassnames[standardKey],\n    classNames?.[standardKey],\n    ...conditionArgs(conditions, options, options),\n    validationClassName\n  );\n};\n\n/**\n * The outer (wrapper) classname for a rule, including every conditional state class.\n *\n * @group Query Tools\n */\nexport const deriveRuleOuterClassName = (options: OuterClassNameOptions): string =>\n  deriveOuterClassName('rule', ruleOuterConditions, options);\n\n/**\n * The outer (wrapper) classname for a rule group, including every conditional state class.\n *\n * A group reflects fewer drag-and-drop states than a rule—`dndOver`, `dndCopy`,\n * `dndDropNotAllowed`, and `hasSubQuery` do not apply—so this is not interchangeable with\n * {@link deriveRuleOuterClassName}.\n *\n * @group Query Tools\n */\nexport const deriveRuleGroupOuterClassName = (options: OuterClassNameOptions): string =>\n  deriveOuterClassName('ruleGroup', ruleGroupOuterConditions, options);\n\n/**\n * The outer (wrapper) classname for the query builder itself, including the conditional\n * `disabled`, `valid`, and `invalid` state classes.\n *\n * @group Query Tools\n */\nexport const deriveQueryBuilderClassNames = (\n  options: OuterClassNameOptionsBase & QueryBuilderClassNameState\n): string => deriveOuterClassName('queryBuilder', queryBuilderOuterConditions, options);\n"],"mappings":";;;AAsFA,MAAM,uBAA+E;CACnF,cAAc,CAAC,cAAc;CAC7B,YAAY,CAAC,YAAY;CACzB,QAAQ,CAAC,iBAAiB,QAAQ;CAClC,WAAW,CAAC,iBAAiB,WAAW;CACxC,gBAAgB,CAAC,iBAAiB,gBAAgB;CAClD,WAAW,CAAC,iBAAiB,WAAW;CACxC,aAAa,CAAC,iBAAiB,aAAa;CAC5C,OAAO,CAAC,OAAO;CACf,WAAW,CAAC,iBAAiB,WAAW;CACxC,UAAU,CAAC,iBAAiB,UAAU;CACtC,UAAU,CAAC,iBAAiB,UAAU;CACtC,YAAY,CAAC,iBAAiB,YAAY;CAC1C,eAAe,CAAC,eAAe;AACjC;AAEA,MAAM,4BAAwF;CAC5F,QAAQ;EACN,SAAS,CAAC,QAAQ;EAClB,YAAY;GAEV;IAAE,KAAK;IAAW,OAAM,MAAK,EAAE;IAAQ,cAAc;GAAK;GAC1D;IAAE,KAAK;IAAW,OAAM,MAAK,EAAE,UAAU,EAAE,eAAe;GAAO;GACjE;IAAE,KAAK;IAAqB,OAAM,MAAK,EAAE;GAAe;EAC1D;CACF;CACA,cAAc,CAAC,cAAc;CAC7B,iBAAiB,CAAC,iBAAiB;CACnC,YAAY,CAAC,iBAAiB,YAAY;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