import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import { useRuntime } from "../ticketBooking/hooks/useRuntime.js";
import { useTicketBookingSecondaryScreen } from "../ticketBooking/components/ticketBooking/useTicketBookingSecondaryScreen.js";
import { subscribeBookingDateChange } from "../../plus/salesSdk/utils/bookingDate.js";
import { useSalesSdk } from "../../plus/salesSdk/hooks/useSalesSdk.js";
import { SalesSdkProvider } from "../../plus/salesSdk/context/SalesSdkProvider.js";
import { SalesSdkBookingContextProvider } from "../../plus/salesSdk/context/SalesSdkBookingContext.js";
import { createCompositeOpenProductDetailHost } from "../../plus/salesSdk/hosts/createCompositeOpenProductDetailHost.js";
import { createOpenBookingEditHost } from "../../plus/salesSdk/hosts/createOpenBookingEditHost.js";
import { createOpenMultiDaySelectHost } from "../../plus/salesSdk/hosts/createOpenMultiDaySelectHost.js";
import { createOpenHolderSelectHost } from "../../plus/salesSdk/hosts/createOpenHolderSelectHost.js";
import { useSalesSdkCustomer } from "../../plus/salesSdk/hooks/useSalesSdkCustomer.js";
import "../../plus/salesSdk/index.js";
import { ResponsiveBigSaleBase } from "./Base.js";
import dayjs from "dayjs";
import React, { useEffect, useMemo, useRef } from "react";
import "./index.less";
//#region src/components/bigSale/index.tsx
const DEFAULT_OS_KEY = "big-sale";
const getBookingTicketKey = (osKey) => {
	return `bookingTicket-${osKey}`;
};
function resolveRuntimeDate(rawDate, fallback) {
	if (rawDate && dayjs(rawDate).isValid()) return dayjs(rawDate).format("YYYY-MM-DD");
	return fallback || dayjs().format("YYYY-MM-DD");
}
/**
* 同步客户与预约日期给 legacy 商品详情 Host，Host 打开时读取最新运行态。
*/
const BigSaleHostRuntimeBridge = ({ runtimeRef }) => {
	var _customer$selected2;
	const sales = useSalesSdk();
	const customer = useSalesSdkCustomer();
	useEffect(() => {
		const syncRuntime = () => {
			var _sales$bookingTicket$, _sales$bookingTicket, _sales$bookingTicket$2, _sales$bookingTicket2, _sales$bookingTicket3, _customer$selected;
			const rawDate = (_sales$bookingTicket$ = (_sales$bookingTicket = sales.bookingTicket) === null || _sales$bookingTicket === void 0 || (_sales$bookingTicket$2 = _sales$bookingTicket.getBookingDate) === null || _sales$bookingTicket$2 === void 0 ? void 0 : _sales$bookingTicket$2.call(_sales$bookingTicket)) !== null && _sales$bookingTicket$ !== void 0 ? _sales$bookingTicket$ : (_sales$bookingTicket2 = sales.bookingTicket) === null || _sales$bookingTicket2 === void 0 || (_sales$bookingTicket2 = _sales$bookingTicket2.store) === null || _sales$bookingTicket2 === void 0 || (_sales$bookingTicket2 = _sales$bookingTicket2.bookingContext) === null || _sales$bookingTicket2 === void 0 || (_sales$bookingTicket3 = _sales$bookingTicket2.getState) === null || _sales$bookingTicket3 === void 0 || (_sales$bookingTicket3 = _sales$bookingTicket3.call(_sales$bookingTicket2)) === null || _sales$bookingTicket3 === void 0 ? void 0 : _sales$bookingTicket3.date;
			runtimeRef.current = {
				customerId: (_customer$selected = customer.selected) === null || _customer$selected === void 0 ? void 0 : _customer$selected.id,
				date: resolveRuntimeDate(rawDate)
			};
		};
		syncRuntime();
		return subscribeBookingDateChange(sales.bookingTicket, syncRuntime);
	}, [
		sales.bookingTicket,
		(_customer$selected2 = customer.selected) === null || _customer$selected2 === void 0 ? void 0 : _customer$selected2.id,
		runtimeRef
	]);
	return null;
};
/**
* BigSale 入口：只创建 SalesSdkProvider 运行环境，UI 由 ResponsiveBigSaleBase 承载。
*/
const BigSale = ({ osKey = DEFAULT_OS_KEY, orderId, autoBootstrap, businessCode, bookingConfigParams, autoInitBookingContext, otherParams, rulesHooks, uiHosts, mode = "ticket", tmpl, template, secondaryScreenDisplay, productLoadParams, productDisplayConfig, saleDetailTemplate, autoCreate = true, enableTempOrderSync = true, isActive = true, className, style, children }) => {
	var _context$appHelper, _context$appHelper2, _context$appHelper2$g, _context$appHelper3, _ref, _app$locales, _app$locales$getLocal;
	const context = useEngineContext();
	const action = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.action;
	const app = context === null || context === void 0 || (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 || (_context$appHelper2$g = _context$appHelper2.getApp) === null || _context$appHelper2$g === void 0 ? void 0 : _context$appHelper2$g.call(_context$appHelper2);
	const pisellos = context === null || context === void 0 || (_context$appHelper3 = context.appHelper) === null || _context$appHelper3 === void 0 || (_context$appHelper3 = _context$appHelper3.utils) === null || _context$appHelper3 === void 0 ? void 0 : _context$appHelper3.pisellos;
	const { runtime } = useRuntime();
	const { isSecondaryScreen, secondaryProductsPerRow, showSecondaryCustomerInfo, mainContentWrapperProps, secondaryScreenSettingsModal } = useTicketBookingSecondaryScreen(runtime, secondaryScreenDisplay);
	const resolvedTemplate = (_ref = tmpl !== null && tmpl !== void 0 ? tmpl : template) !== null && _ref !== void 0 ? _ref : isSecondaryScreen ? "secondaryScreen" : "standard";
	const runtimeRef = useRef({
		date: void 0,
		customerId: void 0
	});
	const resolvedUiHosts = useMemo(() => _objectSpread2(_objectSpread2({}, action ? { openProductDetail: createCompositeOpenProductDetailHost({
		action,
		getRuntime: () => {
			var _pisellos$getModule, _liveBookingTicket$st, _liveBookingTicket$st2, _liveBookingTicket$ge, _liveBookingTicket$ge2;
			const liveBookingTicket = pisellos === null || pisellos === void 0 || (_pisellos$getModule = pisellos.getModule) === null || _pisellos$getModule === void 0 ? void 0 : _pisellos$getModule.call(pisellos, getBookingTicketKey(osKey));
			const bookingContext = liveBookingTicket === null || liveBookingTicket === void 0 || (_liveBookingTicket$st = liveBookingTicket.store) === null || _liveBookingTicket$st === void 0 || (_liveBookingTicket$st = _liveBookingTicket$st.bookingContext) === null || _liveBookingTicket$st === void 0 || (_liveBookingTicket$st2 = _liveBookingTicket$st.getState) === null || _liveBookingTicket$st2 === void 0 ? void 0 : _liveBookingTicket$st2.call(_liveBookingTicket$st);
			const rawDate = (_liveBookingTicket$ge = liveBookingTicket === null || liveBookingTicket === void 0 || (_liveBookingTicket$ge2 = liveBookingTicket.getBookingDate) === null || _liveBookingTicket$ge2 === void 0 ? void 0 : _liveBookingTicket$ge2.call(liveBookingTicket)) !== null && _liveBookingTicket$ge !== void 0 ? _liveBookingTicket$ge : bookingContext === null || bookingContext === void 0 ? void 0 : bookingContext.date;
			return _objectSpread2(_objectSpread2({}, runtimeRef.current), {}, { date: resolveRuntimeDate(rawDate, runtimeRef.current.date) });
		}
	}) } : {}), {}, {
		openBookingEdit: createOpenBookingEditHost(),
		openMultiDaySelect: createOpenMultiDaySelectHost(),
		openHolderSelect: createOpenHolderSelectHost()
	}, uiHosts || {}), [
		action,
		osKey,
		pisellos,
		uiHosts
	]);
	return /* @__PURE__ */ React.createElement(SalesSdkProvider, {
		osKey: `${osKey}-${mode}`,
		orderId,
		autoBootstrap: autoBootstrap !== null && autoBootstrap !== void 0 ? autoBootstrap : Boolean(orderId),
		businessCode,
		bookingConfigParams,
		autoInitBookingContext,
		otherParams,
		rulesHooks: void 0,
		uiHosts: resolvedUiHosts,
		enableTempOrderSync,
		isActive
	}, /* @__PURE__ */ React.createElement(SalesSdkBookingContextProvider, null, /* @__PURE__ */ React.createElement(BigSaleHostRuntimeBridge, { runtimeRef }), /* @__PURE__ */ React.createElement(ResponsiveBigSaleBase, {
		mode,
		template: resolvedTemplate,
		productLoadParams,
		productDisplayConfig,
		saleDetailTemplate,
		autoCreate,
		orderId,
		className,
		style,
		locale: (app === null || app === void 0 || (_app$locales = app.locales) === null || _app$locales === void 0 || (_app$locales$getLocal = _app$locales.getLocale) === null || _app$locales$getLocal === void 0 ? void 0 : _app$locales$getLocal.call(_app$locales)) || "en",
		saleDetailWrapperProps: mainContentWrapperProps,
		cartProductsPerRow: secondaryProductsPerRow,
		showClient: showSecondaryCustomerInfo
	}), children, secondaryScreenSettingsModal));
};
BigSale.displayName = "BigSale";
//#endregion
export { BigSale as default };
