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/transformValueToSelectorValue.ts
var transformValueToSelectorValue_exports = {};
__export(transformValueToSelectorValue_exports, {
  transformValueToSelectorValue: () => transformValueToSelectorValue
});
module.exports = __toCommonJS(transformValueToSelectorValue_exports);
var import_decimal = __toESM(require("decimal.js"));
var transformValueToSelectorValue = (value, dataSource) => {
  var _a, _b, _c, _d, _e, _f;
  console.log("transformValueToSelectorValue_value>>>>>>>", value);
  if (!value || !dataSource || ((_a = Object.keys(value || {})) == null ? void 0 : _a.length) === 0 || ((_b = Object.keys(dataSource || {})) == null ? void 0 : _b.length) === 0) {
    return {};
  }
  let transformValue = {};
  if (value == null ? void 0 : value.bundle) {
    const newBundle = (_c = value == null ? void 0 : value.bundle) == null ? void 0 : _c.reduce((acc, v) => {
      var _a2, _b2, _c2, _d2;
      const _bundleItemId = v == null ? void 0 : v.id;
      const idx = acc == null ? void 0 : acc.findIndex((v1) => (v1 == null ? void 0 : v1.id) === _bundleItemId);
      if (idx !== -1) {
        (_c2 = (_b2 = (_a2 = acc[idx]) == null ? void 0 : _a2.bundleOptions) == null ? void 0 : _b2.push) == null ? void 0 : _c2.call(_b2, v);
      } else {
        (_d2 = acc == null ? void 0 : acc.push) == null ? void 0 : _d2.call(acc, {
          id: _bundleItemId,
          groupId: v == null ? void 0 : v.group_id,
          bundleOptions: [v]
        });
      }
      return acc;
    }, []);
    (_d = newBundle == null ? void 0 : newBundle.forEach) == null ? void 0 : _d.call(newBundle, (v1) => {
      const _bundleGroupId = v1 == null ? void 0 : v1.groupId;
      const _bundleValue = createBundleValue(v1, dataSource);
      if (transformValue[_bundleGroupId]) {
        transformValue[_bundleGroupId] = [
          ...transformValue[_bundleGroupId] || [],
          _bundleValue
        ];
      } else {
        transformValue[_bundleGroupId] = [_bundleValue];
      }
    });
  }
  if (value == null ? void 0 : value.option) {
    transformValue = createOptionValue(transformValue, value, dataSource);
  }
  if (value == null ? void 0 : value.product_variant_id) {
    const filteredVariant = (_e = dataSource == null ? void 0 : dataSource.variant) == null ? void 0 : _e.find((item) => {
      return (item == null ? void 0 : item.id) === (value == null ? void 0 : value.product_variant_id);
    });
    if (!!filteredVariant) {
      const ids = ((_f = filteredVariant == null ? void 0 : filteredVariant.sku_ids) == null ? void 0 : _f.split("_")) || [];
      ids.forEach((id) => {
        var _a2;
        const [variantGroupId, variantId] = id.split(":");
        const _variantGroupId = Number(variantGroupId);
        const _variantId = Number(variantId);
        let _variantGroupItem = null;
        (_a2 = dataSource == null ? void 0 : dataSource.variant_group) == null ? void 0 : _a2.find((v1) => {
          var _a3;
          if ((v1 == null ? void 0 : v1.id) === _variantGroupId) {
            (_a3 = v1 == null ? void 0 : v1.variant_item) == null ? void 0 : _a3.find((v2) => {
              if ((v2 == null ? void 0 : v2.id) === _variantId) {
                _variantGroupItem = v2;
                return true;
              }
              return false;
            });
            return true;
          }
          return false;
        });
        if (!!_variantGroupItem) {
          transformValue[_variantGroupId] = {
            data: { ..._variantGroupItem || {} },
            quantity: 1,
            value: _variantId
          };
        }
      });
    }
  }
  return transformValue;
};
var createBundleValue = (v, dataSource) => {
  var _a, _b, _c, _d;
  let _createdValue = null;
  const bundleGroupItem = (_a = dataSource == null ? void 0 : dataSource.bundle_group) == null ? void 0 : _a.find((v1) => {
    return (v1 == null ? void 0 : v1.id) === (v == null ? void 0 : v.groupId);
  });
  let findedBundleItem = null;
  if (!!bundleGroupItem) {
    findedBundleItem = (_b = bundleGroupItem == null ? void 0 : bundleGroupItem.bundle_item) == null ? void 0 : _b.find((v1) => {
      return (v1 == null ? void 0 : v1.id) === (v == null ? void 0 : v.id);
    });
    if (!!findedBundleItem) {
      _createdValue = {
        value: findedBundleItem == null ? void 0 : findedBundleItem.id,
        data: { ...findedBundleItem || {} }
      };
    }
  }
  if (!_createdValue) {
    return null;
  }
  let totalQuantity = 0;
  (_d = (_c = v == null ? void 0 : v.bundleOptions) == null ? void 0 : _c.forEach) == null ? void 0 : _d.call(_c, (v1) => {
    var _a2, _b2;
    if ((_a2 = v1 == null ? void 0 : v1.option) == null ? void 0 : _a2.length) {
      const _data = ((_b2 = _createdValue == null ? void 0 : _createdValue.data) == null ? void 0 : _b2._data) || [];
      const _createdValueData = createOptionValue({}, v1, findedBundleItem);
      _data.push({
        value: v1 == null ? void 0 : v1.id,
        data: _createdValueData,
        quantity: (v1 == null ? void 0 : v1.quantity) || 1
      });
      _createdValue.data._data = _data;
    }
    totalQuantity = totalQuantity + ((v1 == null ? void 0 : v1.quantity) || (v1 == null ? void 0 : v1.num) || 1);
    const _price = (v1 == null ? void 0 : v1.price) || (findedBundleItem == null ? void 0 : findedBundleItem.price) || 0;
    _createdValue.totalPrice = new import_decimal.default(_createdValue.totalPrice || 0).plus(new import_decimal.default(_price).times((v1 == null ? void 0 : v1.quantity) || 1)).toNumber();
  });
  _createdValue.quantity = totalQuantity;
  _createdValue.data.quantity = totalQuantity;
  _createdValue.data.num = totalQuantity;
  return _createdValue;
};
var createOptionValue = (initValue, value, dataSource) => {
  var _a, _b;
  let _createdValue = initValue || {};
  (_b = (_a = value == null ? void 0 : value.option) == null ? void 0 : _a.forEach) == null ? void 0 : _b.call(_a, (v1) => {
    var _a2, _b2;
    const _optionGroupId = v1 == null ? void 0 : v1.option_group_id;
    const matchedGroup = (_a2 = dataSource == null ? void 0 : dataSource.option_group) == null ? void 0 : _a2.find(
      (v2) => (v2 == null ? void 0 : v2.id) === _optionGroupId
    );
    const matchedOptionItem = (_b2 = matchedGroup == null ? void 0 : matchedGroup.option_item) == null ? void 0 : _b2.find(
      (v3) => (v3 == null ? void 0 : v3.id) === (v1 == null ? void 0 : v1.id)
    );
    const _num = (v1 == null ? void 0 : v1.quantity) || (v1 == null ? void 0 : v1.num) || 1;
    const _price = (matchedOptionItem == null ? void 0 : matchedOptionItem.add_price) || 0;
    const _tempOptionValue = {
      data: {
        ...matchedOptionItem || {},
        ...v1 || {},
        num: _num,
        price: _price,
        original_price: _price,
        original_total: _price
      },
      quantity: _num,
      value: v1 == null ? void 0 : v1.id,
      totalPrice: new import_decimal.default((matchedOptionItem == null ? void 0 : matchedOptionItem.add_price) || 0).times(_num).toNumber()
    };
    if (_createdValue[_optionGroupId]) {
      _createdValue[_optionGroupId] = [
        ..._createdValue[_optionGroupId] || [],
        _tempOptionValue
      ];
    } else {
      if ((matchedGroup == null ? void 0 : matchedGroup.is_multiple) == 1) {
        _createdValue[_optionGroupId] = [_tempOptionValue];
      } else {
        _createdValue[_optionGroupId] = _tempOptionValue;
      }
    }
  });
  return _createdValue;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  transformValueToSelectorValue
});
