const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_SalesSdkContext = require("./SalesSdkContext.js");
let react = require("react");
react = require_runtime.__toESM(react);
let ahooks = require("ahooks");
//#region src/plus/salesSdk/context/SalesSdkSummaryContext.tsx
const SalesSdkSummaryContext = (0, react.createContext)(null);
SalesSdkSummaryContext.displayName = "SalesSdkSummaryContext";
/**
* 仅供 SDK 内部 Provider/action 调用，不对外暴露。
*/
const SalesSdkSummaryRefreshContext = (0, react.createContext)(null);
SalesSdkSummaryRefreshContext.displayName = "SalesSdkSummaryRefreshContext";
const SalesSdkSummaryProvider = ({ children }) => {
	var _sales$bookingTicket;
	const sales = (0, react.useContext)(require_SalesSdkContext.SalesSdkContext);
	const bookingTicket = (_sales$bookingTicket = sales === null || sales === void 0 ? void 0 : sales.bookingTicket) !== null && _sales$bookingTicket !== void 0 ? _sales$bookingTicket : null;
	const [summary, setSummary] = (0, react.useState)(null);
	const requestIdRef = (0, react.useRef)(0);
	const refreshSummary = (0, ahooks.useMemoizedFn)(require_asyncToGenerator._asyncToGenerator(function* () {
		const requestId = ++requestIdRef.current;
		if (!bookingTicket) {
			setSummary(null);
			return null;
		}
		const next = yield bookingTicket.getSummary();
		if (requestId === requestIdRef.current) setSummary(next !== null && next !== void 0 ? next : null);
		return next !== null && next !== void 0 ? next : null;
	}));
	(0, react.useEffect)(() => {
		refreshSummary().catch((e) => {
			console.warn("[SalesSdk] SummaryProvider 初始拉取 summary 失败", e);
			setSummary(null);
		});
	}, [bookingTicket, refreshSummary]);
	(0, react.useEffect)(() => {
		if (!bookingTicket) return;
		const off = bookingTicket.effectsOn("onSalesOrderLoaded", () => {
			refreshSummary().catch((e) => {
				console.warn("[SalesSdk] SummaryProvider onSalesOrderLoaded 刷新失败", e);
			});
		});
		return () => {
			try {
				off();
			} catch (_unused) {}
		};
	}, [bookingTicket, refreshSummary]);
	const value = (0, react.useMemo)(() => ({ summary }), [summary]);
	return /* @__PURE__ */ react.default.createElement(SalesSdkSummaryRefreshContext.Provider, { value: refreshSummary }, /* @__PURE__ */ react.default.createElement(SalesSdkSummaryContext.Provider, { value }, children));
};
//#endregion
exports.SalesSdkSummaryContext = SalesSdkSummaryContext;
exports.SalesSdkSummaryProvider = SalesSdkSummaryProvider;
exports.SalesSdkSummaryRefreshContext = SalesSdkSummaryRefreshContext;
