import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { Context } from "../../../components/booking/info/model.js";
import skuDetailModal_default from "../../../pro/skuDetailModal/index.js";
import { completeNormalProductDetailHost, getNormalProductDetailHostPending, subscribeNormalProductDetailHost } from "./normalProductDetailHostController.js";
import { useSalesSdk } from "../hooks/useSalesSdk.js";
import { prepareNormalProductEditCacheItem, transformNormalProductCreateConfirm } from "./transformNormalProductConfirm.js";
import dayjs from "dayjs";
import React, { useEffect, useMemo, useRef, useSyncExternalStore } from "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 = useSyncExternalStore(subscribeNormalProductDetailHost, getNormalProductDetailHostPending, () => null);
	const { bookingTicket } = useSalesSdk();
	const modalRef = useRef(null);
	const timerRef = useRef(null);
	const bookingContextValue = 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]);
	useEffect(() => {
		var _modalRef$current2, _payload$cacheItem;
		if (!pending) {
			if (timerRef.current) {
				clearTimeout(timerRef.current);
				timerRef.current = null;
			}
			return;
		}
		timerRef.current = setTimeout(() => {
			if (getNormalProductDetailHostPending()) {
				console.error("[SalesSdk] openNormalProductDetail 超时：SkuDetailModal 未响应");
				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 = dayjs(rawDate).isValid() ? dayjs(rawDate).format("YYYY-MM-DD") : dayjs().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(_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;
			completeNormalProductDetailHost({
				mode: "create",
				detailResult: 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;
		}
		completeNormalProductDetailHost({
			mode: "edit",
			cacheItem: prepareNormalProductEditCacheItem(data)
		});
	};
	const handleClose = () => {
		if (timerRef.current) {
			clearTimeout(timerRef.current);
			timerRef.current = null;
		}
		completeNormalProductDetailHost(null);
	};
	return /* @__PURE__ */ React.createElement(Context.Provider, { value: bookingContextValue }, /* @__PURE__ */ React.createElement(skuDetailModal_default, {
		ref: modalRef,
		onConfirm: handleConfirm,
		onClose: handleClose
	}));
};
//#endregion
export { NormalProductDetailHostRenderer };
