const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_SalesSdkContext = require("./SalesSdkContext.js");
const require_bookingDate = require("../utils/bookingDate.js");
let react = require("react");
react = require_runtime.__toESM(react);
let ahooks = require("ahooks");
//#region src/plus/salesSdk/context/SalesSdkBookingContext.tsx
const SalesSdkBookingContext = (0, react.createContext)(null);
SalesSdkBookingContext.displayName = "SalesSdkBookingContext";
function ensureMethod(bookingTicket, method) {
	const fn = bookingTicket === null || bookingTicket === void 0 ? void 0 : bookingTicket[method];
	if (typeof fn !== "function" || !bookingTicket) throw new Error(`[SalesSdk] BookingContext: bookingTicket.${String(method)} 不可用，请升级 pisellos`);
	return fn.bind(bookingTicket);
}
/**
* BookingContext Provider —— 极薄壳：所有写都透传到 OS，所有读都调 OS 上的同名方法。
*
* 不持有 React state（避免与 OS 数据不一致）；如果消费方需要订阅 booking config 变更，
* 通过 `useSalesSdk().bookingTicket?.effectsOn('bookingContext:onBookingConfigChange', cb)` 自订阅。
*/
const SalesSdkBookingContextProvider = ({ 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 setBookingConfig = (0, ahooks.useMemoizedFn)((config) => {
		ensureMethod(bookingTicket, "setBookingConfig")(config);
	});
	const getBookingConfig = (0, ahooks.useMemoizedFn)(() => {
		var _bookingTicket$getBoo;
		return (bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getBoo = bookingTicket.getBookingConfig) === null || _bookingTicket$getBoo === void 0 ? void 0 : _bookingTicket$getBoo.call(bookingTicket)) || null;
	});
	const setResources = (0, ahooks.useMemoizedFn)((resources) => {
		ensureMethod(bookingTicket, "setResources")(resources);
	});
	const getResourcesOrigin = (0, ahooks.useMemoizedFn)(() => {
		var _bookingTicket$getRes;
		return (bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getRes = bookingTicket.getResourcesOrigin) === null || _bookingTicket$getRes === void 0 ? void 0 : _bookingTicket$getRes.call(bookingTicket)) || [];
	});
	const getResourcesOriginMap = (0, ahooks.useMemoizedFn)(() => {
		var _bookingTicket$getRes2;
		return (bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getRes2 = bookingTicket.getResourcesOriginMap) === null || _bookingTicket$getRes2 === void 0 ? void 0 : _bookingTicket$getRes2.call(bookingTicket)) || {};
	});
	/**
	* 写入 BookingContext 当前预约时间（统一 `YYYY-MM-DD HH:mm:ss`）。
	*
	* @example
	* setDate(dayjs()); // TimeBar 点「现在」
	* setDate('2026-05-25 14:30:00');
	*/
	const setDate = (0, ahooks.useMemoizedFn)((date) => {
		if (!(bookingTicket === null || bookingTicket === void 0 ? void 0 : bookingTicket.setBookingDate)) {
			console.warn("[SalesSdk] setDate: bookingTicket 未就绪，跳过写入");
			return;
		}
		const normalized = require_bookingDate.formatBookingDateTime(date);
		bookingTicket.setBookingDate(normalized !== null && normalized !== void 0 ? normalized : date);
	});
	const getDate = (0, ahooks.useMemoizedFn)(() => {
		var _bookingTicket$getBoo2;
		return (bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getBoo2 = bookingTicket.getBookingDate) === null || _bookingTicket$getBoo2 === void 0 ? void 0 : _bookingTicket$getBoo2.call(bookingTicket)) || null;
	});
	const getResourcesForProduct = (0, ahooks.useMemoizedFn)((product, options) => ensureMethod(bookingTicket, "getResourcesForProduct")(product, options));
	const getProductExtend = (0, ahooks.useMemoizedFn)((cacheItem, extra) => ensureMethod(bookingTicket, "getProductExtend")(cacheItem, extra));
	const getResourceErrors = (0, ahooks.useMemoizedFn)((resource, cacheItem) => ensureMethod(bookingTicket, "getResourceErrors")(resource, cacheItem));
	const value = (0, react.useMemo)(() => ({
		setBookingConfig,
		getBookingConfig,
		setResources,
		getResourcesOrigin,
		getResourcesOriginMap,
		setDate,
		getDate,
		getResourcesForProduct,
		getProductExtend,
		getResourceErrors
	}), [
		setBookingConfig,
		getBookingConfig,
		setResources,
		getResourcesOrigin,
		getResourcesOriginMap,
		setDate,
		getDate,
		getResourcesForProduct,
		getProductExtend,
		getResourceErrors
	]);
	return /* @__PURE__ */ react.default.createElement(SalesSdkBookingContext.Provider, { value }, children);
};
function useSalesSdkBookingContext() {
	const ctx = (0, react.useContext)(SalesSdkBookingContext);
	if (!ctx) throw new Error("[SalesSdk] useSalesSdkBookingContext 必须在 <SalesSdkBookingContextProvider> 内调用");
	return ctx;
}
//#endregion
exports.SalesSdkBookingContextProvider = SalesSdkBookingContextProvider;
exports.useSalesSdkBookingContext = useSalesSdkBookingContext;
