require("../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_toShoppingCartProducts = require("../scanOrder/utils/toShoppingCartProducts.js");
let react = require("react");
//#region src/components/venueBooking/hooks.ts
/**
* venueBooking 核心 hooks
* 模块获取、ViewModel 构建、入口上下文标准化、模块方法封装
*/
/** 写死的场地商品 ID（后续改为从配置读取） */
const VENUE_PRODUCT_IDS = [61947, 61948];
/**
* 获取 PisellOS venueBooking 业务模块
* 使用 useRef 稳定模块引用，防止 getModule 返回值每次渲染变化时触发 bootstrap useEffect 重跑
*/
function useVenueBookingModule() {
	const pisellOS = require_useEngineContext.usePisellOS();
	const moduleRef = (0, react.useRef)(null);
	if (!moduleRef.current) {
		var _pisellOS$getModule, _pisellOS$getModule2;
		moduleRef.current = (_pisellOS$getModule = pisellOS === null || pisellOS === void 0 || (_pisellOS$getModule2 = pisellOS.getModule) === null || _pisellOS$getModule2 === void 0 ? void 0 : _pisellOS$getModule2.call(pisellOS, "venueBooking")) !== null && _pisellOS$getModule !== void 0 ? _pisellOS$getModule : null;
	}
	return moduleRef.current;
}
/** 统一获取货币前缀（优先引擎配置，兜底 '$'） */
function useVenueBookingSymbol() {
	var _utils$getSymbolic;
	const utils = contextUtils();
	return String((utils === null || utils === void 0 || (_utils$getSymbolic = utils.getSymbolic) === null || _utils$getSymbolic === void 0 ? void 0 : _utils$getSymbolic.call(utils)) || "$");
}
/** 获取引擎上下文中的 utils 工具集（action / token 等） */
function contextUtils() {
	var _context$appHelper;
	const context = require_useEngineContext.default();
	return (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
}
/** 从 engine props 提取标准化的入口上下文 */
function normalizeEntryContext(props) {
	return {
		biz: (props === null || props === void 0 ? void 0 : props.biz) || "",
		resource_id: props === null || props === void 0 ? void 0 : props.resource_id
	};
}
/**
* 安全同步读取模块 getter 方法
* 如果方法不存在或返回 Promise，则使用 defaultValue
*/
function callModuleGetter(module, methodName, defaultValue) {
	if (typeof (module === null || module === void 0 ? void 0 : module[methodName]) !== "function") return defaultValue;
	const result = module[methodName]();
	if (result && typeof (result === null || result === void 0 ? void 0 : result.then) === "function") return defaultValue;
	return result !== null && result !== void 0 ? result : defaultValue;
}
/**
* 安全调用模块异步方法
* 检查模块和方法是否存在，不存在时抛出友好错误
*/
function safeRunModuleAction(_x, _x2) {
	return _safeRunModuleAction.apply(this, arguments);
}
function _safeRunModuleAction() {
	_safeRunModuleAction = require_asyncToGenerator._asyncToGenerator(function* (module, actionName, ...args) {
		if (!module) throw new Error("venueBooking module not found");
		if (typeof (module === null || module === void 0 ? void 0 : module[actionName]) !== "function") throw new Error(`venueBooking action "${actionName}" not found`);
		return module[actionName](...args);
	});
	return _safeRunModuleAction.apply(this, arguments);
}
/** 加载场地商品（初始化时调用一次） */
function loadVenueProducts(_x3, _x4) {
	return _loadVenueProducts.apply(this, arguments);
}
function _loadVenueProducts() {
	_loadVenueProducts = require_asyncToGenerator._asyncToGenerator(function* (module, productIds) {
		return safeRunModuleAction(module, "loadAllProducts", { productIds });
	});
	return _loadVenueProducts.apply(this, arguments);
}
/** 拉取资源可用性数据（按日期范围，翻周时调用） */
function fetchResourceAvailability(_x5, _x6, _x7) {
	return _fetchResourceAvailability.apply(this, arguments);
}
function _fetchResourceAvailability() {
	_fetchResourceAvailability = require_asyncToGenerator._asyncToGenerator(function* (module, startDate, endDate) {
		return safeRunModuleAction(module, "fetchResourceAvailability", {
			startDate,
			endDate
		});
	});
	return _fetchResourceAvailability.apply(this, arguments);
}
/** 获取日期概览（纯计算不发请求，fetchResourceAvailability 后调用） */
function getDateRangeSummary(module, startDate, endDate) {
	if (!module || typeof module.getDateRangeSummary !== "function") return [];
	return module.getDateRangeSummary({
		startDate,
		endDate
	}) || [];
}
/** 获取某天的时段网格（纯计算不发请求，切换日期时调用） */
function getTimeSlotGrid(module, date) {
	if (!module || typeof module.getTimeSlotGrid !== "function") return null;
	return module.getTimeSlotGrid(date) || null;
}
/** 切换选中/取消一个时段（同一资源下多商品场景必须携带 productId） */
function toggleSlotAction(_x8, _x9) {
	return _toggleSlotAction.apply(this, arguments);
}
function _toggleSlotAction() {
	_toggleSlotAction = require_asyncToGenerator._asyncToGenerator(function* (module, params) {
		return safeRunModuleAction(module, "toggleSlot", params);
	});
	return _toggleSlotAction.apply(this, arguments);
}
/**
* 查询某个时段是否已被选中
* 不传 productId 时：该资源下任一商品选中即 true；传了则精确匹配
*/
function isSlotSelected(module, resourceId, startTime, productId) {
	if (!module || typeof module.isSlotSelected !== "function") return false;
	return !!module.isSlotSelected(resourceId, startTime, productId);
}
/**
* 询问 OS：该 (resourceId, productId, startTime) 格子是否因其它已选项而应禁用
* 覆盖：同场地多商品互斥、组合资源 ↔ 子资源联动
*/
function isSlotDisabledBySelection(module, params) {
	if (!module || typeof module.isSlotDisabledBySelection !== "function") return false;
	return !!module.isSlotDisabledBySelection(params);
}
/** 获取全部已选时段（按资源分组） */
function getAllSelectedSlots(module) {
	if (!module || typeof module.getAllSelectedSlots !== "function") return [];
	return module.getAllSelectedSlots() || [];
}
/** 查询 otherParams */
function getOtherParams(module) {
	if (!module || typeof module.getOtherParams !== "function") return {};
	return module.getOtherParams() || {};
}
function parseDateKeyFromDateTime(value) {
	if (typeof value !== "string") return null;
	const matched = value.trim().match(/^(\d{4}-\d{2}-\d{2})/);
	if (!matched) return null;
	return matched[1];
}
function appendSlotDateKey(target, slot) {
	const candidates = [
		slot === null || slot === void 0 ? void 0 : slot.startTime,
		slot === null || slot === void 0 ? void 0 : slot.start_time,
		slot === null || slot === void 0 ? void 0 : slot.startDate,
		slot === null || slot === void 0 ? void 0 : slot.start_date
	];
	for (const candidate of candidates) {
		const dateKey = parseDateKeyFromDateTime(candidate);
		if (!dateKey) continue;
		target.add(dateKey);
		return;
	}
}
/**
* 获取所有“已选时段”对应的日期 key（YYYY-MM-DD）
* 兼容 venueBooking 模块返回 Map 或数组结构
*/
function getSelectedDateKeySet(module) {
	const selectedDateKeySet = /* @__PURE__ */ new Set();
	if (!module || typeof module.getAllSelectedSlots !== "function") return selectedDateKeySet;
	const selectedSlots = module.getAllSelectedSlots();
	if (!selectedSlots) return selectedDateKeySet;
	if (selectedSlots instanceof Map) {
		selectedSlots.forEach((slots) => {
			if (!Array.isArray(slots)) return;
			slots.forEach((slot) => appendSlotDateKey(selectedDateKeySet, slot));
		});
		return selectedDateKeySet;
	}
	if (!Array.isArray(selectedSlots)) return selectedDateKeySet;
	selectedSlots.forEach((item) => {
		if (Array.isArray(item)) {
			item.forEach((slot) => appendSlotDateKey(selectedDateKeySet, slot));
			return;
		}
		if (Array.isArray(item === null || item === void 0 ? void 0 : item.slots)) {
			item.slots.forEach((slot) => appendSlotDateKey(selectedDateKeySet, slot));
			return;
		}
		appendSlotDateKey(selectedDateKeySet, item);
	});
	return selectedDateKeySet;
}
/**
* 从 OS 模块快照构建 ViewModel（购物车部分）
* 购物车数据使用 toShoppingCartProducts 转换为 ShoppingCart 组件所需格式
*/
function createVenueBookingViewModel(module) {
	const orderProducts = callModuleGetter(module, "getOrderProducts", []);
	const summaryByGetter = callModuleGetter(module, "getSummary", null);
	const tempOrder = callModuleGetter(module, "getTempOrder", null);
	const amountSymbol = (tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.currency_symbol) || "$";
	const cartItems = require_toShoppingCartProducts.toShoppingCartProducts({
		orderProducts: Array.isArray(orderProducts) ? orderProducts : [],
		amountSymbol
	});
	const summary = (summaryByGetter && typeof summaryByGetter === "object" ? summaryByGetter : null) || (tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.summary) || {};
	return {
		cartItems: Array.isArray(cartItems) ? cartItems : [],
		summary: summary && typeof summary === "object" ? summary : {},
		note: String((tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.note) || ""),
		contactsInfo: Array.isArray(tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.contacts_info) ? tempOrder.contacts_info : []
	};
}
/** 登录状态 hook（检查 token + 弹出登录弹窗） */
function useVenueBookingLogin() {
	var _utils$token, _utils$token$get;
	const utils = contextUtils();
	const isLogin = !!(utils === null || utils === void 0 || (_utils$token = utils.token) === null || _utils$token === void 0 || (_utils$token$get = _utils$token.get) === null || _utils$token$get === void 0 ? void 0 : _utils$token$get.call(_utils$token));
	const openLoginModal = (params) => {
		const { callback } = params || {};
		utils.action({
			type: "pisell1.login",
			data: {},
			callback
		});
	};
	return {
		isLogin,
		openLoginModal
	};
}
//#endregion
exports.VENUE_PRODUCT_IDS = VENUE_PRODUCT_IDS;
exports.createVenueBookingViewModel = createVenueBookingViewModel;
exports.fetchResourceAvailability = fetchResourceAvailability;
exports.getAllSelectedSlots = getAllSelectedSlots;
exports.getDateRangeSummary = getDateRangeSummary;
exports.getOtherParams = getOtherParams;
exports.getSelectedDateKeySet = getSelectedDateKeySet;
exports.getTimeSlotGrid = getTimeSlotGrid;
exports.isSlotDisabledBySelection = isSlotDisabledBySelection;
exports.isSlotSelected = isSlotSelected;
exports.loadVenueProducts = loadVenueProducts;
exports.normalizeEntryContext = normalizeEntryContext;
exports.safeRunModuleAction = safeRunModuleAction;
exports.toggleSlotAction = toggleSlotAction;
exports.useVenueBookingLogin = useVenueBookingLogin;
exports.useVenueBookingModule = useVenueBookingModule;
exports.useVenueBookingSymbol = useVenueBookingSymbol;
