const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_useRuntime = require("../ticketBooking/hooks/useRuntime.js");
const require_useTicketBookingSecondaryScreen = require("../ticketBooking/components/ticketBooking/useTicketBookingSecondaryScreen.js");
const require_bookingDate = require("../../plus/salesSdk/utils/bookingDate.js");
const require_useSalesSdk = require("../../plus/salesSdk/hooks/useSalesSdk.js");
const require_SalesSdkProvider = require("../../plus/salesSdk/context/SalesSdkProvider.js");
const require_SalesSdkBookingContext = require("../../plus/salesSdk/context/SalesSdkBookingContext.js");
const require_createCompositeOpenProductDetailHost = require("../../plus/salesSdk/hosts/createCompositeOpenProductDetailHost.js");
const require_createOpenBookingEditHost = require("../../plus/salesSdk/hosts/createOpenBookingEditHost.js");
const require_createOpenMultiDaySelectHost = require("../../plus/salesSdk/hosts/createOpenMultiDaySelectHost.js");
const require_createOpenHolderSelectHost = require("../../plus/salesSdk/hosts/createOpenHolderSelectHost.js");
const require_useSalesSdkCustomer = require("../../plus/salesSdk/hooks/useSalesSdkCustomer.js");
const require_Base = require("./Base.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
require("./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 && (0, dayjs.default)(rawDate).isValid()) return (0, dayjs.default)(rawDate).format("YYYY-MM-DD");
	return fallback || (0, dayjs.default)().format("YYYY-MM-DD");
}
/**
* 同步客户与预约日期给 legacy 商品详情 Host，Host 打开时读取最新运行态。
*/
const BigSaleHostRuntimeBridge = ({ runtimeRef }) => {
	var _customer$selected2;
	const sales = require_useSalesSdk.useSalesSdk();
	const customer = require_useSalesSdkCustomer.useSalesSdkCustomer();
	(0, react.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 require_bookingDate.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 = require_useEngineContext.default();
	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 } = require_useRuntime.useRuntime();
	const { isSecondaryScreen, secondaryProductsPerRow, showSecondaryCustomerInfo, mainContentWrapperProps, secondaryScreenSettingsModal } = require_useTicketBookingSecondaryScreen.useTicketBookingSecondaryScreen(runtime, secondaryScreenDisplay);
	const resolvedTemplate = (_ref = tmpl !== null && tmpl !== void 0 ? tmpl : template) !== null && _ref !== void 0 ? _ref : isSecondaryScreen ? "secondaryScreen" : "standard";
	const runtimeRef = (0, react.useRef)({
		date: void 0,
		customerId: void 0
	});
	const resolvedUiHosts = (0, react.useMemo)(() => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, action ? { openProductDetail: require_createCompositeOpenProductDetailHost.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 require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, runtimeRef.current), {}, { date: resolveRuntimeDate(rawDate, runtimeRef.current.date) });
		}
	}) } : {}), {}, {
		openBookingEdit: require_createOpenBookingEditHost.createOpenBookingEditHost(),
		openMultiDaySelect: require_createOpenMultiDaySelectHost.createOpenMultiDaySelectHost(),
		openHolderSelect: require_createOpenHolderSelectHost.createOpenHolderSelectHost()
	}, uiHosts || {}), [
		action,
		osKey,
		pisellos,
		uiHosts
	]);
	return /* @__PURE__ */ react.default.createElement(require_SalesSdkProvider.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.default.createElement(require_SalesSdkBookingContext.SalesSdkBookingContextProvider, null, /* @__PURE__ */ react.default.createElement(BigSaleHostRuntimeBridge, { runtimeRef }), /* @__PURE__ */ react.default.createElement(require_Base.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
exports.default = BigSale;
