const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_model = require("../../../components/booking/info/model.js");
const require_index = require("../../../pro/skuDetailModal/index.js");
const require_normalProductDetailHostController = require("./normalProductDetailHostController.js");
const require_useSalesSdk = require("../hooks/useSalesSdk.js");
const require_transformNormalProductConfirm = require("./transformNormalProductConfirm.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
//#region src/plus/salesSdk/normalProductDetail/NormalProductDetailHostRenderer.tsx
/** pending 长期未打开弹窗时的兜底超时 */
const OPEN_NORMAL_PRODUCT_DETAIL_TIMEOUT_MS = 12e4;
/**
* 挂载在 SalesSdkProvider 子树内，承接普通商品 SkuDetailModal Host 的渲染。
* 不依赖 miniRedux / openNormalService 全局 handler。
*/
const NormalProductDetailHostRenderer = () => {
	const pending = (0, react.useSyncExternalStore)(require_normalProductDetailHostController.subscribeNormalProductDetailHost, require_normalProductDetailHostController.getNormalProductDetailHostPending, () => null);
	const { bookingTicket } = require_useSalesSdk.useSalesSdk();
	const modalRef = (0, react.useRef)(null);
	const timerRef = (0, react.useRef)(null);
	const bookingContextValue = (0, react.useMemo)(() => {
		var _bookingTicket$getBoo, _bookingTicket$getBoo2;
		return { state: { bookingConfig: (_bookingTicket$getBoo = bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getBoo2 = bookingTicket.getBookingConfig) === null || _bookingTicket$getBoo2 === void 0 ? void 0 : _bookingTicket$getBoo2.call(bookingTicket)) !== null && _bookingTicket$getBoo !== void 0 ? _bookingTicket$getBoo : null } };
	}, [bookingTicket, pending === null || pending === void 0 ? void 0 : pending.payload]);
	(0, react.useEffect)(() => {
		var _modalRef$current2, _payload$cacheItem;
		if (!pending) {
			if (timerRef.current) {
				clearTimeout(timerRef.current);
				timerRef.current = null;
			}
			return;
		}
		timerRef.current = setTimeout(() => {
			if (require_normalProductDetailHostController.getNormalProductDetailHostPending()) {
				console.error("[SalesSdk] openNormalProductDetail 超时：SkuDetailModal 未响应");
				require_normalProductDetailHostController.completeNormalProductDetailHost(null);
			}
		}, OPEN_NORMAL_PRODUCT_DETAIL_TIMEOUT_MS);
		const payload = pending.payload;
		if (payload.mode === "create") {
			var _detailPayload$produc, _modalRef$current;
			const detailPayload = payload.requiresDetailPayload;
			const item = (_detailPayload$produc = detailPayload.productData) !== null && _detailPayload$produc !== void 0 ? _detailPayload$produc : detailPayload.item;
			const rawDate = detailPayload.date;
			const date = (0, dayjs.default)(rawDate).isValid() ? (0, dayjs.default)(rawDate).format("YYYY-MM-DD") : (0, dayjs.default)().format("YYYY-MM-DD");
			(_modalRef$current = modalRef.current) === null || _modalRef$current === void 0 || _modalRef$current.open({
				id: item === null || item === void 0 ? void 0 : item.id,
				date,
				is_eject: detailPayload.isEject,
				productData: item,
				skuDetailConfig: (item === null || item === void 0 ? void 0 : item.skuDetailConfig) || {}
			});
			return;
		}
		(_modalRef$current2 = modalRef.current) === null || _modalRef$current2 === void 0 || _modalRef$current2.open(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, payload.cacheItem), {}, { skuDetailConfig: payload.skuDetailConfig || ((_payload$cacheItem = payload.cacheItem) === null || _payload$cacheItem === void 0 ? void 0 : _payload$cacheItem.skuDetailConfig) || {} }));
		return () => {
			if (timerRef.current) {
				clearTimeout(timerRef.current);
				timerRef.current = null;
			}
		};
	}, [pending]);
	const handleConfirm = (data) => {
		if (!pending) return;
		if (timerRef.current) {
			clearTimeout(timerRef.current);
			timerRef.current = null;
		}
		if (pending.payload.mode === "create") {
			var _detailPayload$produc2, _item$id;
			const detailPayload = pending.payload.requiresDetailPayload;
			const item = (_detailPayload$produc2 = detailPayload.productData) !== null && _detailPayload$produc2 !== void 0 ? _detailPayload$produc2 : detailPayload.item;
			require_normalProductDetailHostController.completeNormalProductDetailHost({
				mode: "create",
				detailResult: require_transformNormalProductConfirm.transformNormalProductCreateConfirm(Number((_item$id = item === null || item === void 0 ? void 0 : item.id) !== null && _item$id !== void 0 ? _item$id : item === null || item === void 0 ? void 0 : item.product_id), data)
			});
			return;
		}
		require_normalProductDetailHostController.completeNormalProductDetailHost({
			mode: "edit",
			cacheItem: require_transformNormalProductConfirm.prepareNormalProductEditCacheItem(data)
		});
	};
	const handleClose = () => {
		if (timerRef.current) {
			clearTimeout(timerRef.current);
			timerRef.current = null;
		}
		require_normalProductDetailHostController.completeNormalProductDetailHost(null);
	};
	return /* @__PURE__ */ react.default.createElement(require_model.Context.Provider, { value: bookingContextValue }, /* @__PURE__ */ react.default.createElement(require_index.default, {
		ref: modalRef,
		onConfirm: handleConfirm,
		onClose: handleClose
	}));
};
//#endregion
exports.NormalProductDetailHostRenderer = NormalProductDetailHostRenderer;
