import { n as isRuleGroupType, t as isRuleGroup } from "./isRuleGroup-DztIOOKa.mjs";
import { produce } from "immer";

//#region src/utils/transformQuery.ts
/**
* Recursively steps through a query object ({@link index!RuleGroupType RuleGroupType} or {@link index!RuleGroupTypeIC RuleGroupTypeIC}),
* passing each {@link index!RuleType RuleType} object to a provided `ruleProcessor` function and returning a
* new query object if there were any referential changes.
*
* @module transformQuery
*/
const remapProperties = (obj, propertyMap, deleteRemappedProperties) => produce(obj, (draft) => {
	for (const [k, v] of Object.entries(propertyMap)) if (v === false) delete draft[k];
	else if (!!v && k !== v && k in draft) {
		draft[v] = draft[k];
		if (deleteRemappedProperties) delete draft[k];
	}
});
function transformQuery(query, options = {}) {
	const { ruleProcessor = (r) => r, ruleGroupProcessor = (rg) => rg, propertyMap = {}, combinatorMap = {}, operatorMap = {}, omitPath = false, deleteRemappedProperties = true } = options;
	const processGroup = (rg) => ({
		...ruleGroupProcessor(remapProperties({
			...rg,
			...isRuleGroupType(rg) ? { combinator: combinatorMap[rg.combinator] ?? rg.combinator } : {}
		}, propertyMap, deleteRemappedProperties)),
		...propertyMap["rules"] === false ? null : { [propertyMap["rules"] ?? "rules"]: rg.rules.map((r, idx) => {
			const pathObject = omitPath ? null : { path: [...rg.path, idx] };
			if (typeof r === "string") return combinatorMap[r] ?? r;
			else if (isRuleGroup(r)) return processGroup({
				...r,
				...pathObject
			});
			return ruleProcessor(remapProperties({
				...r,
				...pathObject,
				..."operator" in r ? { operator: operatorMap[r.operator] ?? r.operator } : {}
			}, propertyMap, deleteRemappedProperties));
		}) }
	});
	return processGroup({
		...query,
		...omitPath ? null : { path: [] }
	});
}

//#endregion
export { transformQuery as t };
//# sourceMappingURL=transformQuery-DUpbpqjX.mjs.map