function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
export var normalizeRules = function normalizeRules(linkageRules) {
  if (Array.isArray(linkageRules)) return _toConsumableArray(linkageRules);
  if (linkageRules && _typeof(linkageRules) === 'object') return Object.values(linkageRules);
  return [];
};
export var getSelectedSet = function getSelectedSet(val) {
  var result = new Set();
  if (val == null) return result;
  if (Array.isArray(val)) {
    val.forEach(function (item) {
      if (item && _typeof(item) === 'object') {
        if (item.value !== undefined) result.add(item.value);
      } else {
        result.add(item);
      }
    });
  } else if (_typeof(val) === 'object') {
    if (val.value !== undefined) result.add(val.value);
  } else {
    result.add(val);
  }
  return result;
};
var evalCond = function evalCond(cond, valuesMap) {
  var _cond$groupId;
  var gid = String((_cond$groupId = cond.groupId) !== null && _cond$groupId !== void 0 ? _cond$groupId : '');
  var selSet = getSelectedSet(valuesMap[gid]);
  var op = function () {
    if (cond.op) return cond.op;
    if (Array.isArray(cond.in)) return 'in';
    return 'in';
  }();
  if (op === 'in') {
    var _ref, _cond$values;
    var arr = (_ref = (_cond$values = cond.values) !== null && _cond$values !== void 0 ? _cond$values : cond.in) !== null && _ref !== void 0 ? _ref : [];
    return arr.some(function (v) {
      return selSet.has(v);
    });
  }
  if (op === 'notIn') {
    var _ref2, _cond$values2;
    var _arr = (_ref2 = (_cond$values2 = cond.values) !== null && _cond$values2 !== void 0 ? _cond$values2 : cond.in) !== null && _ref2 !== void 0 ? _ref2 : [];
    return _arr.every(function (v) {
      return !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;
    var size = selSet.size;
    var min = (_cond$min = cond.min) !== null && _cond$min !== void 0 ? _cond$min : Number.NEGATIVE_INFINITY;
    var 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;
};
var isCond = function isCond(item) {
  return item.groupId !== undefined;
};
export var evaluateExpr = function evaluateExpr(expr, valuesMap) {
  var _expr$allOf, _expr$anyOf, _expr$noneOf;
  if (!expr) return true;
  var allList = (_expr$allOf = expr.allOf) !== null && _expr$allOf !== void 0 ? _expr$allOf : expr.and;
  var anyList = (_expr$anyOf = expr.anyOf) !== null && _expr$anyOf !== void 0 ? _expr$anyOf : expr.or;
  var noneList = (_expr$noneOf = expr.noneOf) !== null && _expr$noneOf !== void 0 ? _expr$noneOf : expr.not;
  var evalItem = function evalItem(item) {
    if (isCond(item)) return evalCond(item, valuesMap);
    return evaluateExpr(item, valuesMap);
  };
  var everyTrue = function everyTrue(list) {
    if (!Array.isArray(list) || list.length === 0) return true;
    return list.every(function (it) {
      return evalItem(it);
    });
  };
  var someTrue = function someTrue(list) {
    if (!Array.isArray(list) || list.length === 0) return true;
    return list.some(function (it) {
      return evalItem(it);
    });
  };
  var noneTrue = function noneTrue(list) {
    if (!Array.isArray(list) || list.length === 0) return true;
    return list.every(function (it) {
      return !evalItem(it);
    });
  };
  var okAll = everyTrue(allList);
  var okAny = someTrue(anyList);
  var okNone = noneTrue(noneList);
  return okAll && okAny && okNone;
};
export var computeAutoHiddenGroups = function computeAutoHiddenGroups(rules) {
  var set = new Set();
  rules.forEach(function (rule) {
    var thenActions = Array.isArray(rule === null || rule === void 0 ? void 0 : rule.then) ? rule.then : [];
    var elseActions = Array.isArray(rule === null || rule === void 0 ? void 0 : rule.elseThen) ? rule.elseThen : [];
    var actions = [].concat(_toConsumableArray(thenActions), _toConsumableArray(elseActions));
    actions.forEach(function (act) {
      if ((act === null || act === void 0 ? void 0 : act.type) === 'show') {
        var _act$groupId;
        var 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;
};
export var computeEffects = function computeEffects() {
  var dataSource = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var rawRules = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  var valuesMap = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  var autoHiddenGroups = computeAutoHiddenGroups(rawRules);
  var visibleGroups = {};
  (dataSource || []).forEach(function (group) {
    var _group$id;
    var gid = String((_group$id = group.id) !== null && _group$id !== void 0 ? _group$id : '');
    visibleGroups[gid] = autoHiddenGroups.has(gid) ? false : true;
  });
  var optionEffects = {};

  // 优先级调整：数字越大，优先级越高（越先执行）
  var rules = _toConsumableArray(rawRules).sort(function (a, b) {
    return ((b === null || b === void 0 ? void 0 : b.priority) || 0) - ((a === null || a === void 0 ? void 0 : a.priority) || 0);
  });
  for (var i = 0; i < rules.length; i++) {
    var rule = rules[i];
    var hit = evaluateExpr(rule === null || rule === void 0 ? void 0 : rule.when, valuesMap);
    var actions = 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 : [];
    actions.forEach(function (act) {
      if ((act === null || act === void 0 ? void 0 : act.type) === 'show') {
        var _act$groupId2;
        var 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;
        var _gid = String((_act$groupId3 = act === null || act === void 0 ? void 0 : act.groupId) !== null && _act$groupId3 !== void 0 ? _act$groupId3 : '');
        if (!_gid) return;
        var prev = ((_optionEffects$_gid = optionEffects[_gid]) === null || _optionEffects$_gid === void 0 ? void 0 : _optionEffects$_gid.allowOnly) || new Set();
        var values = (act === null || act === void 0 ? void 0 : act.values) || [];
        var merged = new Set([].concat(_toConsumableArray(prev), _toConsumableArray(values)));
        optionEffects[_gid] = _objectSpread(_objectSpread({}, optionEffects[_gid] || {}), {}, {
          allowOnly: merged
        });
      } else if ((act === null || act === void 0 ? void 0 : act.type) === 'requireOneOf') {
        var _act$groupId4;
        var _gid2 = String((_act$groupId4 = act === null || act === void 0 ? void 0 : act.groupId) !== null && _act$groupId4 !== void 0 ? _act$groupId4 : '');
        if (!_gid2) return;
        var _values = new Set((act === null || act === void 0 ? void 0 : act.values) || []);
        optionEffects[_gid2] = _objectSpread(_objectSpread({}, optionEffects[_gid2] || {}), {}, {
          requireOneOf: {
            values: _values,
            message: act === null || act === void 0 ? void 0 : act.message
          }
        });
      }
    });
    if (hit && rule !== null && rule !== void 0 && rule.stopAfterApply) break;
  }
  return {
    visibleGroups: visibleGroups,
    optionEffects: optionEffects
  };
};

// 根据 allowOnly 规则清理不允许的已选值（可选的硬约束行为）
export var pruneValuesByAllowOnly = function pruneValuesByAllowOnly(valuesMap, effects) {
  var next = _objectSpread({}, valuesMap);
  Object.keys(effects).forEach(function (gid) {
    var _effects$gid;
    var allowed = (_effects$gid = effects[gid]) === null || _effects$gid === void 0 ? void 0 : _effects$gid.allowOnly;
    if (!allowed || allowed.size === 0) return;
    var current = next[gid];
    if (current == null) return;
    var pruneArray = function pruneArray(arr) {
      return arr.filter(function (item) {
        if (item && _typeof(item) === 'object') {
          return allowed.has(item.value);
        }
        return allowed.has(item);
      });
    };
    if (Array.isArray(current)) {
      var pruned = pruneArray(current);
      next[gid] = pruned;
    } else if (_typeof(current) === 'object') {
      var hasVal = current.value !== undefined;
      if (hasVal) {
        next[gid] = allowed.has(current.value) ? current : undefined;
      } else {
        next[gid] = current;
      }
    } else {
      next[gid] = allowed.has(current) ? current : undefined;
    }
  });
  return next;
};

// 简版 UUID v4 生成器
export var generateSimpleUUID = function generateSimpleUUID() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0;
    var v = c === 'x' ? r : r & 0x3 | 0x8;
    return v.toString(16);
  });
};