var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/salesSdk/bookingEditService/useBookingEditProduct.ts
var useBookingEditProduct_exports = {};
__export(useBookingEditProduct_exports, {
  useBookingEditProduct: () => useBookingEditProduct
});
module.exports = __toCommonJS(useBookingEditProduct_exports);
var import_react = require("react");
var import_ahooks = require("ahooks");
var import_utils = require("../../../components/booking/info/service2/utils");
var import_SalesSdkHostBridgesContext = require("../context/SalesSdkHostBridgesContext");
var import_useSalesSdkUIHosts = require("../hooks/useSalesSdkUIHosts");
var import_applyProductDetailEditInDrawer = require("./applyProductDetailEditInDrawer");
var import_buildEditProductDetailPayload = require("./buildEditProductDetailPayload");
var import_canShowBookingEditProductButton = require("./canShowBookingEditProductButton");
function shouldOpenNormalProductDetail(cacheItem) {
  if ((0, import_utils.isBookingProduct)(cacheItem == null ? void 0 : cacheItem.extension_type)) {
    return false;
  }
  return (0, import_utils.isNormalProductByDurationSchedule)(cacheItem);
}
function useBookingEditProduct(options) {
  const {
    cacheItem,
    bookingState,
    form,
    onCacheItemChange,
    onAfterProductEdit,
    resetManualPriceChange
  } = options;
  const uiHosts = (0, import_useSalesSdkUIHosts.useSalesSdkUIHosts)();
  const bridges = (0, import_SalesSdkHostBridgesContext.useSalesSdkHostBridges)();
  const [editingProduct, setEditingProduct] = (0, import_react.useState)(false);
  const canEditProduct = (0, import_react.useMemo)(
    () => (0, import_canShowBookingEditProductButton.canShowBookingEditProductButton)({
      cacheItem,
      bookingState,
      hasOpenProductDetail: Boolean(uiHosts == null ? void 0 : uiHosts.openProductDetail)
    }),
    [cacheItem, bookingState, uiHosts == null ? void 0 : uiHosts.openProductDetail]
  );
  const applyEditResult = (0, import_ahooks.useMemoizedFn)(
    (result) => {
      onCacheItemChange(result.cacheItem);
      form.setFieldsValue(result.formPatch);
      resetManualPriceChange();
      onAfterProductEdit(result.cacheItem);
    }
  );
  const handleEditProduct = (0, import_ahooks.useMemoizedFn)(async () => {
    var _a;
    if (!cacheItem || !canEditProduct || editingProduct) {
      return;
    }
    const formValues = form.getFieldsValue();
    setEditingProduct(true);
    try {
      if (shouldOpenNormalProductDetail(cacheItem)) {
        const editedResult = await bridges.normalProductDetail.request({
          mode: "edit",
          cacheItem: {
            ...cacheItem,
            _extend: {
              ...cacheItem._extend,
              ...formValues
            }
          },
          skuDetailConfig: cacheItem == null ? void 0 : cacheItem.skuDetailConfig
        });
        if (!editedResult || editedResult.mode !== "edit") {
          return;
        }
        applyEditResult(
          (0, import_applyProductDetailEditInDrawer.mergeNormalProductEditInDrawer)({
            cacheItem,
            editedCacheItem: editedResult.cacheItem,
            formValues
          })
        );
        return;
      }
      const openDetail = uiHosts == null ? void 0 : uiHosts.openProductDetail;
      if (!openDetail) {
        console.warn("[SalesSdk] openProductDetail 未配置，无法编辑商品规格");
        return;
      }
      const payload = (0, import_buildEditProductDetailPayload.buildEditProductDetailPayload)({
        cacheItem,
        bookingState,
        formValues
      });
      const detailResult = await openDetail(payload);
      if (!detailResult) {
        return;
      }
      applyEditResult(
        (0, import_applyProductDetailEditInDrawer.applyProductDetailEditInDrawer)({
          cacheItem,
          formValues,
          detailResult,
          showNumber: (_a = payload.showConfig) == null ? void 0 : _a.number
        })
      );
    } catch (err) {
      console.warn("[SalesSdk] handleEditProduct 失败", err);
    } finally {
      setEditingProduct(false);
    }
  });
  return {
    canEditProduct,
    handleEditProduct,
    editingProduct
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useBookingEditProduct
});
