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/transformSelectorValueToValue.ts
var transformSelectorValueToValue_exports = {};
__export(transformSelectorValueToValue_exports, {
  transformSelectorValueToValue: () => transformSelectorValueToValue
});
module.exports = __toCommonJS(transformSelectorValueToValue_exports);
var import_decimal = __toESM(require("decimal.js"));
var import_store = __toESM(require("../store/store"));
var transformSelectorValueToValue = (selectorValue) => {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  if (!selectorValue) {
    return {};
  }
  const dataSource = import_store.default.getState().dataSource;
  const transformValue = {
    bundle: [],
    // 套餐商品组数据
    option: [],
    // 单规格组数据
    product_id: dataSource == null ? void 0 : dataSource.id,
    // 商品id
    product_variant_id: 0,
    // 多规格商品id
    product_option_string: ""
    // 多规格商品选项字符串
  };
  if ((_a = dataSource == null ? void 0 : dataSource.bundle_group) == null ? void 0 : _a.length) {
    (_b = dataSource == null ? void 0 : dataSource.bundle_group) == null ? void 0 : _b.forEach((bundleGroupItem) => {
      const groupId = bundleGroupItem == null ? void 0 : bundleGroupItem.id;
      const groupItemValue = selectorValue == null ? void 0 : selectorValue[groupId];
      if (!groupId || !groupItemValue) {
        return;
      }
      const _groupItemValue = Array.isArray(groupItemValue) ? groupItemValue : [groupItemValue];
      _groupItemValue.forEach((item) => {
        const { _data, ...rest } = (item == null ? void 0 : item.data) || {};
        if (_data) {
          _data == null ? void 0 : _data.forEach((_item) => {
            if (_item == null ? void 0 : _item.data) {
              let _newData = { ...rest, option: [] };
              let totalPrice = new import_decimal.default(0);
              Object.keys(_item == null ? void 0 : _item.data).forEach((key) => {
                var _a2, _b2;
                const _itemDataItem = (_a2 = _item == null ? void 0 : _item.data) == null ? void 0 : _a2[key];
                if (Array.isArray(_itemDataItem)) {
                  _itemDataItem.forEach(
                    (_itemDataItem_item) => {
                      var _a3;
                      totalPrice = totalPrice.plus(
                        new import_decimal.default((_itemDataItem_item == null ? void 0 : _itemDataItem_item.totalPrice) || 0)
                      );
                      _newData.option.push({
                        ...(_itemDataItem_item == null ? void 0 : _itemDataItem_item.data) || {},
                        product_option_item_id: (_a3 = _itemDataItem_item == null ? void 0 : _itemDataItem_item.data) == null ? void 0 : _a3.id
                      });
                    }
                  );
                } else {
                  totalPrice = totalPrice.plus(
                    new import_decimal.default((_itemDataItem == null ? void 0 : _itemDataItem.totalPrice) || 0)
                  );
                  _newData.option.push({
                    ...(_itemDataItem == null ? void 0 : _itemDataItem.data) || {},
                    product_option_item_id: (_b2 = _itemDataItem == null ? void 0 : _itemDataItem.data) == null ? void 0 : _b2.id
                  });
                }
              });
              const _restPricePrefix = (rest == null ? void 0 : rest.price_type) === "markdown" ? -1 : 1;
              _newData.price = totalPrice.plus(new import_decimal.default((rest == null ? void 0 : rest.price) || 0).times(_restPricePrefix)).toString();
              _newData.original_price = _newData == null ? void 0 : _newData.price;
              _newData.original_total = _newData == null ? void 0 : _newData.price;
              _newData.quantity = (_item == null ? void 0 : _item.quantity) || 0;
              _newData.num = (_item == null ? void 0 : _item.quantity) || 0;
              _newData._bundle_product_id = _newData == null ? void 0 : _newData.bundle_product_id;
              transformValue.bundle.push(_newData);
            }
          });
        } else {
          const _newData = { ...(item == null ? void 0 : item.data) || {} };
          const _pricePrefix = (_newData == null ? void 0 : _newData.price_type) === "markdown" ? -1 : 1;
          _newData.price = new import_decimal.default((_newData == null ? void 0 : _newData.price) || 0).times(_pricePrefix).toString();
          _newData.original_price = _newData == null ? void 0 : _newData.price;
          _newData.original_total = _newData == null ? void 0 : _newData.price;
          _newData._bundle_product_id = _newData == null ? void 0 : _newData.bundle_product_id;
          transformValue.bundle.push(_newData);
        }
      });
    });
  }
  if ((_c = dataSource == null ? void 0 : dataSource.option_group) == null ? void 0 : _c.length) {
    (_d = dataSource == null ? void 0 : dataSource.option_group) == null ? void 0 : _d.forEach((optionGroupItem) => {
      const groupId = optionGroupItem == null ? void 0 : optionGroupItem.id;
      const groupItemValue = selectorValue == null ? void 0 : selectorValue[groupId];
      if (!groupId || !groupItemValue) {
        return;
      }
      let _groupItemValue = null;
      if (Array.isArray(groupItemValue)) {
        _groupItemValue = groupItemValue;
      } else {
        _groupItemValue = [groupItemValue];
      }
      _groupItemValue == null ? void 0 : _groupItemValue.forEach((item) => {
        var _a2;
        if (item == null ? void 0 : item.data) {
          transformValue.option.push({
            ...(item == null ? void 0 : item.data) || {},
            product_option_item_id: (_a2 = item == null ? void 0 : item.data) == null ? void 0 : _a2.id
          });
        }
      });
    });
  }
  if ((_e = dataSource == null ? void 0 : dataSource.variant_group) == null ? void 0 : _e.length) {
    const selectedVariantIds = [];
    const selectedVariantOptionString = [];
    (_f = dataSource == null ? void 0 : dataSource.variant_group) == null ? void 0 : _f.forEach((variantGroupItem) => {
      const groupId = variantGroupItem == null ? void 0 : variantGroupItem.id;
      const groupItemValue = selectorValue == null ? void 0 : selectorValue[groupId];
      if (!groupId || !groupItemValue) {
        return;
      }
      let _groupItemValue = null;
      if (Array.isArray(groupItemValue)) {
        _groupItemValue = groupItemValue;
      } else {
        _groupItemValue = [groupItemValue];
      }
      _groupItemValue == null ? void 0 : _groupItemValue.forEach((item) => {
        var _a2, _b2, _c2;
        if (item == null ? void 0 : item.data) {
          selectedVariantIds.push(
            `${(_a2 = item == null ? void 0 : item.data) == null ? void 0 : _a2.variant_group_id}:${(_b2 = item == null ? void 0 : item.data) == null ? void 0 : _b2.id}`
          );
          selectedVariantOptionString.push(
            `${variantGroupItem == null ? void 0 : variantGroupItem.name}:${(_c2 = item == null ? void 0 : item.data) == null ? void 0 : _c2.name}`
          );
        }
      });
    });
    if (selectedVariantIds == null ? void 0 : selectedVariantIds.length) {
      const filteredVariant = (_g = dataSource == null ? void 0 : dataSource.variant) == null ? void 0 : _g.filter((item) => {
        var _a2;
        const ids = ((_a2 = item == null ? void 0 : item.sku_ids) == null ? void 0 : _a2.split("_")) || [];
        return selectedVariantIds.every(
          (idItem) => ids.includes(idItem)
        );
      });
      if (filteredVariant == null ? void 0 : filteredVariant.length) {
        transformValue.product_variant_id = (_h = filteredVariant[0]) == null ? void 0 : _h.id;
        transformValue.product_option_string = selectedVariantOptionString.join(", ");
      }
    }
  }
  console.log("transformSelectorValueToValue>>>>>", transformValue);
  return transformValue;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  transformSelectorValueToValue
});
