var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/modules/Cart/utils/changePrice.ts
var changePrice_exports = {};
__export(changePrice_exports, {
  updateAllCartItemPrice: () => updateAllCartItemPrice
});
module.exports = __toCommonJS(changePrice_exports);
var import_decimal = __toESM(require("decimal.js"));
async function updateAllCartItemPrice(cartItems, priceData, getProduct, updateCart, updateItemInitInfo) {
  var _a, _b;
  for (const item of cartItems) {
    const targetProduct = priceData.find((n) => n.id === item.id);
    const cartProduct = await getProduct(
      item.id
    );
    let productInfo = cartProduct;
    if (!productInfo) {
      productInfo = item._productOrigin;
    }
    let bundle = item._bundleOrigin;
    productInfo.price = targetProduct == null ? void 0 : targetProduct.price;
    productInfo.base_price = targetProduct == null ? void 0 : targetProduct.base_price;
    productInfo.product_variant_id = (_a = item._productOrigin) == null ? void 0 : _a.product_variant_id;
    bundle = bundle == null ? void 0 : bundle.map((n) => {
      var _a2;
      const targetBundle = (_a2 = targetProduct == null ? void 0 : targetProduct.bundle_group) == null ? void 0 : _a2.find(
        (m) => m.id === n.group_id
      );
      if (targetBundle) {
        const targetBundleItem = targetBundle.bundle_item.find(
          (m) => m.id === n.id
        );
        if (targetBundleItem.price_type === "markdown") {
          targetBundleItem.price = new import_decimal.default(targetBundleItem.price || 0).mul(-1).toNumber();
        }
        if (n.option) {
          targetBundleItem.price = new import_decimal.default(targetBundleItem.price || 0).add(n.option.reduce((pre, cur) => {
            return pre + new import_decimal.default(cur.price || 0).mul(cur.num || 1).toNumber();
          }, 0)).toNumber();
        }
        if (targetBundleItem) {
          return {
            ...n,
            price: targetBundleItem.price,
            base_price: targetBundleItem.base_price
          };
        }
      }
      return n;
    });
    if (targetProduct == null ? void 0 : targetProduct.variant) {
      productInfo.variant = (_b = productInfo.variant) == null ? void 0 : _b.map((n) => {
        var _a2;
        const targetVariant = (_a2 = targetProduct.variant) == null ? void 0 : _a2.find((m) => m.id === n.id);
        if (targetVariant) {
          return {
            ...n,
            price: targetVariant.price,
            base_price: targetVariant.base_price
          };
        }
        return n;
      });
    }
    updateItemInitInfo({
      _id: item._id,
      product: productInfo
    });
    updateCart({
      _id: item._id,
      product: productInfo,
      bundle,
      options: item._optionsOrigin
    });
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  updateAllCartItemPrice
});
