{"version":3,"file":"queryTools.mjs","names":[],"sources":["../../src/utils/queryTools.ts"],"sourcesContent":["import { current, isDraft } from 'immer';\nimport { defaultCombinators } from '../defaults';\nimport type {\n  MatchModeOptions,\n  OptionList,\n  Path,\n  RuleGroupTypeAny,\n  RuleType,\n  UpdateableProperties,\n  UpdateValueMap,\n  ValueSourceFlexibleOptions,\n  ValueSources,\n} from '../types';\nimport { generateID } from './generateID';\nimport { getValueSourcesUtil } from './getValueSourcesUtil';\nimport { producerFor } from './immerInstances';\nimport { isRuleGroup, isRuleGroupType, isRuleGroupTypeIC } from './isRuleGroup';\nimport { getFirstOption, getOption } from './optGroupUtils';\nimport {\n  findID,\n  findPath,\n  getCommonAncestorPath,\n  getParentPath,\n  getPathOfID,\n  isAncestor,\n  pathIsDisabled,\n  pathIsDisabledByPaths,\n  pathsAreEqual,\n} from './pathUtils';\nimport { prepareRuleOrGroup } from './prepareQueryObjects';\nimport { regenerateIDs } from './regenerateIDs';\n\n/**\n * Why a query tool returned the query unmodified. Query tools never throw; when they cannot\n * carry out an operation they return the original query and report the reason through\n * {@link AbortOptions.onAbort}.\n *\n * `\"same-location\"` and `\"no-change\"` describe operations that were valid but had nothing to\n * do, so they are not errors. Every other reason indicates a target that could not be used.\n *\n * @group Query Tools\n */\nexport type AbortReason =\n  /** The rule/group identified by the given path or `id` does not exist. */\n  | 'target-not-found'\n  /** The parent group identified by the given path or `id` does not exist. */\n  | 'parent-not-found'\n  /** The given parent path or `id` refers to a rule rather than a group. */\n  | 'parent-not-a-group'\n  /** The given path or `id` refers to a rule rather than a group. */\n  | 'target-not-a-group'\n  /** The destination's parent group does not exist. */\n  | 'destination-not-found'\n  /** The root group cannot be removed, moved, grouped, or ungrouped. */\n  | 'root-not-allowed'\n  /** In a query with independent combinators, the target index holds a rule, not a combinator. */\n  | 'not-a-combinator-slot'\n  /** The rule/group is already at the destination. Not an error. */\n  | 'same-location'\n  /** The property already has the given value. Not an error. */\n  | 'no-change'\n  /** The target rule/group is disabled, or descends from a disabled group. */\n  | 'target-disabled'\n  /** The parent group is disabled, or descends from a disabled group. */\n  | 'parent-disabled'\n  /** Adding the group would nest it deeper than `maxLevels` allows. */\n  | 'max-levels-exceeded';\n\n/**\n * Details about an aborted query tool operation.\n *\n * @group Query Tools\n */\nexport interface AbortInfo {\n  /** Why the operation was aborted. */\n  reason: AbortReason;\n  /** The query tool that aborted. */\n  operation: 'add' | 'remove' | 'update' | 'move' | 'insert' | 'group' | 'ungroup';\n  /** The path or `id` that could not be used, when the reason relates to a specific target. */\n  pathOrID?: Path | string;\n}\n\n/**\n * Options that block a mutation before it is attempted.\n *\n * `disabled` is a property of the query itself, so honoring it is a matter of data integrity\n * rather than presentation: a query saved with a locked rule should stay locked when it is\n * loaded again. It is opt-in here only to preserve the existing behavior of the standalone\n * query tools; {@link QueryManager} enables it by default.\n *\n * @group Query Tools\n */\nexport interface GuardOptions {\n  /**\n   * Abort when the target (or its parent, for `add`/`insert`) is disabled, either directly or\n   * by descending from a disabled group. Defaults to `false`.\n   *\n   * Updating a rule or group's own `disabled` property is always permitted, since it is the\n   * only way to re-enable it.\n   */\n  respectDisabled?: boolean;\n  /**\n   * Paths that are disabled without the corresponding rule or group carrying a `disabled`\n   * property, mirroring the array form of the `QueryBuilder` `disabled` prop. A path is treated\n   * as disabled if it appears here or descends from a path that does.\n   *\n   * Like the `disabled` property, this is only honored when `respectDisabled` is `true`, and\n   * updating a rule or group's own `disabled` property is still permitted.\n   */\n  disabledPaths?: Path[];\n  /** Abort every mutation, as though the entire query were disabled. Defaults to `false`. */\n  queryDisabled?: boolean;\n  /**\n   * The maximum depth at which a group may be added. A group whose parent path is already this\n   * deep is rejected by `add` and `insert`. Rules are unaffected. Defaults to `Infinity`.\n   */\n  maxLevels?: number;\n}\n\n/**\n * Options for reporting aborted query tool operations.\n *\n * @group Query Tools\n */\nexport interface AbortOptions extends GuardOptions {\n  /**\n   * Called when the operation returns the query unmodified, with the reason why. Query tools\n   * never throw, so this is the only way to distinguish \"the target was invalid\" from\n   * \"the operation had nothing to do\".\n   */\n  onAbort?: (info: AbortInfo) => void;\n}\n\n/**\n * Options shared by every query tool.\n *\n * @group Query Tools\n */\nexport interface QueryToolOptions extends AbortOptions {\n  /**\n   * Set `false` to skip immer's auto-freeze on the returned query, e.g. when the query will be\n   * handed to a framework that wraps it in a proxy (Vue `reactive`, Solid stores) or otherwise\n   * needs to mutate it. Ignored by the `*InPlace` variants, which never freeze.\n   *\n   * @default true\n   */\n  freeze?: boolean;\n}\n\n/**\n * Whether a mutation targeting `pathOrID` is blocked by the given guards, and why.\n * Returns `null` when the mutation may proceed.\n *\n * Exported so that callers which run their own logic before mutating—such as a UI layer that\n * invokes a confirmation callback—can apply the same rules without duplicating them.\n *\n * @group Query Tools\n */\nexport const getGuardAbortReason = (\n  query: RuleGroupTypeAny,\n  pathOrID: Path | string | undefined,\n  guards: GuardOptions = {},\n  { asParent = false }: { asParent?: boolean } = {}\n): AbortReason | null => {\n  if (guards.queryDisabled) return asParent ? 'parent-disabled' : 'target-disabled';\n\n  if (!guards.respectDisabled || pathOrID === undefined) return null;\n\n  const path = Array.isArray(pathOrID) ? pathOrID : getPathOfID(pathOrID, query);\n  if (path && (pathIsDisabled(path, query) || pathIsDisabledByPaths(path, guards.disabledPaths))) {\n    return asParent ? 'parent-disabled' : 'target-disabled';\n  }\n\n  return null;\n};\n\n/**\n * Whether adding a group beneath `parentPath` would exceed `maxLevels`.\n *\n * @group Query Tools\n */\nexport const exceedsMaxLevels = (\n  parentPath: Path | undefined,\n  { maxLevels = Infinity }: GuardOptions = {}\n): boolean => !!parentPath && parentPath.length >= maxLevels;\n\n/**\n * Options for {@link add}.\n *\n * @group Query Tools\n */\nexport interface AddOptions extends QueryToolOptions {\n  /**\n   * If the query extends `RuleGroupTypeIC` (i.e. the query has independent\n   * combinators), then the first combinator in this list will be inserted\n   * before the new rule/group if the parent group is not empty. This option\n   * is overridden by `combinatorPreceding`.\n   */\n  combinators?: OptionList;\n  /**\n   * If the query extends `RuleGroupTypeIC` (i.e. the query has independent\n   * combinators), then this combinator will be inserted before the new rule/group\n   * if the parent group is not empty. This option will supersede `combinators`.\n   */\n  combinatorPreceding?: string;\n  /**\n   * ID generator.\n   */\n  idGenerator?: () => string;\n}\n\nexport interface AddMethod {\n  <RG extends RuleGroupTypeAny>(\n    /** The query to update. */\n    query: RG,\n    /** The rule or group to add. */\n    ruleOrGroup: RG | RuleType,\n    /** Path or ID of the group to add to. */\n    parentPathOrID: Path | string,\n    /** Options. */\n    options?: AddOptions\n  ): RG;\n}\n\n/**\n * Adds a rule or group to a query without mutating the original query.\n *\n * @returns A new query with the rule or group added.\n *\n * @group Query Tools\n */\nexport const add: AddMethod = (query, ruleOrGroup, parentPathOrID, options = {}): typeof query =>\n  producerFor(options.freeze)(query, q =>\n    addInPlace(q, ruleOrGroup as RuleType, parentPathOrID, options)\n  );\n\n/**\n * Adds a rule or group to a query in place.\n *\n * @returns The query (mutated in place) with the rule or group added.\n *\n * @group Query Tools\n */\nexport const addInPlace: AddMethod = (\n  query,\n  ruleOrGroup,\n  parentPathOrID,\n  options = {}\n): typeof query => {\n  const {\n    combinators = defaultCombinators,\n    combinatorPreceding,\n    idGenerator = generateID,\n    onAbort,\n  } = options;\n  const parent = Array.isArray(parentPathOrID)\n    ? findPath(parentPathOrID, query)\n    : findID(parentPathOrID, query);\n\n  if (!parent) {\n    onAbort?.({ reason: 'parent-not-found', operation: 'add', pathOrID: parentPathOrID });\n    return query;\n  }\n\n  if (!isRuleGroup(parent)) {\n    onAbort?.({ reason: 'parent-not-a-group', operation: 'add', pathOrID: parentPathOrID });\n    return query;\n  }\n\n  // The parent is known to exist by this point, so the path always resolves.\n  const parentPath = (\n    Array.isArray(parentPathOrID) ? parentPathOrID : getPathOfID(parentPathOrID, query)\n  ) as Path;\n\n  const addGuardReason = getGuardAbortReason(query, parentPath, options, { asParent: true });\n  if (addGuardReason) {\n    onAbort?.({ reason: addGuardReason, operation: 'add', pathOrID: parentPathOrID });\n    return query;\n  }\n\n  if (isRuleGroup(ruleOrGroup) && exceedsMaxLevels(parentPath, options)) {\n    onAbort?.({ reason: 'max-levels-exceeded', operation: 'add', pathOrID: parentPathOrID });\n    return query;\n  }\n\n  if (isRuleGroupTypeIC(parent) && parent.rules.length > 0) {\n    const prevCombinator = parent.rules.at(-2);\n    parent.rules.push(\n      // @ts-expect-error This is technically a type violation until the next push\n      // to the rules array, but that happens immediately and unconditionally so\n      // there's no significant risk.\n      combinatorPreceding ??\n        (typeof prevCombinator === 'string' ? prevCombinator : getFirstOption(combinators))\n    );\n  }\n  // `as RuleType` is only here to avoid the ambiguity with `RuleGroupTypeAny`\n  parent.rules.push(prepareRuleOrGroup(ruleOrGroup, { idGenerator }) as RuleType);\n\n  return query;\n};\n\n/**\n * Options for {@link update}.\n *\n * @group Query Tools\n */\nexport interface UpdateOptions extends QueryToolOptions {\n  /**\n   * When updating the `field` of a rule, the rule's `operator`, `value`, and `valueSource`\n   * will be reset to their respective defaults. Defaults to `true`.\n   */\n  resetOnFieldChange?: boolean;\n  /**\n   * When updating the `operator` of a rule, the rule's `value` and `valueSource`\n   * will be reset to their respective defaults. Defaults to `false`.\n   */\n  resetOnOperatorChange?: boolean;\n  /**\n   * Determines the default operator name for a given field.\n   */\n  getRuleDefaultOperator?: (field: string) => string;\n  /**\n   * Determines the valid value sources for a given field and operator.\n   */\n  getValueSources?: (field: string, operator: string) => ValueSources | ValueSourceFlexibleOptions;\n  /**\n   * Gets the default value for a given rule, in case the value needs to be reset.\n   */\n  // oxlint-disable-next-line typescript/no-explicit-any\n  getRuleDefaultValue?: (rule: RuleType) => any;\n  /**\n   * Determines the valid match modes for a given field.\n   */\n  getMatchModes?: (field: string) => MatchModeOptions;\n}\n\nexport interface UpdateMethod {\n  /**\n   * Updates a single property of a rule or group.\n   */\n  <RG extends RuleGroupTypeAny>(\n    /** The query to update. */\n    query: RG,\n    /** The name of the property to update. */\n    prop: UpdateableProperties,\n    /** The new value of the property. */\n    // oxlint-disable-next-line typescript/no-explicit-any\n    value: any,\n    /** The path or ID of the rule or group to update. */\n    pathOrID: Path | string,\n    /** Options. */\n    options?: UpdateOptions\n  ): RG;\n  /**\n   * Updates multiple properties of a rule or group using parallel arrays of\n   * property names and corresponding values.\n   */\n  <RG extends RuleGroupTypeAny>(\n    /** The query to update. */\n    query: RG,\n    /** The names of the properties to update. */\n    props: UpdateableProperties[],\n    /** The new values, in the same order as `props`. */\n    // oxlint-disable-next-line typescript/no-explicit-any\n    values: any[],\n    /** The path or ID of the rule or group to update. */\n    pathOrID: Path | string,\n    /** Options. */\n    options?: UpdateOptions\n  ): RG;\n  /**\n   * Updates multiple properties of a rule or group using a map of property\n   * names to their new values.\n   */\n  <RG extends RuleGroupTypeAny>(\n    /** The query to update. */\n    query: RG,\n    /** A map of property names to their new values. */\n    props: UpdateValueMap,\n    /** The path or ID of the rule or group to update. */\n    pathOrID: Path | string,\n    /** Options. */\n    options?: UpdateOptions\n  ): RG;\n}\n\n/**\n * Relative order in which properties are applied during a multi-property update.\n * Lower ranks apply first; `value` applies last so an explicitly-provided value\n * is never clobbered by a reset triggered by a `field`/`operator`/`valueSource`\n * change. Unranked properties default to `3` (between `valueSource` and `value`).\n */\nconst updatePropRank: Record<string, number> = { field: 0, operator: 1, valueSource: 2, value: 4 };\n\n/** Stable-sorts `[prop, value]` entries by {@link updatePropRank}. */\nconst orderUpdateEntries = (entries: [string, unknown][]): [string, unknown][] =>\n  entries.toSorted((x, y) => (updatePropRank[x[0]] ?? 3) - (updatePropRank[y[0]] ?? 3));\n\n/**\n * Normalizes the variadic {@link update}/{@link updateInPlace} arguments (single\n * prop+value, parallel arrays, or property map) into a canonically-ordered list\n * of `[prop, value]` entries plus the target path/ID and options.\n */\nconst normalizeUpdateArgs = (\n  a: unknown,\n  b: unknown,\n  c: unknown,\n  d: unknown\n): { entries: [string, unknown][]; pathOrID: Path | string; options: UpdateOptions } => {\n  // Single property: (prop, value, pathOrID, options)\n  if (typeof a === 'string') {\n    return { entries: [[a, b]], pathOrID: c as Path | string, options: (d as UpdateOptions) ?? {} };\n  }\n  // Parallel arrays: (props, values, pathOrID, options)\n  if (Array.isArray(a)) {\n    const values = b as unknown[];\n    return {\n      entries: orderUpdateEntries(a.map((prop, i): [string, unknown] => [prop, values[i]])),\n      pathOrID: c as Path | string,\n      options: (d as UpdateOptions) ?? {},\n    };\n  }\n  // Property map: (props, pathOrID, options)\n  return {\n    entries: orderUpdateEntries(Object.entries(a as Record<string, unknown>)),\n    pathOrID: b as Path | string,\n    options: (c as UpdateOptions) ?? {},\n  };\n};\n\n/**\n * The options argument of an {@link update} call, whose position depends on the argument form.\n * Cheaper than {@link normalizeUpdateArgs} when only the options are needed.\n */\nconst updateOptionsOf = (a: unknown, b: unknown, c: unknown, d: unknown): UpdateOptions =>\n  ((typeof a === 'string' || Array.isArray(a) ? d : c) as UpdateOptions) ?? {};\n\n/**\n * Applies one or more property updates to a query in place. Shared by\n * {@link update} and {@link updateInPlace}.\n */\nconst applyUpdatesInPlace = <RG extends RuleGroupTypeAny>(\n  query: RG,\n  a: unknown,\n  b: unknown,\n  c: unknown,\n  d: unknown\n): RG => {\n  const { entries, pathOrID, options } = normalizeUpdateArgs(a, b, c, d);\n  for (const [prop, value] of entries) {\n    updateInPlaceSingle(query, prop, value, pathOrID, options);\n  }\n  return query;\n};\n\n/**\n * Updates one or more properties of a rule or group within a query without\n * mutating the original query. Properties may be supplied individually\n * (`prop`, `value`), as parallel arrays (`props`, `values`), or as a\n * property-to-value map. For multi-property updates, `field`, `operator`, and\n * `valueSource` are applied before `value`, so an explicit `value` is never\n * reset by a change to one of those properties.\n *\n * @returns A new query with the rule or group properties updated.\n *\n * @group Query Tools\n */\nexport const update = (<RG extends RuleGroupTypeAny>(\n  query: RG,\n  a: unknown,\n  b?: unknown,\n  c?: unknown,\n  d?: unknown\n): RG =>\n  producerFor(updateOptionsOf(a, b, c, d).freeze)(query, q => {\n    applyUpdatesInPlace(q, a, b, c, d);\n  })) as UpdateMethod;\n\n/**\n * Updates one or more properties of a rule or group within a query in place.\n * See {@link update} for the supported argument forms and ordering semantics.\n *\n * @returns The query (mutated in place) with the rule or group properties updated.\n *\n * @group Query Tools\n */\nexport const updateInPlace = (<RG extends RuleGroupTypeAny>(\n  query: RG,\n  a: unknown,\n  b?: unknown,\n  c?: unknown,\n  d?: unknown\n): RG => applyUpdatesInPlace(query, a, b, c, d)) as UpdateMethod;\n\n/**\n * Updates a single property of a rule or group within a query in place.\n */\nconst updateInPlaceSingle = <RG extends RuleGroupTypeAny>(\n  query: RG,\n  prop: UpdateableProperties,\n  // oxlint-disable-next-line typescript/no-explicit-any\n  value: any,\n  pathOrID: Path | string,\n  options: UpdateOptions = {}\n): RG => {\n  const {\n    resetOnFieldChange: _resetOnFieldChange = true,\n    resetOnOperatorChange = false,\n    getRuleDefaultOperator = () => '=',\n    getValueSources = () => ['value'],\n    getRuleDefaultValue = () => '',\n    getMatchModes = () => [],\n    onAbort,\n  } = options;\n\n  let resetOnFieldChange = _resetOnFieldChange;\n\n  const path = Array.isArray(pathOrID) ? pathOrID : getPathOfID(pathOrID, query);\n\n  // Ignore invalid paths/ids\n  if (!path) {\n    onAbort?.({ reason: 'target-not-found', operation: 'update', pathOrID });\n    return query;\n  }\n\n  // A node's own `disabled` property is exempt from `respectDisabled`—otherwise a disabled node\n  // could never be re-enabled. `queryDisabled` still blocks it, since that disables everything.\n  const updateGuards =\n    prop === 'disabled' ? { queryDisabled: options.queryDisabled } : (options as GuardOptions);\n  const updateGuardReason = getGuardAbortReason(query, path, updateGuards);\n  if (updateGuardReason) {\n    onAbort?.({ reason: updateGuardReason, operation: 'update', pathOrID });\n    return query;\n  }\n\n  // Independent combinators\n  if (prop === 'combinator' && !isRuleGroupType(query)) {\n    // The combinator slot itself is a bare string, so `findPath` can't confirm the target\n    // exists; the containing group has to be resolved instead.\n    const parent = findPath(getParentPath(path), query);\n    if (!parent || !isRuleGroup(parent)) {\n      onAbort?.({ reason: 'target-not-found', operation: 'update', pathOrID });\n      return query;\n    }\n    const parentRules = parent.rules;\n    // Only update an independent combinator if it occupies an odd index\n    if (path.at(-1)! % 2 === 1) {\n      if (parentRules[path.at(-1)!] === value) {\n        onAbort?.({ reason: 'no-change', operation: 'update', pathOrID });\n      } else {\n        parentRules[path.at(-1)!] = value;\n      }\n    } else {\n      onAbort?.({ reason: 'not-a-combinator-slot', operation: 'update', pathOrID });\n    }\n    return query;\n  }\n\n  const ruleOrGroup = findPath(path, query);\n\n  // Ignore invalid paths\n  if (!ruleOrGroup) {\n    onAbort?.({ reason: 'target-not-found', operation: 'update', pathOrID });\n    return query;\n  }\n\n  const isGroup = isRuleGroup(ruleOrGroup);\n\n  // Only update if there is actually a change\n  // @ts-expect-error prop can refer to rule or group properties\n  if (ruleOrGroup[prop] === value) {\n    onAbort?.({ reason: 'no-change', operation: 'update', pathOrID });\n    return query;\n  }\n\n  // Handle valueSource updates later\n  if (prop !== 'valueSource') {\n    // @ts-expect-error prop can refer to rule or group properties\n    ruleOrGroup[prop] = value;\n  }\n\n  // If this is a group, there's no more to do\n  if (isGroup) return query;\n\n  let resetValueSource = false;\n  let resetValue = false;\n\n  if (prop === 'field') {\n    const fromFieldMatchModes = getMatchModes(ruleOrGroup.field);\n    const toFieldMatchModes = getMatchModes(value);\n\n    if (toFieldMatchModes.length === 0) {\n      delete ruleOrGroup.match;\n    } else {\n      const nextMatchMode =\n        ruleOrGroup.match?.mode && getOption(toFieldMatchModes, ruleOrGroup.match.mode)\n          ? null\n          : getFirstOption(toFieldMatchModes);\n      if (nextMatchMode) {\n        ruleOrGroup.match = { mode: nextMatchMode, threshold: 1 };\n      }\n    }\n\n    if (fromFieldMatchModes.length > 0 || toFieldMatchModes.length > 0) {\n      // Force `resetOnFieldChange` when field is updated FROM or TO one that has match modes\n      resetOnFieldChange = true;\n    }\n  }\n\n  // Set default operator, valueSource, and value for field change\n  if (resetOnFieldChange && prop === 'field') {\n    ruleOrGroup.operator = getRuleDefaultOperator(value);\n    resetValueSource = true;\n    resetValue = true;\n  }\n\n  // Set default valueSource and value for operator change\n  if (resetOnOperatorChange && prop === 'operator') {\n    resetValueSource = true;\n    resetValue = true;\n  }\n\n  const valueSources = getValueSourcesUtil(\n    { name: ruleOrGroup.field, value: ruleOrGroup.field, label: '' },\n    ruleOrGroup.operator,\n    getValueSources\n  );\n  const defaultValueSource = getFirstOption(valueSources);\n  if (\n    (resetValueSource &&\n      ruleOrGroup.valueSource &&\n      defaultValueSource !== ruleOrGroup.valueSource) ||\n    (prop === 'valueSource' && value !== ruleOrGroup.valueSource)\n  ) {\n    // Only reset the value if we're changing the valueSource either\n    // 1) from `undefined` to something that is _not_ the default, or\n    // 2) from the current (defined) value to something else\n    resetValue =\n      !!ruleOrGroup.valueSource || (!ruleOrGroup.valueSource && value !== defaultValueSource);\n    ruleOrGroup.valueSource = resetValueSource ? defaultValueSource : value;\n  }\n\n  if (resetValue) {\n    // The default value should be a valid field name if defaultValueSource is 'field'\n    ruleOrGroup.value = getRuleDefaultValue(ruleOrGroup);\n  }\n\n  return query;\n};\n\n/**\n * Options for {@link remove}.\n *\n * @group Query Tools\n */\nexport interface RemoveOptions extends QueryToolOptions {}\n\nexport interface RemoveMethod {\n  <RG extends RuleGroupTypeAny>(\n    /** The query to update. */\n    query: RG,\n    /** The path or ID of the rule or group to remove. */\n    pathOrID: Path | string,\n    /** Options. */\n    options?: RemoveOptions\n  ): RG;\n}\n\n/**\n * Removes a rule or group from a query without mutating the original query.\n *\n * @returns A new query with the rule or group removed.\n *\n * @group Query Tools\n */\nexport const remove: RemoveMethod = (query, pathOrID, options = {}): typeof query =>\n  producerFor(options.freeze)(query, q => removeInPlace(q, pathOrID, options));\n\n/**\n * Removes a rule or group from a query in place.\n *\n * @returns The query (mutated in place) with the rule or group removed.\n *\n * @group Query Tools\n */\nexport const removeInPlace: RemoveMethod = (query, pathOrID, options = {}): typeof query => {\n  const { onAbort } = options;\n  const path = Array.isArray(pathOrID) ? pathOrID : getPathOfID(pathOrID, query);\n\n  // Ignore invalid paths/ids\n  if (!path) {\n    onAbort?.({ reason: 'target-not-found', operation: 'remove', pathOrID });\n    return query;\n  }\n\n  // Can't remove the root group\n  if (path.length === 0) {\n    onAbort?.({ reason: 'root-not-allowed', operation: 'remove', pathOrID });\n    return query;\n  }\n\n  const removeGuardReason = getGuardAbortReason(query, path, options);\n  if (removeGuardReason) {\n    onAbort?.({ reason: removeGuardReason, operation: 'remove', pathOrID });\n    return query;\n  }\n\n  // The target must actually exist. This also covers independent combinators, which `findPath`\n  // reports as missing because they are bare strings rather than rules—they can only be removed\n  // alongside the rule they precede or follow.\n  if (!findPath(path, query)) {\n    onAbort?.({ reason: 'target-not-found', operation: 'remove', pathOrID });\n    return query;\n  }\n\n  // The target resolved, so at a non-root path its parent is necessarily an existing group.\n  const index = path.at(-1)!;\n  const parent = findPath(getParentPath(path), query) as RuleGroupTypeAny;\n  if (!isRuleGroupType(parent) && parent.rules.length > 1) {\n    const idxStartDelete = index === 0 ? 0 : index - 1;\n    parent.rules.splice(idxStartDelete, 2);\n  } else {\n    parent.rules.splice(index, 1);\n  }\n\n  return query;\n};\n\n/**\n * Options for {@link ungroup}.\n *\n * @group Query Tools\n */\nexport interface UngroupOptions extends QueryToolOptions {}\n\nexport interface UngroupMethod {\n  <RG extends RuleGroupTypeAny>(\n    /** The query to update. */\n    query: RG,\n    /** The path or ID of the group to ungroup. */\n    pathOrID: Path | string,\n    /** Options. */\n    options?: UngroupOptions\n  ): RG;\n}\n\n/**\n * Replaces a group with its own rules in its parent group, without mutating the original query.\n *\n * The group's `combinator`, `not`, `id`, `muted`, and `disabled` properties are discarded; its\n * rules keep their `id`s since they remain in the same query. An empty group is simply removed.\n *\n * @returns A new query with the group's rules promoted into its parent group.\n *\n * @group Query Tools\n */\nexport const ungroup: UngroupMethod = (query, pathOrID, options = {}): typeof query =>\n  producerFor(options.freeze)(query, q => ungroupInPlace(q, pathOrID, options));\n\n/**\n * Replaces a group with its own rules in its parent group, in place.\n *\n * @returns The query (mutated in place) with the group's rules promoted into its parent group.\n *\n * @group Query Tools\n */\nexport const ungroupInPlace: UngroupMethod = (query, pathOrID, options = {}): typeof query => {\n  const { onAbort } = options;\n  const path = Array.isArray(pathOrID) ? pathOrID : getPathOfID(pathOrID, query);\n\n  // Ignore invalid paths/ids\n  if (!path) {\n    onAbort?.({ reason: 'target-not-found', operation: 'ungroup', pathOrID });\n    return query;\n  }\n\n  // Can't ungroup the root group\n  if (path.length === 0) {\n    onAbort?.({ reason: 'root-not-allowed', operation: 'ungroup', pathOrID });\n    return query;\n  }\n\n  const ungroupGuardReason = getGuardAbortReason(query, path, options);\n  if (ungroupGuardReason) {\n    onAbort?.({ reason: ungroupGuardReason, operation: 'ungroup', pathOrID });\n    return query;\n  }\n\n  // The target must actually exist. This also covers independent combinators, which `findPath`\n  // reports as missing because they are bare strings rather than rules.\n  const target = findPath(path, query);\n  if (!target) {\n    onAbort?.({ reason: 'target-not-found', operation: 'ungroup', pathOrID });\n    return query;\n  }\n\n  // Only groups can be ungrouped.\n  if (!isRuleGroup(target)) {\n    onAbort?.({ reason: 'target-not-a-group', operation: 'ungroup', pathOrID });\n    return query;\n  }\n\n  // The target resolved, so at a non-root path its parent is necessarily an existing group.\n  const index = path.at(-1)!;\n  const parent = findPath(getParentPath(path), query) as RuleGroupTypeAny;\n\n  // An empty group degenerates to a plain removal, including the adjacent-combinator cleanup\n  // independent combinator queries require.\n  if (target.rules.length === 0) {\n    if (!isRuleGroupType(parent) && parent.rules.length > 1) {\n      const idxStartDelete = index === 0 ? 0 : index - 1;\n      parent.rules.splice(idxStartDelete, 2);\n    } else {\n      parent.rules.splice(index, 1);\n    }\n    return query;\n  }\n\n  // For independent combinators, the group's rules are already in `[rule, comb, rule, ...]` form,\n  // so splicing them in place preserves the parent's alternation.\n  type AnyRules = (RuleType | RuleGroupTypeAny | string)[];\n  (parent.rules as AnyRules).splice(index, 1, ...(target.rules as AnyRules));\n\n  return query;\n};\n\nconst getNextPath = (\n  query: RuleGroupTypeAny,\n  currentPath: Path,\n  newPathOrShiftDirection: Path | 'up' | 'down'\n): Path => {\n  if (Array.isArray(newPathOrShiftDirection)) {\n    return newPathOrShiftDirection;\n  }\n\n  const ic = isRuleGroupTypeIC(query);\n\n  if (newPathOrShiftDirection === 'up') {\n    if (pathsAreEqual(currentPath, [0])) {\n      return currentPath;\n    } else if (currentPath.at(-1) === 0) {\n      const parentPath = getParentPath(currentPath);\n      return [...getParentPath(parentPath), Math.max(0, parentPath.at(-1)! - (ic ? 1 : 0))];\n    } else {\n      const evaluationPath = [\n        ...getParentPath(currentPath),\n        Math.max(0, currentPath.at(-1)! - (ic ? 2 : 1)),\n      ];\n      const entityAtTarget = findPath(evaluationPath, query);\n      if (isRuleGroup(entityAtTarget)) {\n        return [...evaluationPath, entityAtTarget.rules.length];\n      } else {\n        const targetPath = [\n          ...getParentPath(currentPath),\n          Math.max(0, currentPath.at(-1)! - (ic ? 3 : 1)),\n        ];\n        return targetPath;\n      }\n    }\n  } else if (newPathOrShiftDirection === 'down') {\n    if (pathsAreEqual([query.rules.length - 1], currentPath)) {\n      return currentPath;\n    } else if (\n      currentPath.at(-1) ===\n      (findPath(getParentPath(currentPath), query) as RuleGroupTypeAny).rules.length - 1\n    ) {\n      const parentPath = getParentPath(currentPath);\n      return [...getParentPath(parentPath), parentPath.at(-1)! + 1];\n    } else {\n      const evaluationPath = [...getParentPath(currentPath), currentPath.at(-1)! + (ic ? 2 : 1)];\n      const entityToEvaluate = findPath(evaluationPath, query);\n      if (isRuleGroup(entityToEvaluate)) {\n        return [...evaluationPath, 0];\n      } else {\n        const targetPath = [...getParentPath(currentPath), currentPath.at(-1)! + (ic ? 3 : 2)];\n        return targetPath;\n      }\n    }\n  }\n\n  return currentPath;\n};\n\n/**\n * Options for {@link move}.\n *\n * @group Query Tools\n */\nexport interface MoveOptions extends QueryToolOptions {\n  /**\n   * When `true`, the source rule/group will not be removed from its original path.\n   */\n  clone?: boolean;\n  /**\n   * If the query extends `RuleGroupTypeIC` (i.e. the query is using independent\n   * combinators), then the first combinator in this list will be inserted before\n   * the rule/group if necessary.\n   */\n  combinators?: OptionList;\n  /**\n   * ID generator.\n   */\n  idGenerator?: () => string;\n}\n\nexport interface MoveMethod {\n  <RG extends RuleGroupTypeAny>(\n    /** The query to update. */\n    query: RG,\n    /** ID or original path of the rule or group to move. */\n    oldPathOrID: Path | string,\n    /** Path to move the rule or group to, or a shift direction. */\n    newPath: Path | 'up' | 'down',\n    /** Options. */\n    options?: MoveOptions\n  ): RG;\n}\n\n/**\n * Moves a rule or group from one path to another without mutating the original query.\n * In the options parameter, pass `{ clone: true }` to copy instead of move.\n *\n * @returns A new query with the rule or group moved or cloned.\n *\n * @group Query Tools\n */\nexport const move: MoveMethod = (query, oldPathOrID, newPath, options = {}): typeof query =>\n  producerFor(options.freeze)(query, q => moveInPlace(q, oldPathOrID, newPath, options));\n\n/**\n * Moves a rule or group from one path to another in place.\n * In the options parameter, pass `{ clone: true }` to copy instead of move.\n *\n * @returns The query (mutated in place) with the rule or group moved or cloned.\n *\n * @group Query Tools\n */\nexport const moveInPlace: MoveMethod = (\n  query,\n  oldPathOrID,\n  newPath,\n  options = {}\n): typeof query => {\n  const {\n    clone = false,\n    combinators = defaultCombinators,\n    idGenerator = generateID,\n    onAbort,\n  } = options;\n  const oldPath = Array.isArray(oldPathOrID) ? oldPathOrID : getPathOfID(oldPathOrID, query);\n\n  // Ignore invalid paths/ids\n  if (!oldPath) {\n    onAbort?.({ reason: 'target-not-found', operation: 'move', pathOrID: oldPathOrID });\n    return query;\n  }\n\n  // Ignore paths that don't identify anything. This must happen before `getNextPath`, which\n  // assumes the source exists when resolving a shift direction.\n  const ruleOrGroupOriginal = findPath(oldPath, query);\n  if (!ruleOrGroupOriginal) {\n    onAbort?.({ reason: 'target-not-found', operation: 'move', pathOrID: oldPathOrID });\n    return query;\n  }\n\n  const nextPath = getNextPath(query, oldPath, newPath);\n\n  // Can't move the root group\n  if (oldPath.length === 0) {\n    onAbort?.({ reason: 'root-not-allowed', operation: 'move', pathOrID: oldPathOrID });\n    return query;\n  }\n\n  const moveGuardReason = getGuardAbortReason(query, oldPath, options);\n  if (moveGuardReason) {\n    onAbort?.({ reason: moveGuardReason, operation: 'move', pathOrID: oldPathOrID });\n    return query;\n  }\n\n  // Don't move to the same location\n  if (pathsAreEqual(oldPath, nextPath)) {\n    onAbort?.({ reason: 'same-location', operation: 'move', pathOrID: oldPathOrID });\n    return query;\n  }\n\n  // Don't move to a path that doesn't exist yet\n  if (!findPath(getParentPath(nextPath), query)) {\n    onAbort?.({ reason: 'destination-not-found', operation: 'move', pathOrID: newPath });\n    return query;\n  }\n\n  // Don't move a group inside itself. The destination is part of the subtree being relocated,\n  // so it ceases to exist the moment the source is spliced out. (Cloning is fine: the source\n  // stays put and a regenerated copy is inserted.)\n  if (!clone && isAncestor(oldPath, nextPath)) {\n    onAbort?.({ reason: 'destination-not-found', operation: 'move', pathOrID: newPath });\n    return query;\n  }\n\n  const ruleOrGroup = clone\n    ? regenerateIDs(\n        isDraft(ruleOrGroupOriginal) ? current(ruleOrGroupOriginal) : ruleOrGroupOriginal,\n        { idGenerator }\n      )\n    : ruleOrGroupOriginal;\n\n  const independentCombinators = isRuleGroupTypeIC(query);\n  const parentOfRuleToRemove = findPath(getParentPath(oldPath), query) as typeof query;\n  const ruleToRemoveIndex = oldPath.at(-1)!;\n  const oldPrevCombinator =\n    independentCombinators && ruleToRemoveIndex > 0\n      ? (parentOfRuleToRemove.rules[ruleToRemoveIndex - 1] as string)\n      : null;\n  const oldNextCombinator =\n    independentCombinators && ruleToRemoveIndex < parentOfRuleToRemove.rules.length - 1\n      ? (parentOfRuleToRemove.rules[ruleToRemoveIndex + 1] as string)\n      : null;\n\n  // Remove the source item if not cloning\n  if (!clone) {\n    const idxStartDelete = independentCombinators\n      ? Math.max(0, ruleToRemoveIndex - 1)\n      : ruleToRemoveIndex;\n    const deleteLength = independentCombinators ? 2 : 1;\n    parentOfRuleToRemove.rules.splice(idxStartDelete, deleteLength);\n  }\n\n  const newNewPath = [...nextPath];\n  if (independentCombinators && newNewPath.at(-1)! % 2 === 1) {\n    // Odd indexes in an independent combinator array identify combinators, not\n    // rules/groups. Dropping on a combinator means \"insert after the rule that\n    // precedes it\", which is the same slot as the next (even) index. Normalizing\n    // here keeps the destination index even, which the insertion logic below\n    // relies on to maintain the rule/combinator alternation.\n    newNewPath[newNewPath.length - 1] += 1;\n  }\n  const commonAncestorPath = getCommonAncestorPath(oldPath, nextPath);\n  if (\n    !clone &&\n    oldPath.length === commonAncestorPath.length + 1 &&\n    newNewPath[commonAncestorPath.length] > oldPath[commonAncestorPath.length]\n  ) {\n    // Getting here means there will be a shift of paths upward at the common\n    // ancestor level because the object at `oldPath` will be spliced out. The\n    // real new path should therefore be one or two higher than `nextPath`.\n    newNewPath[commonAncestorPath.length] -= independentCombinators ? 2 : 1;\n  }\n  const newNewParentPath = getParentPath(newNewPath);\n  const parentToInsertInto = findPath(newNewParentPath, query) as typeof query;\n  const newIndex = newNewPath.at(-1)!;\n  // For independent combinator arrays, `newIndex` is the (even) index the moved\n  // rule/group should end up at, but the splice inserts a `[combinator, ruleOrGroup]`\n  // pair, so it must happen one slot earlier.\n  const spliceIndex = independentCombinators && newIndex > 0 ? newIndex - 1 : newIndex;\n\n  /**\n   * This function 1) glosses over the need for type assertions to splice directly\n   * into `parentToInsertInto.rules`, and 2) shortens the actual insertion code.\n   */\n  // oxlint-disable-next-line typescript/no-explicit-any\n  const insertRuleOrGroup = (...args: any[]) =>\n    parentToInsertInto.rules.splice(spliceIndex, 0, ...args);\n\n  // Insert the source item at the target path\n  if (parentToInsertInto.rules.length === 0 || !independentCombinators) {\n    insertRuleOrGroup(ruleOrGroup);\n  } else {\n    if (newIndex === 0) {\n      if (ruleToRemoveIndex === 0 && oldNextCombinator) {\n        insertRuleOrGroup(ruleOrGroup, oldNextCombinator);\n      } else {\n        const newNextCombinator =\n          parentToInsertInto.rules[1] ?? oldPrevCombinator ?? getFirstOption(combinators);\n        insertRuleOrGroup(ruleOrGroup, newNextCombinator);\n      }\n    } else {\n      if (oldPrevCombinator) {\n        insertRuleOrGroup(oldPrevCombinator, ruleOrGroup);\n      } else {\n        const newPrevCombinator =\n          parentToInsertInto.rules[spliceIndex - 2] ??\n          oldNextCombinator ??\n          getFirstOption(combinators);\n        insertRuleOrGroup(newPrevCombinator, ruleOrGroup);\n      }\n    }\n  }\n\n  return query;\n};\n\n/**\n * Options for {@link insert}.\n *\n * @group Query Tools\n */\nexport interface InsertOptions extends QueryToolOptions {\n  /**\n   * If the query extends `RuleGroupTypeIC` (i.e. the query has independent\n   * combinators), then the first combinator in this list will be inserted\n   * before the new rule/group if the parent group is not empty. This option\n   * is overridden by `combinatorPreceding`.\n   */\n  combinators?: OptionList;\n  /**\n   * If the query extends `RuleGroupTypeIC` (i.e. the query has independent\n   * combinators), then this combinator will be inserted before the new rule/group\n   * if the parent group is not empty and the new rule/group is not the first in the\n   * group (`path.at(-1) > 0`). This option will supersede `combinators`.\n   */\n  combinatorPreceding?: string;\n  /**\n   * If the query extends `RuleGroupTypeIC` (i.e. the query has independent\n   * combinators), then this combinator will be inserted after the new rule/group\n   * if the parent group is not empty and the new rule/group is the first in the\n   * group (`path.at(-1) === 0`). This option will supersede `combinators`.\n   */\n  combinatorSucceeding?: string;\n  /**\n   * ID generator.\n   *\n   * @default generateID\n   */\n  idGenerator?: () => string;\n  /**\n   * When `true`, the new rule/group will replace the rule/group at `path`.\n   */\n  replace?: boolean;\n}\n\nexport interface InsertMethod {\n  <RG extends RuleGroupTypeAny>(\n    /** The query to update. */\n    query: RG,\n    /** The rule or group to insert. */\n    ruleOrGroup: RG | RuleType,\n    /** Path at which to insert the rule or group. */\n    path: number[],\n    /** Options. */\n    options?: InsertOptions\n  ): RG;\n}\n\n/**\n * Inserts a rule or group into a query without mutating the original query.\n *\n * @returns A new query with the rule or group inserted.\n *\n * @group Query Tools\n */\nexport const insert: InsertMethod = (query, ruleOrGroup, path, options = {}): typeof query =>\n  producerFor(options.freeze)(query, q => insertInPlace(q, ruleOrGroup as RuleType, path, options));\n\n/**\n * Inserts a rule or group into a query in place.\n *\n * @returns The query (mutated in place) with the rule or group inserted.\n *\n * @group Query Tools\n */\nexport const insertInPlace: InsertMethod = (\n  query,\n  ruleOrGroup,\n  path,\n  options = {}\n): typeof query => {\n  const {\n    combinators = defaultCombinators,\n    combinatorPreceding,\n    combinatorSucceeding,\n    idGenerator = generateID,\n    replace = false,\n    onAbort,\n  } = options;\n\n  const parentToInsertInto = findPath(getParentPath(path), query) as typeof query;\n  if (!parentToInsertInto) {\n    onAbort?.({ reason: 'parent-not-found', operation: 'insert', pathOrID: path });\n    return query;\n  }\n\n  if (!isRuleGroup(parentToInsertInto)) {\n    onAbort?.({ reason: 'parent-not-a-group', operation: 'insert', pathOrID: path });\n    return query;\n  }\n\n  const parentPath = getParentPath(path);\n  const insertGuardReason = getGuardAbortReason(query, parentPath, options, { asParent: true });\n  if (insertGuardReason) {\n    onAbort?.({ reason: insertGuardReason, operation: 'insert', pathOrID: path });\n    return query;\n  }\n\n  if (isRuleGroup(ruleOrGroup) && exceedsMaxLevels(parentPath, options)) {\n    onAbort?.({ reason: 'max-levels-exceeded', operation: 'insert', pathOrID: path });\n    return query;\n  }\n\n  const rorg = regenerateIDs(ruleOrGroup, { idGenerator });\n  const independentCombinators = isRuleGroupTypeIC(query);\n  const newIndex = path.at(-1)!;\n\n  /**\n   * This function 1) glosses over the need for type assertions to splice directly\n   * into `parentToInsertInto.rules`, and 2) shortens the actual insertion code.\n   */\n  // oxlint-disable-next-line typescript/no-explicit-any\n  const insertRuleOrGroup = (idx: number, ...args: any[]) =>\n    parentToInsertInto.rules.splice(idx, replace ? args.length : 0, ...args);\n\n  // Insert the source item at the target path\n  if (parentToInsertInto.rules.length === 0 || !independentCombinators) {\n    insertRuleOrGroup(newIndex, rorg);\n  } else if (replace && independentCombinators) {\n    insertRuleOrGroup(newIndex + (newIndex % 2), rorg);\n  } else {\n    if (newIndex === 0) {\n      if (rorg.path?.at(-1) === 0 && combinatorSucceeding) {\n        insertRuleOrGroup(newIndex, rorg, combinatorSucceeding);\n      } else {\n        const newNextCombinator =\n          parentToInsertInto.rules[1] ?? combinatorPreceding ?? getFirstOption(combinators);\n        insertRuleOrGroup(newIndex, rorg, newNextCombinator);\n      }\n    } else {\n      const normalizedNewIndex = newIndex % 2 === 0 ? newIndex - 1 : newIndex;\n      if (combinatorPreceding) {\n        insertRuleOrGroup(normalizedNewIndex, combinatorPreceding, rorg);\n      } else {\n        const newPrevCombinator =\n          parentToInsertInto.rules[normalizedNewIndex - 2] ??\n          combinatorSucceeding ??\n          getFirstOption(combinators);\n        insertRuleOrGroup(normalizedNewIndex, newPrevCombinator, rorg);\n      }\n    }\n  }\n\n  return query;\n};\n\n/**\n * Options for {@link group}.\n *\n * @group Query Tools\n */\nexport interface GroupOptions extends QueryToolOptions {\n  /**\n   * When `true`, the source rule/group will not be removed from its original path.\n   */\n  clone?: boolean;\n  /**\n   * If the query extends `RuleGroupTypeIC` (i.e. the query is using independent\n   * combinators), then the first combinator in this list will be inserted between\n   * the two rules/groups.\n   */\n  combinators?: OptionList;\n  /**\n   * ID generator.\n   */\n  idGenerator?: () => string;\n}\n\nexport interface GroupMethod {\n  <RG extends RuleGroupTypeAny>(\n    /** The query to update. */\n    query: RG,\n    /** Path of the rule/group to move or clone. */\n    sourcePathOrID: Path | string,\n    /** Path of the target rule/group, which will become the path of the new group. */\n    targetPathOrID: Path | string,\n    /** Options. */\n    options?: GroupOptions\n  ): RG;\n}\n\n/**\n * Creates a new group at a target path with its `rules` array containing the current\n * objects at the target path and the source path without mutating the original query.\n * In the options parameter, pass `{ clone: true }` to copy the source rule/group instead of move.\n *\n * @returns A new query with the rules or groups grouped.\n *\n * @group Query Tools\n */\nexport const group: GroupMethod = (\n  query,\n  sourcePathOrID,\n  targetPathOrID,\n  options = {}\n): typeof query =>\n  producerFor(options.freeze)(query, q => groupInPlace(q, sourcePathOrID, targetPathOrID, options));\n\n/**\n * Creates a new group at a target path with its `rules` array containing the current\n * objects at the target path and the source path in place.\n * In the options parameter, pass `{ clone: true }` to copy the source rule/group instead of move.\n *\n * @returns The query (mutated in place) with the rules or groups grouped.\n *\n * @group Query Tools\n */\nexport const groupInPlace: GroupMethod = (\n  query,\n  sourcePathOrID,\n  targetPathOrID,\n  options = {}\n): typeof query => {\n  const {\n    clone = false,\n    combinators = defaultCombinators,\n    idGenerator = generateID,\n    onAbort,\n  } = options;\n  const sourcePath = Array.isArray(sourcePathOrID)\n    ? sourcePathOrID\n    : getPathOfID(sourcePathOrID, query);\n  const targetPath = Array.isArray(targetPathOrID)\n    ? targetPathOrID\n    : getPathOfID(targetPathOrID, query);\n\n  // Ignore invalid paths/ids\n  if (!sourcePath) {\n    onAbort?.({ reason: 'target-not-found', operation: 'group', pathOrID: sourcePathOrID });\n    return query;\n  }\n\n  if (!targetPath) {\n    onAbort?.({ reason: 'target-not-found', operation: 'group', pathOrID: targetPathOrID });\n    return query;\n  }\n\n  const nextPath = getNextPath(query, sourcePath, targetPath);\n\n  // Can't group the root group\n  if (sourcePath.length === 0) {\n    onAbort?.({ reason: 'root-not-allowed', operation: 'group', pathOrID: sourcePathOrID });\n    return query;\n  }\n\n  const groupGuardReason = getGuardAbortReason(query, sourcePath, options);\n  if (groupGuardReason) {\n    onAbort?.({ reason: groupGuardReason, operation: 'group', pathOrID: sourcePathOrID });\n    return query;\n  }\n\n  // Don't move to the same location\n  if (pathsAreEqual(sourcePath, nextPath)) {\n    onAbort?.({ reason: 'same-location', operation: 'group', pathOrID: sourcePathOrID });\n    return query;\n  }\n\n  // Don't move to a path that doesn't exist yet\n  if (!findPath(getParentPath(nextPath), query)) {\n    onAbort?.({ reason: 'destination-not-found', operation: 'group', pathOrID: targetPathOrID });\n    return query;\n  }\n\n  // Don't group a group with something inside itself. See the equivalent check in `moveInPlace`.\n  if (!clone && isAncestor(sourcePath, nextPath)) {\n    onAbort?.({ reason: 'destination-not-found', operation: 'group', pathOrID: targetPathOrID });\n    return query;\n  }\n\n  const sourceRuleOrGroupOriginal = findPath(sourcePath, query);\n  const targetRuleOrGroup = findPath(targetPath, query);\n  if (!sourceRuleOrGroupOriginal) {\n    onAbort?.({ reason: 'target-not-found', operation: 'group', pathOrID: sourcePathOrID });\n    return query;\n  }\n\n  if (!targetRuleOrGroup) {\n    onAbort?.({ reason: 'target-not-found', operation: 'group', pathOrID: targetPathOrID });\n    return query;\n  }\n  const sourceRuleOrGroup = clone\n    ? regenerateIDs(\n        isDraft(sourceRuleOrGroupOriginal)\n          ? current(sourceRuleOrGroupOriginal)\n          : sourceRuleOrGroupOriginal,\n        { idGenerator }\n      )\n    : sourceRuleOrGroupOriginal;\n\n  const independentCombinators = isRuleGroupTypeIC(query);\n  const parentOfRuleToRemove = findPath(getParentPath(sourcePath), query) as typeof query;\n  const ruleToRemoveIndex = sourcePath.at(-1)!;\n\n  // Remove the source item if not cloning\n  if (!clone) {\n    const idxStartDelete = independentCombinators\n      ? Math.max(0, ruleToRemoveIndex - 1)\n      : ruleToRemoveIndex;\n    const deleteLength = independentCombinators ? 2 : 1;\n    parentOfRuleToRemove.rules.splice(idxStartDelete, deleteLength);\n  }\n\n  const newNewPath = [...nextPath];\n  const commonAncestorPath = getCommonAncestorPath(sourcePath, nextPath);\n  if (\n    !clone &&\n    sourcePath.length === commonAncestorPath.length + 1 &&\n    nextPath[commonAncestorPath.length] > sourcePath[commonAncestorPath.length]\n  ) {\n    // Getting here means there will be a shift of paths upward at the common\n    // ancestor level because the object at `oldPath` will be spliced out. The\n    // real new path should therefore be one or two higher than `newPathCalc`.\n    newNewPath[commonAncestorPath.length] -= independentCombinators ? 2 : 1;\n  }\n  const newNewParentPath = getParentPath(newNewPath);\n  const parentOfTargetPath = findPath(newNewParentPath, query) as typeof query;\n  const targetPathIndex = newNewPath.at(-1)!;\n\n  // Convert the target path to a group and insert the source and target items as children\n  parentOfTargetPath.rules.splice(\n    targetPathIndex,\n    1,\n    prepareRuleOrGroup(\n      (independentCombinators\n        ? { rules: [targetRuleOrGroup, getFirstOption(combinators), sourceRuleOrGroup] }\n        : {\n            combinator: getFirstOption(combinators),\n            rules: [targetRuleOrGroup, sourceRuleOrGroup],\n            // oxlint-disable-next-line typescript/no-explicit-any\n          }) as any,\n      { idGenerator }\n      // oxlint-disable-next-line typescript/no-explicit-any\n    ) as any\n  );\n\n  return query;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA8JA,MAAa,uBACX,OACA,UACA,SAAuB,CAAC,GACxB,EAAE,WAAW,UAAkC,CAAC,MACzB;CACvB,IAAI,OAAO,eAAe,OAAO,WAAW,oBAAoB;CAEhE,IAAI,CAAC,OAAO,mBAAmB,aAAa,KAAA,GAAW,OAAO;CAE9D,MAAM,OAAO,MAAM,QAAQ,QAAQ,IAAI,WAAW,YAAY,UAAU,KAAK;CAC7E,IAAI,SAAS,eAAe,MAAM,KAAK,KAAK,sBAAsB,MAAM,OAAO,aAAa,IAC1F,OAAO,WAAW,oBAAoB;CAGxC,OAAO;AACT;;;;;;AAOA,MAAa,oBACX,YACA,EAAE,YAAY,UAA2B,CAAC,MAC9B,CAAC,CAAC,cAAc,WAAW,UAAU;;;;;;;;AA+CnD,MAAa,OAAkB,OAAO,aAAa,gBAAgB,UAAU,CAAC,MAC5E,YAAY,QAAQ,MAAM,CAAC,CAAC,QAAO,MACjC,WAAW,GAAG,aAAyB,gBAAgB,OAAO,CAChE;;;;;;;;AASF,MAAa,cACX,OACA,aACA,gBACA,UAAU,CAAC,MACM;CACjB,MAAM,EACJ,cAAc,oBACd,qBACA,cAAc,YACd,YACE;CACJ,MAAM,SAAS,MAAM,QAAQ,cAAc,IACvC,SAAS,gBAAgB,KAAK,IAC9B,OAAO,gBAAgB,KAAK;CAEhC,IAAI,CAAC,QAAQ;EACX,UAAU;GAAE,QAAQ;GAAoB,WAAW;GAAO,UAAU;EAAe,CAAC;EACpF,OAAO;CACT;CAEA,IAAI,CA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