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 _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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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 _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; }
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
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); }
import Decimal from 'decimal.js';
import { StrategyEngine } from "../../index";
import { PromotionAdapter } from "./index";
import { PROMOTION_ACTION_TYPES } from "./type";

// ============================================
// 类型定义
// ============================================

// ============================================
// 多语言定义
// ============================================

var defaultLocales = {
  en: {
    no_applicable_promotion: 'No applicable promotion',
    promotion_not_in_time_range: 'Promotion not in valid time range',
    product_not_in_promotion: 'Product not in promotion scope'
  },
  'zh-CN': {
    no_applicable_promotion: '无适用的促销活动',
    promotion_not_in_time_range: '促销活动不在有效时间范围内',
    product_not_in_promotion: '商品不在促销范围内'
  },
  'zh-HK': {
    no_applicable_promotion: '無適用的促銷活動',
    promotion_not_in_time_range: '促銷活動不在有效時間範圍內',
    product_not_in_promotion: '商品不在促銷範圍內'
  }
};

// ============================================
// PromotionEvaluator 实现
// ============================================

/**
 * Promotion 评估器
 *
 * 用于评估商品适用的促销活动
 * 封装了 StrategyEngine 和 PromotionAdapter 的调用逻辑
 */
export var PromotionEvaluator = /*#__PURE__*/function () {
  function PromotionEvaluator() {
    _classCallCheck(this, PromotionEvaluator);
    _defineProperty(this, "engine", void 0);
    _defineProperty(this, "adapter", void 0);
    _defineProperty(this, "strategyConfigs", [{
      metadata: {
        id: 'STRATEGY_001',
        name: {
          en: '2 items for $10',
          'zh-CN': '2杯10元',
          'zh-HK': '2杯10元'
        },
        type: 'promotion',
        custom: {
          display: {
            product_card: {
              text: {
                en: '2 for $10',
                'zh-CN': '2杯10元',
                'zh-HK': '2杯10元'
              },
              type: 'tag'
            }
          }
        }
      },
      conditions: {
        operator: 'and',
        rules: [{
          type: 'operator',
          field: 'currentDateTime',
          value: '2023-01-01 00:00:00',
          operator: '>='
        }, {
          type: 'operator',
          field: 'currentDateTime',
          value: '2027-01-01 00:00:00',
          operator: '<='
        }, {
          type: 'operator',
          field: 'productIdAndVariantId',
          value: [{
            product_id: 60736,
            product_variant_id: 0 // 0 = 匹配任意变体
          }, {
            product_id: 60737,
            product_variant_id: 0 // 0 = 匹配任意变体
          }],
          operator: 'product_match'
        }],
        actionIds: ['1']
      },
      actions: [{
        id: '1',
        type: 'X_ITEMS_FOR_Y_PRICE',
        value: {
          x: 2,
          // 每X件
          price: 10 // 固定价格Y元
        },
        valueType: 'object',
        target: 'product',
        priority: 1,
        config: {
          allowCrossProduct: true,
          // 允许跨商品组合（A+B可以凑成一组）
          cumulative: true // 可累计（买5件 = 2组优惠 + 1件原价）
        }
      }]
    }]);
    _defineProperty(this, "locale", 'en');
    _defineProperty(this, "locales", defaultLocales);
    this.engine = new StrategyEngine({
      debug: false,
      enableTrace: false
    });
    this.adapter = new PromotionAdapter();
  }

  // ============================================
  // 配置管理
  // ============================================

  /**
   * 设置策略配置列表
   */
  _createClass(PromotionEvaluator, [{
    key: "setStrategyConfigs",
    value: function setStrategyConfigs(strategyConfigs) {
      this.strategyConfigs = strategyConfigs;
    }

    /**
     * 获取策略配置列表
     */
  }, {
    key: "getStrategyConfigs",
    value: function getStrategyConfigs() {
      return this.strategyConfigs;
    }

    /**
     * 添加策略配置
     */
  }, {
    key: "addStrategyConfig",
    value: function addStrategyConfig(strategyConfig) {
      this.strategyConfigs.push(strategyConfig);
    }

    /**
     * 设置语言
     */
  }, {
    key: "setLocale",
    value: function setLocale(locale) {
      this.locale = locale;
    }

    /**
     * 设置自定义多语言文案
     */
  }, {
    key: "setLocales",
    value: function setLocales(locales) {
      this.locales = _objectSpread(_objectSpread({}, defaultLocales), locales);
    }

    /**
     * 获取多语言文案
     */
  }, {
    key: "getText",
    value: function getText(key) {
      var _this$locales;
      return ((_this$locales = this.locales) === null || _this$locales === void 0 || (_this$locales = _this$locales[this.locale]) === null || _this$locales === void 0 ? void 0 : _this$locales[key]) || key;
    }

    /**
     * 获取多语言名称
     */
  }, {
    key: "getLocalizedName",
    value: function getLocalizedName(name) {
      if (typeof name === 'string') {
        return name;
      }
      return name[this.locale] || name['en'] || Object.values(name)[0] || '';
    }

    // ============================================
    // 核心评估方法
    // ============================================

    /**
     * 评估商品列表
     *
     * 判断每个商品适用哪些促销活动
     *
     * @param input 评估输入
     * @returns 每个商品的促销评估结果
     */
  }, {
    key: "evaluateProducts",
    value: function evaluateProducts(input) {
      var products = input.products,
        strategyConfigs = input.strategyConfigs,
        channel = input.channel;
      var configs = strategyConfigs || this.strategyConfigs;
      var results = [];
      var _iterator = _createForOfIteratorHelper(products),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var product = _step.value;
          var applicablePromotions = [];

          // 遍历所有策略，检查此商品是否适用
          var _iterator2 = _createForOfIteratorHelper(configs),
            _step2;
          try {
            for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
              var config = _step2.value;
              // 准备单商品上下文
              var context = this.adapter.prepareContext({
                products: [product],
                currentProduct: product,
                channel: channel
              });

              // 评估策略
              var result = this.engine.evaluate(config, context);
              if (result.applicable && result.matchedActions.length > 0) {
                var action = result.matchedActions[0];
                var transformedResult = this.adapter.transformResult(result, {
                  products: [product],
                  currentProduct: product,
                  channel: channel
                });
                if (transformedResult.actionDetail) {
                  applicablePromotions.push({
                    strategyId: config.metadata.id,
                    strategyName: config.metadata.name,
                    actionType: action.type,
                    actionDetail: transformedResult.actionDetail,
                    display: this.getDisplayConfig(config),
                    strategyConfig: config
                  });
                }
              }
            }
          } catch (err) {
            _iterator2.e(err);
          } finally {
            _iterator2.f();
          }
          results.push({
            product: product,
            applicablePromotions: applicablePromotions,
            hasPromotion: applicablePromotions.length > 0
          });
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
      return results;
    }

    /**
     * 评估购物车
     *
     * 返回所有适用的促销及按促销分组的商品
     *
     * @param input 评估输入
     * @returns 购物车评估结果
     */
  }, {
    key: "evaluateCart",
    value: function evaluateCart(input) {
      var products = input.products,
        strategyConfigs = input.strategyConfigs,
        channel = input.channel;
      var configs = strategyConfigs || this.strategyConfigs;

      // 1. 先获取每个商品的促销结果
      var productResults = this.evaluateProducts(input);

      // 2. 收集所有适用的促销（去重）
      var promotionMap = new Map();
      var _iterator3 = _createForOfIteratorHelper(productResults),
        _step3;
      try {
        for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
          var productResult = _step3.value;
          var _iterator4 = _createForOfIteratorHelper(productResult.applicablePromotions),
            _step4;
          try {
            for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
              var promo = _step4.value;
              var existing = promotionMap.get(promo.strategyId);
              if (existing) {
                // 已存在，添加商品到列表
                existing.applicableProducts.push(productResult.product);
                existing.totalQuantity += productResult.product.quantity;
                existing.totalAmount += productResult.product.price * productResult.product.quantity;
              } else {
                // 新促销
                promotionMap.set(promo.strategyId, _objectSpread(_objectSpread({}, promo), {}, {
                  applicableProducts: [productResult.product],
                  totalQuantity: productResult.product.quantity,
                  totalAmount: productResult.product.price * productResult.product.quantity
                }));
              }
            }
          } catch (err) {
            _iterator4.e(err);
          } finally {
            _iterator4.f();
          }
        }
      } catch (err) {
        _iterator3.e(err);
      } finally {
        _iterator3.f();
      }
      var applicablePromotions = Array.from(promotionMap.values());

      // 3. 按促销分组
      var promotionGroups = applicablePromotions.map(function (promo) {
        return {
          strategyId: promo.strategyId,
          strategyName: promo.strategyName,
          actionType: promo.actionType,
          actionDetail: promo.actionDetail,
          products: promo.applicableProducts,
          totalQuantity: promo.totalQuantity,
          totalAmount: promo.totalAmount
        };
      });
      return {
        productResults: productResults,
        applicablePromotions: applicablePromotions,
        promotionGroups: promotionGroups
      };
    }

    /**
     * 评估购物车并计算定价
     *
     * 返回处理后的商品数组（包含拆分、finalPrice）和赠品信息
     * - 对于 X_ITEMS_FOR_Y_PRICE：按原价比例均摊价格，优先使用高价商品
     * - 对于 BUY_X_GET_Y_FREE：计算赠品数量和可选赠品列表
     *
     * @param input 评估输入
     * @returns 带定价的购物车评估结果
     */
  }, {
    key: "evaluateCartWithPricing",
    value: function evaluateCartWithPricing(input) {
      var products = input.products;

      // 1. 先调用 evaluateCart 获取分组结果
      var cartResult = this.evaluateCart(input);

      // 2. 按 action priority 排序促销组（从 strategyConfig 获取 priority）
      var sortedGroups = this.sortPromotionGroupsByPriority(cartResult.promotionGroups, cartResult.applicablePromotions);

      // 3. 追踪已处理的商品（避免重复处理）
      // key: `${product_id}_${product_variant_id}`
      var processedQuantityMap = new Map();

      // 4. 处理每个促销组，收集结果
      var pricedProducts = [];
      var gifts = [];
      var totalOriginalAmount = new Decimal(0);
      var totalFinalAmount = new Decimal(0);
      var _iterator5 = _createForOfIteratorHelper(sortedGroups),
        _step5;
      try {
        for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
          var group = _step5.value;
          var actionType = group.actionType,
            actionDetail = group.actionDetail,
            strategyId = group.strategyId,
            strategyName = group.strategyName;
          if (actionType === PROMOTION_ACTION_TYPES.X_ITEMS_FOR_Y_PRICE) {
            // X件Y元处理
            var result = this.processXItemsForYPrice(group, processedQuantityMap);
            pricedProducts.push.apply(pricedProducts, _toConsumableArray(result.products));
            totalOriginalAmount = totalOriginalAmount.plus(result.originalAmount);
            totalFinalAmount = totalFinalAmount.plus(result.finalAmount);
          } else if (actionType === PROMOTION_ACTION_TYPES.BUY_X_GET_Y_FREE) {
            // 买X送Y处理
            var _result = this.processBuyXGetYFree(group, processedQuantityMap);
            pricedProducts.push.apply(pricedProducts, _toConsumableArray(_result.products));
            totalOriginalAmount = totalOriginalAmount.plus(_result.originalAmount);
            totalFinalAmount = totalFinalAmount.plus(_result.finalAmount);

            // 添加赠品信息
            if (_result.giftInfo) {
              gifts.push(_result.giftInfo);
            }
          }
        }

        // 5. 处理未参与任何促销的商品
      } catch (err) {
        _iterator5.e(err);
      } finally {
        _iterator5.f();
      }
      var remainingProducts = this.getRemainingProducts(products, processedQuantityMap);
      var _iterator6 = _createForOfIteratorHelper(remainingProducts),
        _step6;
      try {
        for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
          var product = _step6.value;
          var amount = new Decimal(product.price).mul(product.quantity);
          totalOriginalAmount = totalOriginalAmount.plus(amount);
          totalFinalAmount = totalFinalAmount.plus(amount);
        }
      } catch (err) {
        _iterator6.e(err);
      } finally {
        _iterator6.f();
      }
      pricedProducts.push.apply(pricedProducts, _toConsumableArray(remainingProducts));

      // 6. 计算总优惠金额
      var totalDiscount = totalOriginalAmount.minus(totalFinalAmount);
      return {
        products: pricedProducts,
        gifts: gifts,
        totalDiscount: Decimal.max(0, totalDiscount).toNumber()
      };
    }

    /**
     * 获取商品适用的促销列表
     *
     * 简化方法，用于商品卡片展示
     *
     * @param product 商品
     * @param strategyConfigs 策略配置（可选）
     * @returns 适用的促销列表
     */
  }, {
    key: "getProductPromotions",
    value: function getProductPromotions(product, strategyConfigs) {
      var _results$;
      var results = this.evaluateProducts({
        products: [product],
        strategyConfigs: strategyConfigs
      });
      return ((_results$ = results[0]) === null || _results$ === void 0 ? void 0 : _results$.applicablePromotions) || [];
    }

    /**
     * 获取商品的促销标签
     *
     * 用于商品卡片展示
     *
     * @param product 商品
     * @param strategyConfigs 策略配置（可选）
     * @returns 促销标签列表
     */
  }, {
    key: "getProductPromotionTags",
    value: function getProductPromotionTags(product, strategyConfigs) {
      var _this = this;
      var promotions = this.getProductPromotions(product, strategyConfigs);
      return promotions.filter(function (promo) {
        return promo.display;
      }).map(function (promo) {
        return {
          text: _this.getLocalizedName(promo.display.text),
          type: promo.display.type,
          strategyId: promo.strategyId
        };
      });
    }

    /**
     * 查找商品适用的策略配置
     *
     * @param product 商品
     * @param strategyConfigs 策略配置列表（可选）
     * @returns 适用的策略配置列表
     */
  }, {
    key: "findApplicableStrategies",
    value: function findApplicableStrategies(product, strategyConfigs) {
      var _this2 = this;
      var configs = strategyConfigs || this.strategyConfigs;
      return configs.filter(function (config) {
        return _this2.isProductInStrategy(product, config);
      });
    }

    /**
     * 批量获取商品列表的适用策略信息
     *
     * 用于给商品列表追加策略标签信息
     * 只检查商品 ID 匹配和策略时间范围
     *
     * @param input 评估输入（商品列表）
     * @returns 每个商品的适用策略完整数据
     */
  }, {
    key: "getProductsApplicableStrategies",
    value: function getProductsApplicableStrategies(input) {
      var products = input.products,
        strategyConfigs = input.strategyConfigs,
        channel = input.channel;
      var configs = strategyConfigs || this.strategyConfigs;
      var results = [];
      var _iterator7 = _createForOfIteratorHelper(products),
        _step7;
      try {
        for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
          var product = _step7.value;
          var applicableStrategies = [];

          // 遍历所有策略，检查此商品是否适用
          var _iterator8 = _createForOfIteratorHelper(configs),
            _step8;
          try {
            for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
              var config = _step8.value;
              // 准备单商品上下文
              var context = this.adapter.prepareContext({
                products: [product],
                currentProduct: product,
                channel: channel
              });

              // 评估策略
              var result = this.engine.evaluate(config, context);
              if (result.applicable && result.matchedActions.length > 0) {
                var action = result.matchedActions[0];
                var transformedResult = this.adapter.transformResult(result, {
                  products: [product],
                  currentProduct: product,
                  channel: channel
                });
                if (transformedResult.actionDetail) {
                  applicableStrategies.push({
                    strategyId: config.metadata.id,
                    strategyName: config.metadata.name,
                    strategyMetadata: config.metadata,
                    actionType: action.type,
                    actionDetail: transformedResult.actionDetail,
                    display: this.getDisplayConfig(config),
                    strategyConfig: config
                  });
                }
              }
            }
          } catch (err) {
            _iterator8.e(err);
          } finally {
            _iterator8.f();
          }
          results.push({
            product: product,
            applicableStrategies: applicableStrategies,
            hasApplicableStrategy: applicableStrategies.length > 0
          });
        }
      } catch (err) {
        _iterator7.e(err);
      } finally {
        _iterator7.f();
      }
      return results;
    }

    // ============================================
    // 私有辅助方法
    // ============================================

    /**
     * 按优先级排序促销组
     * 优先级从 strategyConfig.actions[0].priority 获取，数值越小优先级越高
     */
  }, {
    key: "sortPromotionGroupsByPriority",
    value: function sortPromotionGroupsByPriority(groups, promotions) {
      // 创建策略ID到优先级的映射
      var priorityMap = new Map();
      var _iterator9 = _createForOfIteratorHelper(promotions),
        _step9;
      try {
        for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
          var _promo$strategyConfig, _actions$0$priority, _actions$;
          var promo = _step9.value;
          var actions = ((_promo$strategyConfig = promo.strategyConfig) === null || _promo$strategyConfig === void 0 ? void 0 : _promo$strategyConfig.actions) || [];
          var priority = (_actions$0$priority = (_actions$ = actions[0]) === null || _actions$ === void 0 ? void 0 : _actions$.priority) !== null && _actions$0$priority !== void 0 ? _actions$0$priority : 999;
          priorityMap.set(promo.strategyId, priority);
        }

        // 按优先级排序（数值越小优先级越高）
      } catch (err) {
        _iterator9.e(err);
      } finally {
        _iterator9.f();
      }
      return _toConsumableArray(groups).sort(function (a, b) {
        var _priorityMap$get, _priorityMap$get2;
        var priorityA = (_priorityMap$get = priorityMap.get(a.strategyId)) !== null && _priorityMap$get !== void 0 ? _priorityMap$get : 999;
        var priorityB = (_priorityMap$get2 = priorityMap.get(b.strategyId)) !== null && _priorityMap$get2 !== void 0 ? _priorityMap$get2 : 999;
        return priorityA - priorityB;
      });
    }

    /**
     * 处理 X件Y元 促销
     *
     * 1. 展开商品为单件列表，按价格从高到低排序
     * 2. 计算可凑成的组数
     * 3. 按原价比例均摊价格
     * 4. 拆分商品（部分参与促销、部分原价）
     *
     * 注意：每个商品按 id 独立处理，不会合并不同 id 的商品
     */
  }, {
    key: "processXItemsForYPrice",
    value: function processXItemsForYPrice(group, processedQuantityMap) {
      var strategyId = group.strategyId,
        actionDetail = group.actionDetail,
        groupProducts = group.products;
      var detail = actionDetail;
      var x = detail.x,
        groupPrice = detail.price,
        cumulative = detail.cumulative;
      var result = [];
      var originalAmount = new Decimal(0);
      var finalAmount = new Decimal(0);

      // 1. 展开所有商品为单件列表（排除已处理的数量）
      // 每个单件记录来自哪个原商品（通过 product.id）
      var unitProducts = [];
      var _iterator10 = _createForOfIteratorHelper(groupProducts),
        _step10;
      try {
        for (_iterator10.s(); !(_step10 = _iterator10.n()).done;) {
          var product = _step10.value;
          var key = this.getProductKey(product);
          var processedQty = processedQuantityMap.get(key) || 0;
          var availableQty = product.quantity - processedQty;
          if (availableQty <= 0) continue;

          // 展开为单件，每个单件都记录来源商品
          for (var _i = 0; _i < availableQty; _i++) {
            unitProducts.push({
              product: product,
              price: product.price,
              productId: product.id
            });
          }
        }
      } catch (err) {
        _iterator10.e(err);
      } finally {
        _iterator10.f();
      }
      if (unitProducts.length === 0) {
        return {
          products: [],
          originalAmount: 0,
          finalAmount: 0
        };
      }

      // 2. 按价格从高到低排序（优先使用高价商品）
      unitProducts.sort(function (a, b) {
        return b.price - a.price;
      });

      // 3. 计算可以凑成多少组
      var totalUnits = unitProducts.length;
      var groupCount = cumulative ? Math.floor(totalUnits / x) : totalUnits >= x ? 1 : 0;
      var promotionUnits = groupCount * x;

      // 4. 分离参与促销的和不参与的商品
      var inPromotionUnits = unitProducts.slice(0, promotionUnits);
      var notInPromotionUnits = unitProducts.slice(promotionUnits);

      // 5. 按原商品 id 统计参与促销和不参与的数量
      var promotionQtyByProductId = new Map();
      var _iterator11 = _createForOfIteratorHelper(inPromotionUnits),
        _step11;
      try {
        for (_iterator11.s(); !(_step11 = _iterator11.n()).done;) {
          var _unit = _step11.value;
          var id = _unit.productId;
          promotionQtyByProductId.set(id, (promotionQtyByProductId.get(id) || 0) + 1);
        }
      } catch (err) {
        _iterator11.e(err);
      } finally {
        _iterator11.f();
      }
      var remainingQtyByProductId = new Map();
      var _iterator12 = _createForOfIteratorHelper(notInPromotionUnits),
        _step12;
      try {
        for (_iterator12.s(); !(_step12 = _iterator12.n()).done;) {
          var _unit2 = _step12.value;
          var _id = _unit2.productId;
          remainingQtyByProductId.set(_id, (remainingQtyByProductId.get(_id) || 0) + 1);
        }

        // 6. 为每个参与促销的单件计算均摊价格
        // 按组内原价比例均摊
        // 记录每个原商品的均摊价格累加（用于后续聚合）
      } catch (err) {
        _iterator12.e(err);
      } finally {
        _iterator12.f();
      }
      var allocatedPricesByProductId = new Map();
      for (var g = 0; g < groupCount; g++) {
        var startIdx = g * x;
        var endIdx = startIdx + x;
        var groupUnits = inPromotionUnits.slice(startIdx, endIdx);

        // 计算组内原价总和
        var groupOriginalTotal = groupUnits.reduce(function (sum, u) {
          return sum.plus(u.price);
        }, new Decimal(0));

        // 按原价比例均摊
        var groupPriceDecimal = new Decimal(groupPrice);
        var allocatedSum = new Decimal(0);
        for (var i = 0; i < groupUnits.length; i++) {
          var unit = groupUnits[i];
          var isLastUnit = i === groupUnits.length - 1;
          var ratio = groupOriginalTotal.eq(0) ? new Decimal(0) : new Decimal(unit.price).div(groupOriginalTotal);
          var allocatedDecimal = isLastUnit ? groupPriceDecimal.minus(allocatedSum) : groupPriceDecimal.mul(ratio);

          // 记录这个单件的均摊价格
          var productId = unit.productId;
          var prices = allocatedPricesByProductId.get(productId) || [];
          prices.push(allocatedDecimal);
          allocatedPricesByProductId.set(productId, prices);
          allocatedSum = allocatedSum.plus(allocatedDecimal);
        }
      }

      // 7. 为每个原商品生成结果
      var _iterator13 = _createForOfIteratorHelper(groupProducts),
        _step13;
      try {
        for (_iterator13.s(); !(_step13 = _iterator13.n()).done;) {
          var _product = _step13.value;
          var _key = this.getProductKey(_product);
          var _processedQty = processedQuantityMap.get(_key) || 0;
          var _availableQty = _product.quantity - _processedQty;
          if (_availableQty <= 0) continue;
          var promoQty = promotionQtyByProductId.get(_product.id) || 0;
          var remainingQty = remainingQtyByProductId.get(_product.id) || 0;
          var allocatedPrices = allocatedPricesByProductId.get(_product.id) || [];
          if (promoQty > 0 && remainingQty === 0) {
            // 全部参与促销：保留原 id，计算总 finalPrice
            var totalAllocated = allocatedPrices.reduce(function (sum, p) {
              return sum.plus(p);
            }, new Decimal(0));
            var finalPricePerUnit = this.formatPrice(totalAllocated.div(promoQty));
            result.push(_objectSpread(_objectSpread({}, _product), {}, {
              quantity: promoQty,
              finalPrice: finalPricePerUnit,
              strategyId: strategyId,
              inPromotion: true,
              isSplit: false
            }));
            var productOriginalAmount = new Decimal(_product.price).mul(promoQty);
            originalAmount = originalAmount.plus(productOriginalAmount);
            finalAmount = finalAmount.plus(totalAllocated);
          } else if (promoQty === 0 && remainingQty > 0) {
            // 全部不参与促销：保留原 id，finalPrice = 原价
            result.push(_objectSpread(_objectSpread({}, _product), {}, {
              quantity: remainingQty,
              finalPrice: this.formatPrice(_product.price),
              strategyId: undefined,
              inPromotion: false,
              isSplit: false
            }));
            var amount = new Decimal(_product.price).mul(remainingQty);
            originalAmount = originalAmount.plus(amount);
            finalAmount = finalAmount.plus(amount);
          } else if (promoQty > 0 && remainingQty > 0) {
            // 部分参与促销，部分不参与：需要拆分
            // 参与促销的部分：新 id
            var _totalAllocated = allocatedPrices.reduce(function (sum, p) {
              return sum.plus(p);
            }, new Decimal(0));
            var _finalPricePerUnit = this.formatPrice(_totalAllocated.div(promoQty));
            result.push(_objectSpread(_objectSpread({}, _product), {}, {
              id: this.generateRandomId(),
              originalId: _product.id,
              quantity: promoQty,
              finalPrice: _finalPricePerUnit,
              strategyId: strategyId,
              inPromotion: true,
              isSplit: true
            }));
            var promoOriginalAmount = new Decimal(_product.price).mul(promoQty);
            originalAmount = originalAmount.plus(promoOriginalAmount);
            finalAmount = finalAmount.plus(_totalAllocated);

            // 不参与促销的部分：保留原 id
            result.push(_objectSpread(_objectSpread({}, _product), {}, {
              quantity: remainingQty,
              finalPrice: this.formatPrice(_product.price),
              strategyId: undefined,
              inPromotion: false,
              isSplit: true
            }));
            var remainingAmount = new Decimal(_product.price).mul(remainingQty);
            originalAmount = originalAmount.plus(remainingAmount);
            finalAmount = finalAmount.plus(remainingAmount);
          }

          // 更新已处理数量
          processedQuantityMap.set(_key, _processedQty + _availableQty);
        }
      } catch (err) {
        _iterator13.e(err);
      } finally {
        _iterator13.f();
      }
      return {
        products: result,
        originalAmount: originalAmount.toNumber(),
        finalAmount: finalAmount.toNumber()
      };
    }

    /**
     * 处理 买X送Y 促销
     *
     * 计算赠品数量，商品本身价格不变
     */
  }, {
    key: "processBuyXGetYFree",
    value: function processBuyXGetYFree(group, processedQuantityMap) {
      var strategyId = group.strategyId,
        strategyName = group.strategyName,
        actionDetail = group.actionDetail,
        groupProducts = group.products;
      var detail = actionDetail;
      var buyQuantity = detail.buyQuantity,
        freeQuantity = detail.freeQuantity,
        cumulative = detail.cumulative,
        giftProducts = detail.giftProducts;
      var result = [];
      var originalAmount = new Decimal(0);
      var finalAmount = new Decimal(0);
      var totalAvailableQty = 0;

      // 1. 计算可用商品总数量
      var _iterator14 = _createForOfIteratorHelper(groupProducts),
        _step14;
      try {
        for (_iterator14.s(); !(_step14 = _iterator14.n()).done;) {
          var product = _step14.value;
          var key = this.getProductKey(product);
          var processedQty = processedQuantityMap.get(key) || 0;
          var availableQty = product.quantity - processedQty;
          if (availableQty <= 0) continue;
          totalAvailableQty += availableQty;

          // 商品本身价格不变，只是触发赠品
          result.push(_objectSpread(_objectSpread({}, product), {}, {
            quantity: availableQty,
            finalPrice: this.formatPrice(product.price),
            strategyId: strategyId,
            inPromotion: true,
            isSplit: availableQty < product.quantity
          }));
          var amount = new Decimal(product.price).mul(availableQty);
          originalAmount = originalAmount.plus(amount);
          finalAmount = finalAmount.plus(amount);

          // 更新已处理数量
          processedQuantityMap.set(key, processedQty + availableQty);
        }

        // 2. 计算赠品数量
      } catch (err) {
        _iterator14.e(err);
      } finally {
        _iterator14.f();
      }
      var giftCount = 0;
      if (totalAvailableQty >= buyQuantity) {
        giftCount = cumulative ? Math.floor(totalAvailableQty / buyQuantity) * freeQuantity : freeQuantity;
      }

      // 3. 构建赠品信息
      var giftInfo = giftCount > 0 ? {
        strategyId: strategyId,
        strategyName: strategyName,
        giftCount: giftCount,
        giftOptions: giftProducts || []
      } : null;
      return {
        products: result,
        originalAmount: originalAmount.toNumber(),
        finalAmount: finalAmount.toNumber(),
        giftInfo: giftInfo
      };
    }

    /**
     * 获取未参与任何促销的商品
     */
  }, {
    key: "getRemainingProducts",
    value: function getRemainingProducts(allProducts, processedQuantityMap) {
      var result = [];
      var _iterator15 = _createForOfIteratorHelper(allProducts),
        _step15;
      try {
        for (_iterator15.s(); !(_step15 = _iterator15.n()).done;) {
          var product = _step15.value;
          var key = this.getProductKey(product);
          var processedQty = processedQuantityMap.get(key) || 0;
          var remainingQty = product.quantity - processedQty;
          if (remainingQty > 0) {
            result.push(_objectSpread(_objectSpread({}, product), {}, {
              quantity: remainingQty,
              finalPrice: this.formatPrice(product.price),
              strategyId: undefined,
              inPromotion: false,
              isSplit: remainingQty < product.quantity
            }));
          }
        }
      } catch (err) {
        _iterator15.e(err);
      } finally {
        _iterator15.f();
      }
      return result;
    }

    /**
     * 价格统一保留两位小数
     */
  }, {
    key: "formatPrice",
    value: function formatPrice(value) {
      return new Decimal(value).toDecimalPlaces(2, Decimal.ROUND_HALF_UP).toNumber();
    }

    /**
     * 获取商品唯一标识 key
     * 使用商品的 id 作为唯一标识，不同 id 的商品不会被合并
     */
  }, {
    key: "getProductKey",
    value: function getProductKey(product) {
      return String(product.id);
    }

    /**
     * 生成随机ID
     */
  }, {
    key: "generateRandomId",
    value: function generateRandomId() {
      return Math.floor(Math.random() * 1000000000) + Date.now();
    }

    /**
     * 检查商品是否在策略的适用范围内
     */
  }, {
    key: "isProductInStrategy",
    value: function isProductInStrategy(product, config) {
      var productMatchRule = this.findProductMatchRule(config);
      if (!productMatchRule) {
        // 没有商品范围条件，默认适用所有商品
        return true;
      }
      var configProducts = productMatchRule.value;
      return this.isProductMatch(product, configProducts);
    }

    /**
     * 查找商品匹配规则
     */
  }, {
    key: "findProductMatchRule",
    value: function findProductMatchRule(config) {
      var _config$conditions;
      if (!(config !== null && config !== void 0 && (_config$conditions = config.conditions) !== null && _config$conditions !== void 0 && _config$conditions.rules)) {
        return null;
      }
      return config.conditions.rules.find(function (rule) {
        return rule.field === 'productIdAndVariantId' && (rule.operator === 'product_match' || rule.operator === 'object_in');
      });
    }

    /**
     * 检查商品是否匹配配置
     */
  }, {
    key: "isProductMatch",
    value: function isProductMatch(product, configProducts) {
      return configProducts.some(function (config) {
        // 首先检查 product_id
        if (config.product_id !== product.product_id) {
          return false;
        }

        // 如果配置的 product_variant_id = 0，只需要 product_id 匹配
        if (config.product_variant_id === 0) {
          return true;
        }

        // 否则需要 product_variant_id 精确匹配
        return config.product_variant_id === product.product_variant_id;
      });
    }

    /**
     * 获取展示配置
     */
  }, {
    key: "getDisplayConfig",
    value: function getDisplayConfig(config) {
      var _custom$display;
      var custom = config.metadata.custom;
      if (custom !== null && custom !== void 0 && (_custom$display = custom.display) !== null && _custom$display !== void 0 && _custom$display.product_card) {
        return {
          text: custom.display.product_card.text,
          type: custom.display.product_card.type
        };
      }
      return undefined;
    }
  }]);
  return PromotionEvaluator;
}();