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/shoppingCart/hooks/useSkuModal.tsx
var useSkuModal_exports = {};
__export(useSkuModal_exports, {
  useSkuModal: () => useSkuModal
});
module.exports = __toCommonJS(useSkuModal_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_ahooks = require("ahooks");
var import_utils = require("@pisell/utils");
var import_skuOptionsSelection = __toESM(require("../../../plus/skuOptionsSelection"));
var import_server = require("../server");
var import_utils2 = require("../../booking/utils");
var import_utils3 = require("../utils");
var defaultSkuValue = {
  product_id: 0,
  price: 0,
  product_variant_id: 0,
  bundle: [],
  option: []
};
var useSkuModal = (options) => {
  const [skuModalOpen, setSkuModalOpen] = (0, import_react.useState)(false);
  const [currentEditProduct, setCurrentEditProduct] = (0, import_react.useState)(null);
  const [skuValue, setSkuValue] = (0, import_react.useState)({});
  const skuOptionsSelectionRef = (0, import_react.useRef)(null);
  const localProductDataRef = (0, import_react.useRef)(null);
  const [skuDefaultValue, setSkuDefaultValue] = (0, import_react.useState)(defaultSkuValue);
  const getProductDetailRequest = (0, import_ahooks.useRequest)(import_server.getProductDetail, {
    manual: true,
    onSuccess: (res) => {
      const detail = (0, import_utils3.isGiftProductPrice)(res || {});
      setCurrentEditProduct({ ...res, ...detail });
    },
    onError: (error) => {
      console.error("getProductDetail error:", error);
    }
  });
  const getProductPrice = (0, import_ahooks.useMemoizedFn)((item) => {
    return (0, import_utils2.getProductTotalPrice)({
      ...item,
      _extend: {
        price: item == null ? void 0 : item.price,
        other: {
          ...item,
          ...skuValue
        }
      }
    });
  });
  const openSkuModal = (0, import_react.useCallback)(
    (product) => {
      setSkuDefaultValue((product == null ? void 0 : product._skuValue) || setSkuDefaultValue);
      setSkuModalOpen(true);
      getProductDetailRequest.run({
        id: product == null ? void 0 : product.id
      });
    },
    [getProductDetailRequest]
  );
  const closeSkuModal = (0, import_react.useCallback)(() => {
    var _a;
    setSkuModalOpen(false);
    setCurrentEditProduct(null);
    setSkuValue(null);
    setSkuDefaultValue(defaultSkuValue);
    localProductDataRef.current = null;
    (_a = options == null ? void 0 : options.onCancel) == null ? void 0 : _a.call(options);
  }, [options == null ? void 0 : options.onCancel]);
  const handleSkuConfirm = (0, import_react.useCallback)(async () => {
    var _a, _b;
    const skuCheckResult = await ((_a = skuOptionsSelectionRef == null ? void 0 : skuOptionsSelectionRef.current) == null ? void 0 : _a.validate());
    if (!skuCheckResult) {
      return;
    }
    const _price = getProductPrice(currentEditProduct);
    await ((_b = options == null ? void 0 : options.onConfirm) == null ? void 0 : _b.call(options, currentEditProduct == null ? void 0 : currentEditProduct.id, skuValue, _price));
    closeSkuModal();
  }, [
    skuValue,
    currentEditProduct,
    getProductPrice,
    options == null ? void 0 : options.onConfirm,
    closeSkuModal
  ]);
  const SkuModal = (0, import_react.useMemo)(() => {
    return /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellModal,
      {
        width: 660,
        open: skuModalOpen,
        title: import_utils.locales.getText("pisell2.text.shopping-cart.sku.modal.title"),
        onOk: handleSkuConfirm,
        okText: import_utils.locales.getText("pisell2.text.shopping-cart.confirm"),
        cancelText: import_utils.locales.getText("pisell2.text.shopping-cart.cancel"),
        destroyOnClose: true,
        onCancel: closeSkuModal,
        className: "shopping-cart-sku-modal"
      },
      getProductDetailRequest.loading ? /* @__PURE__ */ import_react.default.createElement(import_antd.Skeleton, { active: true, loading: true }) : skuModalOpen && currentEditProduct && /* @__PURE__ */ import_react.default.createElement(
        import_skuOptionsSelection.default,
        {
          ref: skuOptionsSelectionRef,
          dataSource: currentEditProduct,
          value: skuDefaultValue,
          skuCardConfig: {
            title: {
              lineClamp: 1
            },
            subTitle: {
              lineClamp: 1
            }
          },
          customConfig: {
            modalWidth: "660px"
          },
          onChange: (v) => {
            console.log("SKUOptionsSelection onChange:", v);
            setSkuValue(v);
          }
        }
      )
    );
  }, [
    skuModalOpen,
    currentEditProduct,
    skuDefaultValue,
    getProductDetailRequest.loading,
    handleSkuConfirm,
    closeSkuModal
  ]);
  return {
    /** 打开 SKU 弹窗方法 */
    openSkuModal,
    /** 关闭 SKU 弹窗方法 */
    closeSkuModal,
    /** SKU Modal 组件 */
    SkuModal,
    /** SKU 弹窗是否打开 */
    isSkuModalOpen: skuModalOpen,
    /** 当前编辑的商品 */
    currentEditProduct,
    /** 当前选中的 SKU 值 */
    skuValue
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useSkuModal
});
