const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_formResourcePage = require("../serve/formResourcePage.js");
const require_associatedMenuListIdsFromOpenData = require("../data/associatedMenuListIdsFromOpenData.js");
const require_reservationRuleProducts = require("../data/reservationRuleProducts.js");
const require_productQuery = require("../serve/productQuery.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
//#region src/plus/pisellReservation/hooks/usePicoCodingOpenData.ts
const CHANNEL = "pos";
const SCOPE = "board";
function loadPicoCodingOpenDataSnapshot(_x, _x2) {
	return _loadPicoCodingOpenDataSnapshot.apply(this, arguments);
}
function _loadPicoCodingOpenDataSnapshot() {
	_loadPicoCodingOpenDataSnapshot = require_asyncToGenerator._asyncToGenerator(function* (engineCtx, BIZ) {
		var _engineCtx$appHelper, _engineCtx$appHelper$, _engineCtx$appHelper2, _engineCtx$appHelper3;
		const emptyResult = {
			picoCodingOpenData: null,
			reservationRuleProduct: null,
			reservationAssociatedMenuListIds: []
		};
		if (!engineCtx || !BIZ) return emptyResult;
		const res = yield require_formResourcePage.getPicoCodingOpenData({
			shop_id: engineCtx === null || engineCtx === void 0 || (_engineCtx$appHelper = engineCtx.appHelper) === null || _engineCtx$appHelper === void 0 || (_engineCtx$appHelper = _engineCtx$appHelper.utils) === null || _engineCtx$appHelper === void 0 || (_engineCtx$appHelper$ = _engineCtx$appHelper.getData) === null || _engineCtx$appHelper$ === void 0 ? void 0 : _engineCtx$appHelper$.call(_engineCtx$appHelper, "shop_id"),
			data: {
				scope: SCOPE,
				section_code: [
					"reservation",
					"fulfillment",
					"menu"
				],
				target: `${BIZ}+${CHANNEL}`
			}
		}, {
			prefix: false,
			headers: { "Tenant-Id": engineCtx === null || engineCtx === void 0 || (_engineCtx$appHelper2 = engineCtx.appHelper) === null || _engineCtx$appHelper2 === void 0 || (_engineCtx$appHelper2 = _engineCtx$appHelper2.utils) === null || _engineCtx$appHelper2 === void 0 || (_engineCtx$appHelper3 = _engineCtx$appHelper2.getData) === null || _engineCtx$appHelper3 === void 0 ? void 0 : _engineCtx$appHelper3.call(_engineCtx$appHelper2, "tenant_id") }
		});
		if ((res === null || res === void 0 ? void 0 : res.code) !== 200) return emptyResult;
		const picoCodingOpenData = res.data;
		const reservationAssociatedMenuListIds = require_associatedMenuListIdsFromOpenData.mapAssociatedMenusToMenuListIds(picoCodingOpenData);
		const linkIds = require_reservationRuleProducts.collectLinkProductIdsFromReservationRules(picoCodingOpenData === null || picoCodingOpenData === void 0 ? void 0 : picoCodingOpenData["fulfillment.enabled_resource_rules"]);
		if (linkIds.length === 0) return {
			picoCodingOpenData,
			reservationRuleProduct: null,
			reservationAssociatedMenuListIds
		};
		const now = (0, dayjs.default)();
		return {
			picoCodingOpenData,
			reservationRuleProduct: require_reservationRuleProducts.pickFirstDurationProduct(yield require_productQuery.queryProductsByIds({
				product_ids: linkIds,
				schedule_date: now.format("YYYY-MM-DD"),
				schedule_datetime: now.format("YYYY-MM-DD HH:mm:ss")
			})),
			reservationAssociatedMenuListIds
		};
	});
	return _loadPicoCodingOpenDataSnapshot.apply(this, arguments);
}
/**
* 拉取 Pico 开放配置（预约/履约/菜单）及规则关联的首个时长商品。
*/
function usePicoCodingOpenData(engineCtx, BIZ) {
	const [picoCodingOpenData, setPicoCodingOpenData] = (0, react.useState)(null);
	const [reservationRuleProduct, setReservationRuleProduct] = (0, react.useState)(null);
	const reservationAssociatedMenuListIds = (0, react.useMemo)(() => require_associatedMenuListIdsFromOpenData.mapAssociatedMenusToMenuListIds(picoCodingOpenData), [picoCodingOpenData]);
	(0, react.useEffect)(() => {
		let cancelled = false;
		require_asyncToGenerator._asyncToGenerator(function* () {
			const snapshot = yield loadPicoCodingOpenDataSnapshot(engineCtx, BIZ);
			if (cancelled) return;
			setPicoCodingOpenData(snapshot.picoCodingOpenData);
			setReservationRuleProduct(snapshot.reservationRuleProduct);
		})();
		return () => {
			cancelled = true;
		};
	}, []);
	return {
		picoCodingOpenData,
		reservationRuleProduct,
		reservationAssociatedMenuListIds
	};
}
//#endregion
exports.usePicoCodingOpenData = usePicoCodingOpenData;
