import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import dayjs from "dayjs";
//#region src/plus/salesSdk/demo/hosts/legacyOpenProductDetail.ts
/** 商品详情弹窗未回调时的兜底超时（避免 addProductWithFlow 永久 pending） */
const OPEN_PRODUCT_DETAIL_TIMEOUT_MS = 12e4;
const formatBundle = (bundle) => {
	if (!Array.isArray(bundle)) return [];
	return bundle.map((item) => _objectSpread2(_objectSpread2({}, item), {}, { bundle_product_id: item._bundle_product_id }));
};
/**
* 对齐 ticketBooking `openProductModal` 预约商品分支：`pisell1.handleOpenProductModal`。
*
* - 入参以 OS `requiresDetail` payload 为准（showConfig / isEject / productData）。
* - `customer_id` / `date`：payload 优先，runtime 兜底，最后默认当天。
* - 用户取消 → resolve(null)；确认 → resolve Info2 callback 三元组供 `cart.confirmDetail` 使用。
*/
function createLegacyOpenProductDetailHost(deps) {
	const { action, getRuntime } = deps;
	return (payload) => {
		var _payload$productData, _payload$date;
		if (!action) return Promise.reject(/* @__PURE__ */ new Error("[SalesSdk Demo] openProductDetail: appHelper.utils.action 不可用"));
		const item = (_payload$productData = payload.productData) !== null && _payload$productData !== void 0 ? _payload$productData : payload.item;
		const runtime = getRuntime();
		const rawDate = (_payload$date = payload.date) !== null && _payload$date !== void 0 ? _payload$date : runtime.date;
		const date = dayjs(rawDate).isValid() ? dayjs(rawDate).format("YYYY-MM-DD") : dayjs().format("YYYY-MM-DD");
		return new Promise((resolve, reject) => {
			var _payload$customerId, _payload$showConfig;
			let settled = false;
			const settle = (fn) => {
				if (settled) return;
				settled = true;
				clearTimeout(timer);
				fn();
			};
			const timer = setTimeout(() => {
				settle(() => reject(/* @__PURE__ */ new Error("[SalesSdk] openProductDetail 超时：页面未响应 pisell1.handleOpenProductModal（请确认低代码页已注册该 action，且场次弹窗能正常打开）")));
			}, OPEN_PRODUCT_DETAIL_TIMEOUT_MS);
			action({
				type: "pisell1.handleOpenProductModal",
				data: {
					id: item === null || item === void 0 ? void 0 : item.id,
					date,
					productDetail: item,
					showConfig: payload.showConfig,
					value: item === null || item === void 0 ? void 0 : item.initValue,
					isOnlySession: payload.isOnlySession,
					is_eject: payload.isEject ? 1 : 0,
					customer_id: (_payload$customerId = payload.customerId) !== null && _payload$customerId !== void 0 ? _payload$customerId : runtime.customerId,
					isOS: (_payload$showConfig = payload.showConfig) === null || _payload$showConfig === void 0 ? void 0 : _payload$showConfig.session
				},
				onCancel: () => settle(() => resolve(null)),
				callback: (e, extension_type, detail) => {
					console.log(e, extension_type, detail, "detail1234");
					settle(() => resolve({
						e: _objectSpread2(_objectSpread2({}, e), {}, { bundle: formatBundle(e.bundle) }),
						extension_type,
						detail
					}));
				}
			});
		});
	};
}
//#endregion
export { createLegacyOpenProductDetailHost };
