import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import dayjs from "dayjs";
//#region src/plus/pisellReservation/utils/reservationTicketBookingHostParams.ts
/**
* 组装 `createModeConfig` 中与商品来源、展示、业务码相关的字段（不含 `initialModalState`）。
*/
function buildReservationTicketBookingCreateModeConfigBase(input) {
	var _associated_menu_list;
	const { reservationBiz, associated_menu_list_ids } = input;
	const menuIds = (_associated_menu_list = associated_menu_list_ids === null || associated_menu_list_ids === void 0 ? void 0 : associated_menu_list_ids.filter((id) => typeof id === "number" && Number.isFinite(id) && id > 0)) !== null && _associated_menu_list !== void 0 ? _associated_menu_list : [];
	return _objectSpread2(_objectSpread2({
		bookingConfigParams: {
			item_type: "appointment_booking",
			sub_type: "food",
			source_type: "pos"
		},
		productDisplayConfig: "sku-list-a5",
		businessCode: reservationBiz
	}, menuIds.length > 0 ? { associated_menu_list_ids: menuIds } : {}), reservationBiz === "dine_in" ? { order_type: "table-order" } : {});
}
/**
* 从桌位行组装 booking.resources 所需的资源映射。
*/
function buildResourcePayloadFromReservationRow(row) {
	var _row$capacityLabel;
	const relationIdNum = Number(row.id);
	const relationId = Number.isFinite(relationIdNum) ? relationIdNum : 0;
	const formIdNum = Number(row.form_id);
	const formId = Number.isFinite(formIdNum) ? formIdNum : 0;
	const cap = Number((_row$capacityLabel = row.capacityLabel) !== null && _row$capacityLabel !== void 0 ? _row$capacityLabel : 0);
	const capacity = Number.isFinite(cap) ? cap : 0;
	return { [String(formId)]: [{
		id: 0,
		shop_id: 0,
		schedule_event_id: 0,
		category: "basic",
		form_id: formId,
		parent_relation_id: 0,
		relation_type: "form",
		relation_id: relationId,
		capacity,
		like_status: "common",
		metadata: null,
		created_at: null,
		updated_at: null,
		deleted_at: null
	}] };
}
function buildInitialExtendCapacityFromRuleProduct(product) {
	const capacitys = product === null || product === void 0 ? void 0 : product.capacity;
	if ((capacitys === null || capacitys === void 0 ? void 0 : capacitys.type) !== "custom" || !Array.isArray(capacitys.custom)) return [{
		id: 0,
		value: 1,
		name: ""
	}];
	return capacitys.custom.map((d) => ({
		id: d.id,
		value: d.min || 1,
		name: d.name
	}));
}
function buildServiceItemFromReservationRuleProduct(product, resource) {
	var _product$duration, _ref, _product$title, _ref2, _product$cover, _product$id;
	const duration = Number(product === null || product === void 0 || (_product$duration = product.duration) === null || _product$duration === void 0 ? void 0 : _product$duration.value) || 0;
	const price = Number(product === null || product === void 0 ? void 0 : product.price) || 0;
	const start = dayjs();
	const end = duration > 0 ? start.add(duration, "minute") : start;
	const title = (_ref = (_product$title = product === null || product === void 0 ? void 0 : product.title) !== null && _product$title !== void 0 ? _product$title : product === null || product === void 0 ? void 0 : product.name) !== null && _ref !== void 0 ? _ref : "";
	const cover = (_ref2 = (_product$cover = product === null || product === void 0 ? void 0 : product.cover) !== null && _product$cover !== void 0 ? _product$cover : product === null || product === void 0 ? void 0 : product.main_image) !== null && _ref2 !== void 0 ? _ref2 : "";
	return _objectSpread2(_objectSpread2({}, product), {}, {
		_id: `pre_${(_product$id = product === null || product === void 0 ? void 0 : product.id) !== null && _product$id !== void 0 ? _product$id : "unknown"}_${Date.now()}`,
		id: product === null || product === void 0 ? void 0 : product.id,
		product_id: product === null || product === void 0 ? void 0 : product.id,
		title,
		quantity: 1,
		price,
		cover,
		duration,
		_extend: {
			capacity: buildInitialExtendCapacityFromRuleProduct(product),
			duration,
			price,
			total: price,
			origin_total: price,
			quantity: 1,
			product_name: title,
			startDate: start,
			start_date: start,
			endDate: end,
			end_date: end,
			resource,
			start_time: start.format("HH:mm")
		}
	});
}
function buildReservationTicketBookingCreateModeConfigForRow(input) {
	const { reservationBiz, associated_menu_list_ids, reservationRuleProduct, row } = input;
	const biz = reservationBiz != null && String(reservationBiz).trim() !== "" ? String(reservationBiz).trim() : "";
	if (!biz) return null;
	const createModeConfig = buildReservationTicketBookingCreateModeConfigBase({
		reservationBiz: biz,
		associated_menu_list_ids
	});
	if (biz !== "dine_in") return createModeConfig;
	if (!reservationRuleProduct || !row) return null;
	createModeConfig.initialModalState = {
		service: {
			value: [buildServiceItemFromReservationRuleProduct(reservationRuleProduct, buildResourcePayloadFromReservationRow(row))],
			edit: true
		},
		appointment_status: "started"
	};
	return createModeConfig;
}
//#endregion
export { buildReservationTicketBookingCreateModeConfigBase, buildReservationTicketBookingCreateModeConfigForRow };
