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/components/eventBooking/components/Voucher/hooks.ts
var hooks_exports = {};
__export(hooks_exports, {
  useShopDiscount: () => useShopDiscount,
  useVoucherLogin: () => useVoucherLogin
});
module.exports = __toCommonJS(hooks_exports);
var import_Sales = require("../../../Sales");
var import_ahooks = require("ahooks");
var import_serve = require("../../../Sales/Event/serve");
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var useShopDiscount = () => {
  const pisellOS = (0, import_useEngineContext.usePisellOS)();
  return pisellOS.getModule("shopDiscount:eventBooking");
};
var useVoucherLogin = () => {
  var _a;
  const context = (0, import_useEngineContext.default)();
  const { getStore } = ((_a = context.appHelper) == null ? void 0 : _a.utils) || {};
  const { setData } = (0, import_Sales.useSales)();
  const { cartData } = (0, import_Sales.useCart)();
  const _getProductPrice = (0, import_ahooks.useRequest)(import_serve.getProductPrice, {
    manual: true,
    onSuccess: (res) => {
      const list = res.data || [];
      const productById = list.reduce((pre, cur) => {
        pre[cur.id] = cur;
        return pre;
      }, {});
      console.log("start update price");
      const newCartItems = cartData.cartItems.map((item) => {
        var _a2;
        const { product } = item._originData || {};
        let newProduct = productById[product.product_id];
        let _totalPrice = Number(newProduct.price);
        if (!(item == null ? void 0 : item._productDetail)) {
          item.price = _totalPrice;
          item.total = _totalPrice;
          return item;
        }
        const { bundle = [], options } = item._productDetail;
        if (
          // 1. 套餐商品
          Array.isArray(product == null ? void 0 : product.product_bundle) && product.product_bundle.length > 0
        ) {
          const { bundle_group = [] } = newProduct;
          for (let i = 0; i < product.product_bundle.length; i++) {
            const pbItem = product.product_bundle[i];
            const { bundle_group_id, bundle_id, option } = pbItem;
            const _originBundleItemInfo = bundle.find(
              (bi) => bi.id === bundle_id
            );
            const _targetBundleGroup = bundle_group.find(
              (bg) => bg.id === bundle_group_id
            );
            if ((_a2 = _targetBundleGroup == null ? void 0 : _targetBundleGroup.bundle_item) == null ? void 0 : _a2.length) {
              let _newBundleItemInfo = _targetBundleGroup.bundle_item.find(
                (bi) => bi.id === bundle_id
              );
              if ((_originBundleItemInfo == null ? void 0 : _originBundleItemInfo._bundle_product_id) === (_newBundleItemInfo == null ? void 0 : _newBundleItemInfo.bundle_product_id)) {
                const {
                  price_type,
                  num = 1,
                  option: _originBundleItemOptions
                } = _originBundleItemInfo;
                const _bundleItemPrice = Number(_newBundleItemInfo.price);
                if (price_type === "markup") {
                  _totalPrice += _bundleItemPrice * num;
                } else if (price_type === "markdown") {
                  _totalPrice -= _bundleItemPrice * num;
                } else {
                  _totalPrice += _bundleItemPrice * num;
                }
                if (Array.isArray(option) && (option == null ? void 0 : option.length) > 0) {
                  for (let j = 0; j < option.length; j++) {
                    const { option_group_item_id } = option[j];
                    const _originOptionItem = _originBundleItemOptions.find(
                      (op) => op.product_option_item_id === option_group_item_id
                    );
                    if (_originOptionItem) {
                      const { price, num: _optionNum = 1 } = _originOptionItem;
                      _totalPrice += Number(price) * _optionNum;
                    }
                  }
                }
              }
            }
          }
        } else {
          if (product == null ? void 0 : product.product_variant_id) {
            const { variant = [] } = newProduct;
            const _variant = variant.find(
              (v) => v.id === product.product_variant_id
            );
            if (_variant) {
              _totalPrice = Number(_variant.price);
            }
          }
          if (Array.isArray(product == null ? void 0 : product.product_option_item) && product.product_option_item.length > 0) {
            for (let i = 0; i < product.product_option_item.length; i++) {
              const { option_group_item_id } = product.product_option_item[i];
              const _originOptionItem = options.find(
                (op) => op.product_option_item_id === option_group_item_id
              );
              if (_originOptionItem) {
                const { price, num = 1 } = _originOptionItem;
                _totalPrice += Number(price) * num;
              }
            }
          }
        }
        item.price = _totalPrice;
        item.total = _totalPrice;
        return item;
      });
      console.log("new cart data", newCartItems);
      setData({
        cartData: {
          ...cartData,
          cartItems: newCartItems
        }
      });
    }
  });
  const getCartItemProductsWithQuotation = async () => {
    var _a2, _b;
    const ids = cartData.cartItems.map((item) => {
      const { product = {} } = item._originData || {};
      const { product_id } = product;
      return product_id;
    }).filter((id) => !!id);
    const store = getStore();
    const customer = (_b = (_a2 = store == null ? void 0 : store.getState()) == null ? void 0 : _a2.global) == null ? void 0 : _b.userInfo;
    if (!(ids == null ? void 0 : ids.length)) return;
    _getProductPrice.run({
      ids,
      customer_id: customer == null ? void 0 : customer.id
    });
  };
  return {
    getCartItemProductsWithQuotation
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useShopDiscount,
  useVoucherLogin
});
