import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
//#region src/pro/Selector/utils/index.ts
const normalizeRules = (linkageRules) => {
	if (Array.isArray(linkageRules)) return [...linkageRules];
	if (linkageRules && typeof linkageRules === "object") return Object.values(linkageRules);
	return [];
};
const getSelectedSet = (val) => {
	const result = /* @__PURE__ */ new Set();
	if (val == null) return result;
	if (Array.isArray(val)) val.forEach((item) => {
		if (item && typeof item === "object") {
			if (item.value !== void 0) result.add(item.value);
		} else result.add(item);
	});
	else if (typeof val === "object") {
		if (val.value !== void 0) result.add(val.value);
	} else result.add(val);
	return result;
};
const evalCond = (cond, valuesMap) => {
	var _cond$groupId;
	const selSet = getSelectedSet(valuesMap[String((_cond$groupId = cond.groupId) !== null && _cond$groupId !== void 0 ? _cond$groupId : "")]);
	const op = (() => {
		if (cond.op) return cond.op;
		if (Array.isArray(cond.in)) return "in";
		return "in";
	})();
	if (op === "in") {
		var _ref, _cond$values;
		return ((_ref = (_cond$values = cond.values) !== null && _cond$values !== void 0 ? _cond$values : cond.in) !== null && _ref !== void 0 ? _ref : []).some((v) => selSet.has(v));
	}
	if (op === "notIn") {
		var _ref2, _cond$values2;
		return ((_ref2 = (_cond$values2 = cond.values) !== null && _cond$values2 !== void 0 ? _cond$values2 : cond.in) !== null && _ref2 !== void 0 ? _ref2 : []).every((v) => !selSet.has(v));
	}
	if (op === "eq" || op === "contains") return selSet.has(cond.value);
	if (op === "neq") return !selSet.has(cond.value);
	if (op === "range") {
		var _cond$min, _cond$max;
		const size = selSet.size;
		const min = (_cond$min = cond.min) !== null && _cond$min !== void 0 ? _cond$min : Number.NEGATIVE_INFINITY;
		const max = (_cond$max = cond.max) !== null && _cond$max !== void 0 ? _cond$max : Number.POSITIVE_INFINITY;
		return size >= min && size <= max;
	}
	if (op === "exists") return selSet.size > 0;
	return false;
};
const isCond = (item) => {
	return item.groupId !== void 0;
};
const evaluateExpr = (expr, valuesMap) => {
	var _expr$allOf, _expr$anyOf, _expr$noneOf;
	if (!expr) return true;
	const allList = (_expr$allOf = expr.allOf) !== null && _expr$allOf !== void 0 ? _expr$allOf : expr.and;
	const anyList = (_expr$anyOf = expr.anyOf) !== null && _expr$anyOf !== void 0 ? _expr$anyOf : expr.or;
	const noneList = (_expr$noneOf = expr.noneOf) !== null && _expr$noneOf !== void 0 ? _expr$noneOf : expr.not;
	const evalItem = (item) => {
		if (isCond(item)) return evalCond(item, valuesMap);
		return evaluateExpr(item, valuesMap);
	};
	const everyTrue = (list) => {
		if (!Array.isArray(list) || list.length === 0) return true;
		return list.every((it) => evalItem(it));
	};
	const someTrue = (list) => {
		if (!Array.isArray(list) || list.length === 0) return true;
		return list.some((it) => evalItem(it));
	};
	const noneTrue = (list) => {
		if (!Array.isArray(list) || list.length === 0) return true;
		return list.every((it) => !evalItem(it));
	};
	const okAll = everyTrue(allList);
	const okAny = someTrue(anyList);
	const okNone = noneTrue(noneList);
	return okAll && okAny && okNone;
};
const computeAutoHiddenGroups = (rules) => {
	const set = /* @__PURE__ */ new Set();
	rules.forEach((rule) => {
		const thenActions = Array.isArray(rule === null || rule === void 0 ? void 0 : rule.then) ? rule.then : [];
		const elseActions = Array.isArray(rule === null || rule === void 0 ? void 0 : rule.elseThen) ? rule.elseThen : [];
		[...thenActions, ...elseActions].forEach((act) => {
			if ((act === null || act === void 0 ? void 0 : act.type) === "show") {
				var _act$groupId;
				const gid = String((_act$groupId = act === null || act === void 0 ? void 0 : act.groupId) !== null && _act$groupId !== void 0 ? _act$groupId : "");
				if (gid) set.add(gid);
			}
		});
	});
	return set;
};
const computeEffects = (dataSource = [], rawRules = [], valuesMap = {}) => {
	const autoHiddenGroups = computeAutoHiddenGroups(rawRules);
	const visibleGroups = {};
	(dataSource || []).forEach((group) => {
		var _group$id;
		const gid = String((_group$id = group.id) !== null && _group$id !== void 0 ? _group$id : "");
		visibleGroups[gid] = autoHiddenGroups.has(gid) ? false : true;
	});
	const optionEffects = {};
	const rules = [...rawRules].sort((a, b) => ((b === null || b === void 0 ? void 0 : b.priority) || 0) - ((a === null || a === void 0 ? void 0 : a.priority) || 0));
	for (let i = 0; i < rules.length; i++) {
		const rule = rules[i];
		const hit = evaluateExpr(rule === null || rule === void 0 ? void 0 : rule.when, valuesMap);
		(hit ? Array.isArray(rule === null || rule === void 0 ? void 0 : rule.then) ? rule.then : [] : Array.isArray(rule === null || rule === void 0 ? void 0 : rule.elseThen) ? rule.elseThen : []).forEach((act) => {
			if ((act === null || act === void 0 ? void 0 : act.type) === "show") {
				var _act$groupId2;
				const gid = String((_act$groupId2 = act === null || act === void 0 ? void 0 : act.groupId) !== null && _act$groupId2 !== void 0 ? _act$groupId2 : "");
				if (gid) visibleGroups[gid] = true;
			} else if ((act === null || act === void 0 ? void 0 : act.type) === "allowOnly") {
				var _act$groupId3, _optionEffects$gid;
				const gid = String((_act$groupId3 = act === null || act === void 0 ? void 0 : act.groupId) !== null && _act$groupId3 !== void 0 ? _act$groupId3 : "");
				if (!gid) return;
				const prev = ((_optionEffects$gid = optionEffects[gid]) === null || _optionEffects$gid === void 0 ? void 0 : _optionEffects$gid.allowOnly) || /* @__PURE__ */ new Set();
				const values = (act === null || act === void 0 ? void 0 : act.values) || [];
				const merged = new Set([...prev, ...values]);
				optionEffects[gid] = _objectSpread2(_objectSpread2({}, optionEffects[gid] || {}), {}, { allowOnly: merged });
			} else if ((act === null || act === void 0 ? void 0 : act.type) === "requireOneOf") {
				var _act$groupId4;
				const gid = String((_act$groupId4 = act === null || act === void 0 ? void 0 : act.groupId) !== null && _act$groupId4 !== void 0 ? _act$groupId4 : "");
				if (!gid) return;
				const values = new Set((act === null || act === void 0 ? void 0 : act.values) || []);
				optionEffects[gid] = _objectSpread2(_objectSpread2({}, optionEffects[gid] || {}), {}, { requireOneOf: {
					values,
					message: act === null || act === void 0 ? void 0 : act.message
				} });
			}
		});
		if (hit && (rule === null || rule === void 0 ? void 0 : rule.stopAfterApply)) break;
	}
	return {
		visibleGroups,
		optionEffects
	};
};
const pruneValuesByAllowOnly = (valuesMap, effects) => {
	const next = _objectSpread2({}, valuesMap);
	Object.keys(effects).forEach((gid) => {
		var _effects$gid;
		const allowed = (_effects$gid = effects[gid]) === null || _effects$gid === void 0 ? void 0 : _effects$gid.allowOnly;
		if (!allowed || allowed.size === 0) return;
		const current = next[gid];
		if (current == null) return;
		const pruneArray = (arr) => {
			return arr.filter((item) => {
				if (item && typeof item === "object") return allowed.has(item.value);
				return allowed.has(item);
			});
		};
		if (Array.isArray(current)) next[gid] = pruneArray(current);
		else if (typeof current === "object") if (current.value !== void 0) next[gid] = allowed.has(current.value) ? current : void 0;
		else next[gid] = current;
		else next[gid] = allowed.has(current) ? current : void 0;
	});
	return next;
};
const generateSimpleUUID = () => {
	return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
		const r = Math.random() * 16 | 0;
		return (c === "x" ? r : r & 3 | 8).toString(16);
	});
};
//#endregion
export { computeAutoHiddenGroups, computeEffects, generateSimpleUUID, getSelectedSet, normalizeRules, pruneValuesByAllowOnly };
