import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { getPicoCodingOpenData } from "../serve/formResourcePage.js";
import { mapAssociatedMenusToMenuListIds } from "../data/associatedMenuListIdsFromOpenData.js";
import { collectLinkProductIdsFromReservationRules, pickFirstDurationProduct } from "../data/reservationRuleProducts.js";
import { queryProductsByIds } from "../serve/productQuery.js";
import dayjs from "dayjs";
import { useEffect, useMemo, useState } from "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 = _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 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 = mapAssociatedMenusToMenuListIds(picoCodingOpenData);
		const linkIds = collectLinkProductIdsFromReservationRules(picoCodingOpenData === null || picoCodingOpenData === void 0 ? void 0 : picoCodingOpenData["fulfillment.enabled_resource_rules"]);
		if (linkIds.length === 0) return {
			picoCodingOpenData,
			reservationRuleProduct: null,
			reservationAssociatedMenuListIds
		};
		const now = dayjs();
		return {
			picoCodingOpenData,
			reservationRuleProduct: pickFirstDurationProduct(yield 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] = useState(null);
	const [reservationRuleProduct, setReservationRuleProduct] = useState(null);
	const reservationAssociatedMenuListIds = useMemo(() => mapAssociatedMenusToMenuListIds(picoCodingOpenData), [picoCodingOpenData]);
	useEffect(() => {
		let cancelled = false;
		_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
export { usePicoCodingOpenData };
