import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { buildBookingEditDrawerState, resolveBookingEditCustomerId } from "../bookingEditService/buildBookingEditDrawerState.js";
import { useSalesSdk } from "../hooks/useSalesSdk.js";
import { BookingEditServiceDrawer } from "../bookingEditService/BookingEditServiceDrawer.js";
import { prepareCacheItemForBookingEdit } from "../bookingEditService/prepareCacheItemForBookingEdit.js";
import { completeBookingEditHost, getBookingEditHostPending, subscribeBookingEditHost } from "../bookingEditService/bookingEditHostController.js";
import dayjs from "dayjs";
import React, { useEffect, useMemo, useState, useSyncExternalStore } from "react";
import { locales } from "@pisell/utils";
//#region src/plus/salesSdk/hosts/BookingEditHostRenderer.tsx
/**
* 挂载在 SalesSdkProvider 子树内，承接 `openBookingEdit` Promise Host 的抽屉渲染。
*/
const BookingEditHostRenderer = () => {
	const pending = useSyncExternalStore(subscribeBookingEditHost, getBookingEditHostPending, () => null);
	const { bookingTicket, tempOrder } = useSalesSdk();
	const [cacheItem, setCacheItem] = useState(null);
	const open = !!pending;
	const payload = pending === null || pending === void 0 ? void 0 : pending.payload;
	useEffect(() => {
		if (!open) {
			setCacheItem(null);
			return;
		}
		if (!(payload === null || payload === void 0 ? void 0 : payload.cacheItem)) {
			console.error("[SalesSdk] openBookingEdit: 缺少 cacheItem");
			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 = prepareCacheItemForBookingEdit(item);
		if (item.autoClose && !payload.forceOpen) {
			completeBookingEditHost(item);
			return;
		}
		if (payload.forceOpen) item = _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 永久挂起 */
	useEffect(() => {
		if (!open || bookingTicket) return;
		const timer = setTimeout(() => {
			if (getBookingEditHostPending()) {
				console.error("[SalesSdk] openBookingEdit: bookingTicket 未就绪，已取消");
				completeBookingEditHost(null);
			}
		}, 8e3);
		return () => clearTimeout(timer);
	}, [open, bookingTicket]);
	/**
	* 当天全局资源池与 cacheItem 无关；若随 cacheItem 重建会得到新数组引用，
	* 触发 Drawer 内 getResources → updateCacheItem 死循环。
	*/
	const resourcesOrigin = 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 = 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 : dayjs().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 buildBookingEditDrawerState({
			cacheItem,
			bookingConfig,
			date,
			customerId: 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: locales.locale,
			resourcesOrigin
		});
	}, [
		cacheItem,
		payload,
		bookingTicket,
		tempOrder,
		resourcesOrigin
	]);
	if (!open || !cacheItem || !bookingState) return null;
	return /* @__PURE__ */ React.createElement(BookingEditServiceDrawer, {
		open,
		cacheItem,
		bookingState,
		onCacheItemChange: setCacheItem,
		onConfirm: (edited) => completeBookingEditHost(edited),
		onCancel: () => completeBookingEditHost(null),
		showNoteButton: true
	});
};
//#endregion
export { BookingEditHostRenderer };
