var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/pro/Selector/utils/index.ts
var utils_exports = {};
__export(utils_exports, {
  computeAutoHiddenGroups: () => computeAutoHiddenGroups,
  computeEffects: () => computeEffects,
  evaluateExpr: () => evaluateExpr,
  generateSimpleUUID: () => generateSimpleUUID,
  getSelectedSet: () => getSelectedSet,
  normalizeRules: () => normalizeRules,
  pruneValuesByAllowOnly: () => pruneValuesByAllowOnly
});
module.exports = __toCommonJS(utils_exports);
var normalizeRules = (linkageRules) => {
  if (Array.isArray(linkageRules)) return [...linkageRules];
  if (linkageRules && typeof linkageRules === "object")
    return Object.values(linkageRules);
  return [];
};
var 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;
};
var evalCond = (cond, valuesMap) => {
  const gid = String(cond.groupId ?? "");
  const selSet = getSelectedSet(valuesMap[gid]);
  const op = (() => {
    if (cond.op) return cond.op;
    if (Array.isArray(cond.in)) return "in";
    return "in";
  })();
  if (op === "in") {
    const arr = cond.values ?? cond.in ?? [];
    return arr.some((v) => selSet.has(v));
  }
  if (op === "notIn") {
    const arr = cond.values ?? cond.in ?? [];
    return arr.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") {
    const size = selSet.size;
    const min = cond.min ?? Number.NEGATIVE_INFINITY;
    const max = cond.max ?? Number.POSITIVE_INFINITY;
    return size >= min && size <= max;
  }
  if (op === "exists") {
    return selSet.size > 0;
  }
  return false;
};
var isCond = (item) => {
  return item.groupId !== void 0;
};
var evaluateExpr = (expr, valuesMap) => {
  if (!expr) return true;
  const allList = expr.allOf ?? expr.and;
  const anyList = expr.anyOf ?? expr.or;
  const noneList = 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;
};
var computeAutoHiddenGroups = (rules) => {
  const set = /* @__PURE__ */ new Set();
  rules.forEach((rule) => {
    const thenActions = Array.isArray(rule == null ? void 0 : rule.then) ? rule.then : [];
    const elseActions = Array.isArray(rule == null ? void 0 : rule.elseThen) ? rule.elseThen : [];
    const actions = [...thenActions, ...elseActions];
    actions.forEach((act) => {
      if ((act == null ? void 0 : act.type) === "show") {
        const gid = String((act == null ? void 0 : act.groupId) ?? "");
        if (gid) set.add(gid);
      }
    });
  });
  return set;
};
var computeEffects = (dataSource = [], rawRules = [], valuesMap = {}) => {
  const autoHiddenGroups = computeAutoHiddenGroups(rawRules);
  const visibleGroups = {};
  (dataSource || []).forEach((group) => {
    const gid = String(group.id ?? "");
    visibleGroups[gid] = autoHiddenGroups.has(gid) ? false : true;
  });
  const optionEffects = {};
  const rules = [...rawRules].sort((a, b) => ((b == null ? void 0 : b.priority) || 0) - ((a == null ? void 0 : a.priority) || 0));
  for (let i = 0; i < rules.length; i++) {
    const rule = rules[i];
    const hit = evaluateExpr(rule == null ? void 0 : rule.when, valuesMap);
    const actions = hit ? Array.isArray(rule == null ? void 0 : rule.then) ? rule.then : [] : Array.isArray(rule == null ? void 0 : rule.elseThen) ? rule.elseThen : [];
    actions.forEach((act) => {
      var _a;
      if ((act == null ? void 0 : act.type) === "show") {
        const gid = String((act == null ? void 0 : act.groupId) ?? "");
        if (gid) {
          visibleGroups[gid] = true;
        }
      } else if ((act == null ? void 0 : act.type) === "allowOnly") {
        const gid = String((act == null ? void 0 : act.groupId) ?? "");
        if (!gid) return;
        const prev = ((_a = optionEffects[gid]) == null ? void 0 : _a.allowOnly) || /* @__PURE__ */ new Set();
        const values = (act == null ? void 0 : act.values) || [];
        const merged = /* @__PURE__ */ new Set([...prev, ...values]);
        optionEffects[gid] = { ...optionEffects[gid] || {}, allowOnly: merged };
      } else if ((act == null ? void 0 : act.type) === "requireOneOf") {
        const gid = String((act == null ? void 0 : act.groupId) ?? "");
        if (!gid) return;
        const values = new Set((act == null ? void 0 : act.values) || []);
        optionEffects[gid] = {
          ...optionEffects[gid] || {},
          requireOneOf: { values, message: act == null ? void 0 : act.message }
        };
      }
    });
    if (hit && (rule == null ? void 0 : rule.stopAfterApply)) break;
  }
  return { visibleGroups, optionEffects };
};
var pruneValuesByAllowOnly = (valuesMap, effects) => {
  const next = { ...valuesMap };
  Object.keys(effects).forEach((gid) => {
    var _a;
    const allowed = (_a = effects[gid]) == null ? void 0 : _a.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)) {
      const pruned = pruneArray(current);
      next[gid] = pruned;
    } else if (typeof current === "object") {
      const hasVal = current.value !== void 0;
      if (hasVal) {
        next[gid] = allowed.has(current.value) ? current : void 0;
      } else {
        next[gid] = current;
      }
    } else {
      next[gid] = allowed.has(current) ? current : void 0;
    }
  });
  return next;
};
var generateSimpleUUID = () => {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
    const r = Math.random() * 16 | 0;
    const v = c === "x" ? r : r & 3 | 8;
    return v.toString(16);
  });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  computeAutoHiddenGroups,
  computeEffects,
  evaluateExpr,
  generateSimpleUUID,
  getSelectedSet,
  normalizeRules,
  pruneValuesByAllowOnly
});
