import withResponsive from "../../responsive/withResponsive.js";
import { useBigSaleDataSource } from "./hooks/useBigSaleDataSource.js";
import { BigSaleTemplateRenderer } from "./templates/index.js";
import React, { useEffect, useMemo, useRef } from "react";
import classNames from "classnames";
//#region src/components/bigSale/Base.tsx
const DEFAULT_MODE = "ticket";
const SECONDARY_SCREEN_HIDDEN_SALE_DETAIL_MODULES = ["cartActions", "orderActions"];
function getSaleDetailModuleConfig({ template }) {
	if (template !== "secondaryScreen") return void 0;
	return {
		hidden: SECONDARY_SCREEN_HIDDEN_SALE_DETAIL_MODULES,
		amountDetails: { defaultExpanded: true }
	};
}
/**
* BigSaleBase 位于 SalesSdkProvider 内部，负责把 SDK 数据分发给商品列表和 SaleDetailBase。
*/
function BigSaleBase({ className, style, mode = DEFAULT_MODE, tmpl, template = "standard", variant = "desktop", productLoadParams, productDisplayConfig, saleDetailTemplate, autoCreate = true, orderId, locale, saleDetailWrapperProps }) {
	const dataSource = useBigSaleDataSource();
	const currentTemplate = tmpl || template || "standard";
	const autoCreateStartedRef = useRef(false);
	const resolvedSaleDetailModuleConfig = useMemo(() => getSaleDetailModuleConfig({ template: currentTemplate }), [currentTemplate]);
	const isSecondaryTemplate = currentTemplate === "secondaryScreen";
	useEffect(() => {
		if (!autoCreate || orderId || autoCreateStartedRef.current) return;
		if (!dataSource.sales.bookingTicket || dataSource.sales.tempOrder) return;
		if (dataSource.sales.status !== "idle") return;
		autoCreateStartedRef.current = true;
		dataSource.sales.createNew().catch(() => {
			autoCreateStartedRef.current = false;
		});
	}, [
		autoCreate,
		orderId,
		dataSource.sales,
		dataSource.sales.bookingTicket,
		dataSource.sales.status,
		dataSource.sales.tempOrder
	]);
	return /* @__PURE__ */ React.createElement("div", {
		className: classNames("pisell-big-sale", `pisell-big-sale--${variant}`, `pisell-big-sale-tmpl--${currentTemplate}`, { "pisell-big-sale--secondary-screen": isSecondaryTemplate }, className),
		style
	}, /* @__PURE__ */ React.createElement(BigSaleTemplateRenderer, {
		mode,
		tmpl: currentTemplate,
		variant,
		dataSource,
		productLoadParams,
		productDisplayConfig,
		saleDetailTemplate,
		saleDetailModuleConfig: resolvedSaleDetailModuleConfig,
		orderId,
		locale,
		saleDetailWrapperProps: isSecondaryTemplate ? saleDetailWrapperProps : void 0
	}));
}
BigSaleBase.displayName = "BigSaleBase";
const ResponsiveBigSaleBase = withResponsive(BigSaleBase);
//#endregion
export { ResponsiveBigSaleBase };
