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/salesSdk/normalProductDetail/NormalProductDetailHostRenderer.tsx
var NormalProductDetailHostRenderer_exports = {};
__export(NormalProductDetailHostRenderer_exports, {
  NormalProductDetailHostRenderer: () => NormalProductDetailHostRenderer
});
module.exports = __toCommonJS(NormalProductDetailHostRenderer_exports);
var import_react = __toESM(require("react"));
var import_dayjs = __toESM(require("dayjs"));
var import_skuDetailModal = __toESM(require("../../../pro/skuDetailModal"));
var import_model = require("../../../components/booking/info/model");
var import_SalesSdkHostBridgesContext = require("../context/SalesSdkHostBridgesContext");
var import_useSalesSdk = require("../hooks/useSalesSdk");
var import_transformNormalProductConfirm = require("./transformNormalProductConfirm");
var import_resolveCatalogProductId = require("../giftSelect/resolveCatalogProductId");
var import_ahooks = require("ahooks");
var OPEN_NORMAL_PRODUCT_DETAIL_TIMEOUT_MS = 12e4;
var NormalProductDetailHostRenderer = () => {
  const bridges = (0, import_SalesSdkHostBridgesContext.useSalesSdkHostBridges)();
  const bridge = bridges.normalProductDetail;
  const pending = (0, import_react.useSyncExternalStore)(bridge.subscribe, bridge.getSnapshot, () => null);
  const { bookingTicket } = (0, import_useSalesSdk.useSalesSdk)();
  const modalRef = (0, import_react.useRef)(null);
  const timerRef = (0, import_react.useRef)(null);
  const bookingContextValue = (0, import_react.useMemo)(
    () => {
      var _a;
      return {
        state: {
          bookingConfig: ((_a = bookingTicket == null ? void 0 : bookingTicket.getBookingConfig) == null ? void 0 : _a.call(bookingTicket)) ?? null
        }
      };
    },
    [bookingTicket, pending == null ? void 0 : pending.payload]
  );
  (0, import_react.useEffect)(() => {
    var _a, _b, _c;
    if (!pending) {
      if (timerRef.current) {
        clearTimeout(timerRef.current);
        timerRef.current = null;
      }
      return;
    }
    timerRef.current = setTimeout(() => {
      if (bridge.getSnapshot()) {
        console.error("[SalesSdk] openNormalProductDetail 超时：SkuDetailModal 未响应");
        bridge.complete(null);
      }
    }, OPEN_NORMAL_PRODUCT_DETAIL_TIMEOUT_MS);
    const payload = pending.payload;
    if (payload.mode === "create") {
      const detailPayload = payload.requiresDetailPayload;
      const item = (0, import_resolveCatalogProductId.normalizeCatalogProduct)(
        detailPayload.productData ?? detailPayload.item ?? {}
      );
      const productId = (0, import_resolveCatalogProductId.resolveCatalogProductId)(item);
      const rawDate = detailPayload.date;
      const date = (0, import_dayjs.default)(rawDate).isValid() ? (0, import_dayjs.default)(rawDate).format("YYYY-MM-DD") : (0, import_dayjs.default)().format("YYYY-MM-DD");
      (_a = modalRef.current) == null ? void 0 : _a.open({
        id: productId ?? (item == null ? void 0 : item.id),
        date,
        is_eject: detailPayload.isEject,
        productData: item,
        skuDetailConfig: (item == null ? void 0 : item.skuDetailConfig) || {}
      });
      return;
    }
    (_c = modalRef.current) == null ? void 0 : _c.open({
      ...payload.cacheItem,
      skuDetailConfig: payload.skuDetailConfig || ((_b = payload.cacheItem) == null ? void 0 : _b.skuDetailConfig) || {}
    });
    return () => {
      if (timerRef.current) {
        clearTimeout(timerRef.current);
        timerRef.current = null;
      }
    };
  }, [pending, bridge]);
  const handleConfirm = (0, import_ahooks.useMemoizedFn)((data) => {
    if (!pending) return;
    if (timerRef.current) {
      clearTimeout(timerRef.current);
      timerRef.current = null;
    }
    if (pending.payload.mode === "create") {
      const detailPayload = pending.payload.requiresDetailPayload;
      const item = (0, import_resolveCatalogProductId.normalizeCatalogProduct)(
        detailPayload.productData ?? detailPayload.item ?? {}
      );
      const productId = (0, import_resolveCatalogProductId.resolveCatalogProductId)(item);
      if (!productId) {
        console.warn("[SalesSdk] openNormalProductDetail: 商品缺少有效 id/product_id");
        bridge.complete(null);
        return;
      }
      bridge.complete({
        mode: "create",
        detailResult: (0, import_transformNormalProductConfirm.transformNormalProductCreateConfirm)(productId, data)
      });
      return;
    }
    bridge.complete({
      mode: "edit",
      cacheItem: (0, import_transformNormalProductConfirm.prepareNormalProductEditCacheItem)(data)
    });
  });
  const handleClose = (0, import_ahooks.useMemoizedFn)(() => {
    if (timerRef.current) {
      clearTimeout(timerRef.current);
      timerRef.current = null;
    }
    bridge.complete(null);
  });
  const handleRemove = (0, import_ahooks.useMemoizedFn)(() => {
    if (!pending) return;
    if (timerRef.current) {
      clearTimeout(timerRef.current);
      timerRef.current = null;
    }
    if (pending.payload.mode !== "edit") {
      bridge.complete(null);
      return;
    }
    bridge.complete({ mode: "remove" });
  });
  return /* @__PURE__ */ import_react.default.createElement(import_model.Context.Provider, { value: bookingContextValue }, /* @__PURE__ */ import_react.default.createElement(
    import_skuDetailModal.default,
    {
      ref: modalRef,
      onConfirm: handleConfirm,
      onClose: handleClose,
      onRemove: handleRemove
    }
  ));
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  NormalProductDetailHostRenderer
});
