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/model/strategy/adapter/itemRule/evaluator.ts
var evaluator_exports = {};
__export(evaluator_exports, {
  ItemRuleEvaluator: () => ItemRuleEvaluator
});
module.exports = __toCommonJS(evaluator_exports);
var import_engine = require("../../engine");
var import_adapter = require("./adapter");
var import_type = require("./type");
var ItemRuleEvaluator = class {
  constructor() {
    this.strategyConfigs = [];
    this.engine = new import_engine.StrategyEngine({
      debug: false,
      enableTrace: false
    });
    this.adapter = new import_adapter.ItemRuleAdapter();
  }
  /**
   * 设置策略配置列表
   * 自动过滤 metadata.type === 'item_rule' 的配置
   */
  setStrategyConfigs(configs) {
    this.strategyConfigs = configs.filter(
      (c) => c.metadata.type === "item_rule"
    );
  }
  getStrategyConfigs() {
    return this.strategyConfigs;
  }
  /**
   * 完整评估：数量校验 + 预填购物车
   */
  evaluate(businessData) {
    const configs = businessData.strategyConfigs ? businessData.strategyConfigs.filter((c) => c.metadata.type === "item_rule") : this.strategyConfigs;
    const allQuantityChecks = [];
    const allPrefillItems = [];
    const rawResults = [];
    const context = this.adapter.prepareContext(businessData);
    for (const config of configs) {
      const evalResult = this.engine.evaluate(config, context);
      rawResults.push(evalResult);
      const { quantityChecks, prefillItems } = this.adapter.transformResult(
        evalResult,
        businessData
      );
      allQuantityChecks.push(...quantityChecks);
      allPrefillItems.push(...prefillItems);
    }
    const failedChecks = allQuantityChecks.filter((c) => !c.passed);
    return {
      quantityChecks: allQuantityChecks,
      prefillItems: allPrefillItems,
      allPassed: failedChecks.length === 0,
      failedChecks,
      rawResults
    };
  }
  /**
   * 获取当前配置中各商品的数量限制
   *
   * 不做购物车校验，仅从策略配置中提取 QUANTITY_CHECK 规则的
   * min / max / exact / mustInclude 限制值。
   * 会评估条件是否满足、根据 scope + submissionIndex 过滤。
   */
  getQuantityLimits(businessData) {
    const configs = businessData.strategyConfigs ? businessData.strategyConfigs.filter((c) => c.metadata.type === "item_rule") : this.strategyConfigs;
    const results = [];
    const context = this.adapter.prepareContext(businessData);
    for (const config of configs) {
      const evalResult = this.engine.evaluate(config, context);
      if (!evalResult.applicable)
        continue;
      for (const action of evalResult.matchedActions) {
        if (action.type !== import_type.ITEM_RULE_ACTION_TYPES.QUANTITY_CHECK)
          continue;
        const limit = this.adapter.extractQuantityLimits(action, businessData);
        if (limit) {
          results.push(limit);
        }
      }
    }
    return results;
  }
  /**
   * 便捷方法：仅做数量校验，返回是否全部通过及失败详情
   */
  validateCart(businessData) {
    const result = this.evaluate(businessData);
    return {
      passed: result.allPassed,
      failures: result.failedChecks
    };
  }
  /**
   * 便捷方法：仅获取需要预填到购物车的商品
   */
  getPrefillItems(businessData) {
    const result = this.evaluate(businessData);
    return result.prefillItems;
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ItemRuleEvaluator
});
