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/plus/skuOptionsSelection/utils/calculateOption.ts
var calculateOption_exports = {};
__export(calculateOption_exports, {
  calculateOptionItem: () => calculateOptionItem
});
module.exports = __toCommonJS(calculateOption_exports);
var import_decimal = __toESM(require("decimal.js"));
var import_lodash = require("lodash");
var calculateOptionItem = (params) => {
  var _a, _b, _c;
  const { editIndex, optionItemValue, optionItemDataSource, data } = params;
  const currentData = ((_a = optionItemValue == null ? void 0 : optionItemValue.data) == null ? void 0 : _a._data) || [];
  const uniqueKey = optionItemDataSource == null ? void 0 : optionItemDataSource._key;
  let newOriginal = (0, import_lodash.cloneDeep)((optionItemDataSource == null ? void 0 : optionItemDataSource._original) || {});
  if (data == null ? void 0 : data.quantity) {
    if (editIndex === -1) {
      newOriginal._data = [...currentData, { value: uniqueKey, ...data }];
    } else {
      currentData[editIndex] = {
        value: uniqueKey,
        ...data
      };
      newOriginal._data = [...currentData];
    }
  } else {
    if (editIndex !== -1) {
      currentData.splice(editIndex, 1);
      newOriginal._data = [...currentData];
    }
  }
  let totalQuantity = 0;
  let totalPrice = new import_decimal.default(0);
  (_c = (_b = newOriginal == null ? void 0 : newOriginal._data) == null ? void 0 : _b.forEach) == null ? void 0 : _c.call(_b, (item) => {
    totalQuantity += (item == null ? void 0 : item.quantity) || 0;
    let itemTotalPrice = new import_decimal.default(0);
    if (Object.prototype.toString.call(item.data) === "[object Object]") {
      Object.keys(item.data).forEach((key) => {
        var _a2, _b2, _c2;
        const itemDataItem = (_a2 = item.data) == null ? void 0 : _a2[key];
        console.log("newOriginal_item_key>>>>>", key, itemDataItem);
        let tempPrice = new import_decimal.default(0);
        if (Array.isArray(itemDataItem)) {
          itemDataItem.forEach((_item) => {
            var _a3, _b3;
            tempPrice = tempPrice.plus(
              new import_decimal.default(((_a3 = _item == null ? void 0 : _item.data) == null ? void 0 : _a3.price) || 0).times(
                new import_decimal.default(((_b3 = _item == null ? void 0 : _item.data) == null ? void 0 : _b3.quantity) || 0)
              )
            );
          });
        } else {
          tempPrice = new import_decimal.default(((_b2 = itemDataItem == null ? void 0 : itemDataItem.data) == null ? void 0 : _b2.price) || 0).times(
            new import_decimal.default(((_c2 = itemDataItem == null ? void 0 : itemDataItem.data) == null ? void 0 : _c2.quantity) || 0)
          );
        }
        itemTotalPrice = itemTotalPrice.plus(tempPrice);
      });
    }
    totalPrice = totalPrice.plus(
      itemTotalPrice.times(new import_decimal.default((item == null ? void 0 : item.quantity) || 0))
    );
  });
  const _pricePrefix = (newOriginal == null ? void 0 : newOriginal.price_type) === "markdown" ? -1 : 1;
  totalPrice = totalPrice.plus(
    new import_decimal.default((newOriginal == null ? void 0 : newOriginal.price) || 0).times(new import_decimal.default(totalQuantity || 0)).times(_pricePrefix)
  );
  const totalPriceNumber = totalPrice.toNumber();
  newOriginal = {
    ...newOriginal,
    quantity: totalQuantity,
    num: totalQuantity
  };
  return [newOriginal, totalPriceNumber, totalQuantity];
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  calculateOptionItem
});
