const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_buildBookingEditDrawerState = require("../bookingEditService/buildBookingEditDrawerState.js");
const require_useSalesSdk = require("../hooks/useSalesSdk.js");
const require_BookingEditServiceDrawer = require("../bookingEditService/BookingEditServiceDrawer.js");
const require_prepareCacheItemForBookingEdit = require("../bookingEditService/prepareCacheItemForBookingEdit.js");
const require_bookingEditHostController = require("../bookingEditService/bookingEditHostController.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
//#region src/plus/salesSdk/hosts/BookingEditHostRenderer.tsx
/**
* 挂载在 SalesSdkProvider 子树内，承接 `openBookingEdit` Promise Host 的抽屉渲染。
*/
const BookingEditHostRenderer = () => {
	const pending = (0, react.useSyncExternalStore)(require_bookingEditHostController.subscribeBookingEditHost, require_bookingEditHostController.getBookingEditHostPending, () => null);
	const { bookingTicket, tempOrder } = require_useSalesSdk.useSalesSdk();
	const [cacheItem, setCacheItem] = (0, react.useState)(null);
	const open = !!pending;
	const payload = pending === null || pending === void 0 ? void 0 : pending.payload;
	(0, react.useEffect)(() => {
		if (!open) {
			setCacheItem(null);
			return;
		}
		if (!(payload === null || payload === void 0 ? void 0 : payload.cacheItem)) {
			console.error("[SalesSdk] openBookingEdit: 缺少 cacheItem");
			require_bookingEditHostController.completeBookingEditHost(null);
			return;
		}
		if (!bookingTicket) {
			setCacheItem(null);
			return;
		}
		let item = payload.cacheItem;
		if (typeof bookingTicket.getProductExtend === "function") try {
			item = bookingTicket.getProductExtend(item);
		} catch (e) {
			console.warn("[SalesSdk] getProductExtend 失败，使用原始 cacheItem", e);
		}
		item = require_prepareCacheItemForBookingEdit.prepareCacheItemForBookingEdit(item);
		if (item.autoClose && !payload.forceOpen) {
			require_bookingEditHostController.completeBookingEditHost(item);
			return;
		}
		if (payload.forceOpen) item = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { autoClose: false });
		setCacheItem(item);
	}, [
		open,
		payload === null || payload === void 0 ? void 0 : payload.cacheItem,
		payload === null || payload === void 0 ? void 0 : payload.forceOpen,
		bookingTicket
	]);
	/** pending 已创建但 bookingTicket 长期未就绪时，避免 addProductWithFlow 永久挂起 */
	(0, react.useEffect)(() => {
		if (!open || bookingTicket) return;
		const timer = setTimeout(() => {
			if (require_bookingEditHostController.getBookingEditHostPending()) {
				console.error("[SalesSdk] openBookingEdit: bookingTicket 未就绪，已取消");
				require_bookingEditHostController.completeBookingEditHost(null);
			}
		}, 8e3);
		return () => clearTimeout(timer);
	}, [open, bookingTicket]);
	/**
	* 当天全局资源池与 cacheItem 无关；若随 cacheItem 重建会得到新数组引用，
	* 触发 Drawer 内 getResources → updateCacheItem 死循环。
	*/
	const resourcesOrigin = (0, react.useMemo)(() => {
		var _bookingTicket$getRes, _bookingTicket$getRes2;
		if (!open || !bookingTicket) return [];
		return (_bookingTicket$getRes = (_bookingTicket$getRes2 = bookingTicket.getResourcesOrigin) === null || _bookingTicket$getRes2 === void 0 ? void 0 : _bookingTicket$getRes2.call(bookingTicket)) !== null && _bookingTicket$getRes !== void 0 ? _bookingTicket$getRes : [];
	}, [
		open,
		bookingTicket,
		payload === null || payload === void 0 ? void 0 : payload.date
	]);
	const bookingState = (0, react.useMemo)(() => {
		var _bookingTicket$getBoo, _bookingTicket$getBoo2, _ref, _payload$date, _bookingTicket$getBoo3, _bookingTicket$getTem, _bookingTicket$getTem2, _bookingTicket$getOrd;
		if (!cacheItem) return null;
		const 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;
		const date = (_ref = (_payload$date = payload === null || payload === void 0 ? void 0 : payload.date) !== null && _payload$date !== void 0 ? _payload$date : bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getBoo3 = bookingTicket.getBookingDate) === null || _bookingTicket$getBoo3 === void 0 ? void 0 : _bookingTicket$getBoo3.call(bookingTicket)) !== null && _ref !== void 0 ? _ref : (0, dayjs.default)().format("YYYY-MM-DD");
		const liveTempOrder = (_bookingTicket$getTem = bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getTem2 = bookingTicket.getTempOrder) === null || _bookingTicket$getTem2 === void 0 ? void 0 : _bookingTicket$getTem2.call(bookingTicket)) !== null && _bookingTicket$getTem !== void 0 ? _bookingTicket$getTem : tempOrder;
		return require_buildBookingEditDrawerState.buildBookingEditDrawerState({
			cacheItem,
			bookingConfig,
			date,
			customerId: require_buildBookingEditDrawerState.resolveBookingEditCustomerId(liveTempOrder, payload === null || payload === void 0 ? void 0 : payload.customerId, bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getOrd = bookingTicket.getOrderCustomerSnapshot) === null || _bookingTicket$getOrd === void 0 || (_bookingTicket$getOrd = _bookingTicket$getOrd.call(bookingTicket)) === null || _bookingTicket$getOrd === void 0 ? void 0 : _bookingTicket$getOrd.id),
			amountSymbol: (liveTempOrder === null || liveTempOrder === void 0 ? void 0 : liveTempOrder.currency_symbol) || (bookingConfig === null || bookingConfig === void 0 ? void 0 : bookingConfig.currency_symbol) || "$",
			locale: _pisell_utils.locales.locale,
			resourcesOrigin
		});
	}, [
		cacheItem,
		payload,
		bookingTicket,
		tempOrder,
		resourcesOrigin
	]);
	if (!open || !cacheItem || !bookingState) return null;
	return /* @__PURE__ */ react.default.createElement(require_BookingEditServiceDrawer.BookingEditServiceDrawer, {
		open,
		cacheItem,
		bookingState,
		onCacheItemChange: setCacheItem,
		onConfirm: (edited) => require_bookingEditHostController.completeBookingEditHost(edited),
		onCancel: () => require_bookingEditHostController.completeBookingEditHost(null),
		showNoteButton: true
	});
};
//#endregion
exports.BookingEditHostRenderer = BookingEditHostRenderer;
