const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_selectors = require("../selectors.js");
const require_identity = require("./identity.js");
//#region src/plus/salesSdk/utils/enrich.ts
function normalizeBundleForDisplay(bundle) {
	var _bundle$metadata, _ref, _ref2, _ref3, _ref4, _bundle$bundle_title, _ref5, _ref6, _ref7, _ref8, _bundle$bundle_sum_pr, _bundle$bundle_paymen, _bundle$bundle_sellin, _bundle$custom_price;
	const origin = (bundle === null || bundle === void 0 || (_bundle$metadata = bundle.metadata) === null || _bundle$metadata === void 0 ? void 0 : _bundle$metadata.origin) || (bundle === null || bundle === void 0 ? void 0 : bundle.origin) || (bundle === null || bundle === void 0 ? void 0 : bundle.product);
	const title = (_ref = (_ref2 = (_ref3 = (_ref4 = (_bundle$bundle_title = bundle === null || bundle === void 0 ? void 0 : bundle.bundle_title) !== null && _bundle$bundle_title !== void 0 ? _bundle$bundle_title : bundle === null || bundle === void 0 ? void 0 : bundle.title) !== null && _ref4 !== void 0 ? _ref4 : bundle === null || bundle === void 0 ? void 0 : bundle.name) !== null && _ref3 !== void 0 ? _ref3 : bundle === null || bundle === void 0 ? void 0 : bundle.product_title) !== null && _ref2 !== void 0 ? _ref2 : origin === null || origin === void 0 ? void 0 : origin.title) !== null && _ref !== void 0 ? _ref : origin === null || origin === void 0 ? void 0 : origin.name;
	const price = (_ref5 = (_ref6 = (_ref7 = (_ref8 = (_bundle$bundle_sum_pr = bundle === null || bundle === void 0 ? void 0 : bundle.bundle_sum_price) !== null && _bundle$bundle_sum_pr !== void 0 ? _bundle$bundle_sum_pr : bundle === null || bundle === void 0 ? void 0 : bundle.bundle_payment_price) !== null && _ref8 !== void 0 ? _ref8 : bundle === null || bundle === void 0 ? void 0 : bundle.custom_price) !== null && _ref7 !== void 0 ? _ref7 : bundle === null || bundle === void 0 ? void 0 : bundle.price) !== null && _ref6 !== void 0 ? _ref6 : bundle === null || bundle === void 0 ? void 0 : bundle.selling_price) !== null && _ref5 !== void 0 ? _ref5 : origin === null || origin === void 0 ? void 0 : origin.price;
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, bundle), {}, {
		bundle_title: title !== null && title !== void 0 ? title : bundle === null || bundle === void 0 ? void 0 : bundle.bundle_title,
		bundle_sum_price: price !== null && price !== void 0 ? price : bundle === null || bundle === void 0 ? void 0 : bundle.bundle_sum_price,
		bundle_payment_price: (_bundle$bundle_paymen = bundle === null || bundle === void 0 ? void 0 : bundle.bundle_payment_price) !== null && _bundle$bundle_paymen !== void 0 ? _bundle$bundle_paymen : price,
		bundle_selling_price: (_bundle$bundle_sellin = bundle === null || bundle === void 0 ? void 0 : bundle.bundle_selling_price) !== null && _bundle$bundle_sellin !== void 0 ? _bundle$bundle_sellin : price,
		custom_price: (_bundle$custom_price = bundle === null || bundle === void 0 ? void 0 : bundle.custom_price) !== null && _bundle$custom_price !== void 0 ? _bundle$custom_price : price
	});
}
/**
* enrichCartItem 多源合并（仅活在 React 内存，不污染 OS tempOrder.products 提交契约）。
*
* 数据来源按字段优先级：
*  - title / cover / variant_title / option / bundle 标题：bookings*.detail（编辑态权威）
*      → productsCatalog 兜底（新建态从商品列表点加购，编辑态做兜底）
*  - duration / product_resource / capacity：bookings*.product（编辑态商品快照）
*      → productsCatalog 兜底
*  - start_at / end_at / resource_id：bookings 主资源（仅编辑态）
*  - note：detail.note → line.note（行级备注）
*
* 入参严格遵守 .cursor/rules/data-field-semantics.mdc：bookings 通过 product_uid 反查
* （不发明 booking_id ↔ product 之间的等价关系），catalog 通过 product_id 字符串比对。
*/
function enrichCartItem(line, ctx) {
	var _line$metadata, _ctx$bookingDetailMap, _ctx$productsByUid, _ref9, _line$metadata2, _catalog$variants, _ref10, _ref11, _ref12, _ref13, _detail$product_title, _ref14, _ref15, _detail$product_cover, _ref16, _ref17, _detail$variant_title, _ref18, _detail$note, _ref19, _productSummary$durat, _ref20, _productSummary$produ, _productSummary$capac, _matched$relation_id;
	const productUid = (_line$metadata = line.metadata) === null || _line$metadata === void 0 ? void 0 : _line$metadata.unique_identification_number;
	const matched = productUid ? (_ctx$bookingDetailMap = ctx.bookingDetailMap) === null || _ctx$bookingDetailMap === void 0 || (_ctx$bookingDetailMap = _ctx$bookingDetailMap[String(productUid)]) === null || _ctx$bookingDetailMap === void 0 ? void 0 : _ctx$bookingDetailMap[0] : void 0;
	const detail = matched === null || matched === void 0 ? void 0 : matched.detail;
	const productSummary = matched === null || matched === void 0 ? void 0 : matched.product;
	const catalog = ctx.productsCatalog.find((p) => String(p.id) === String(line.product_id));
	const runtimeOrigin = productUid ? (_ctx$productsByUid = ctx.productsByUid) === null || _ctx$productsByUid === void 0 || (_ctx$productsByUid = _ctx$productsByUid[String(productUid)]) === null || _ctx$productsByUid === void 0 ? void 0 : _ctx$productsByUid.origin : void 0;
	const lineOrigin = (_ref9 = (_line$metadata2 = line.metadata) === null || _line$metadata2 === void 0 ? void 0 : _line$metadata2.origin) !== null && _ref9 !== void 0 ? _ref9 : runtimeOrigin;
	const lineExtend = line._extend || (lineOrigin === null || lineOrigin === void 0 ? void 0 : lineOrigin._extend) || {};
	const variant = catalog === null || catalog === void 0 || (_catalog$variants = catalog.variants) === null || _catalog$variants === void 0 ? void 0 : _catalog$variants.find((v) => (v === null || v === void 0 ? void 0 : v.id) === line.product_variant_id);
	const detailBundles = Array.isArray(detail === null || detail === void 0 ? void 0 : detail.product_bundle) ? detail.product_bundle : [];
	const detailOptions = Array.isArray(detail === null || detail === void 0 ? void 0 : detail.product_option_item) ? detail.product_option_item : [];
	const catalogBundles = Array.isArray(catalog === null || catalog === void 0 ? void 0 : catalog.bundles) ? catalog.bundles : [];
	const catalogOptions = Array.isArray(catalog === null || catalog === void 0 ? void 0 : catalog.options) ? catalog.options : [];
	const bundleTitles = (line.product_bundle || []).map((b) => {
		const matchedDetail = detailBundles.find((db) => (db === null || db === void 0 ? void 0 : db.bundle_id) === (b === null || b === void 0 ? void 0 : b.bundle_id));
		if (matchedDetail === null || matchedDetail === void 0 ? void 0 : matchedDetail.title) return String(matchedDetail.title);
		const matchedCatalog = catalogBundles.find((cb) => (cb === null || cb === void 0 ? void 0 : cb.id) === (b === null || b === void 0 ? void 0 : b.bundle_id));
		if (matchedCatalog === null || matchedCatalog === void 0 ? void 0 : matchedCatalog.title) return String(matchedCatalog.title);
		return "";
	});
	const optionTitles = (line.product_option_item || []).map((o) => {
		const matchedDetail = detailOptions.find((d) => (d === null || d === void 0 ? void 0 : d.product_option_item_id) === (o === null || o === void 0 ? void 0 : o.product_option_item_id));
		if (matchedDetail === null || matchedDetail === void 0 ? void 0 : matchedDetail.title) return String(matchedDetail.title);
		const matchedCatalog = catalogOptions.find((co) => (co === null || co === void 0 ? void 0 : co.id) === (o === null || o === void 0 ? void 0 : o.product_option_item_id));
		if (matchedCatalog === null || matchedCatalog === void 0 ? void 0 : matchedCatalog.title) return String(matchedCatalog.title);
		return "";
	});
	const startAt = matched && matched.start_date && matched.start_time ? `${matched.start_date} ${matched.start_time}` : null;
	const endAt = matched && matched.end_date && matched.end_time ? `${matched.end_date} ${matched.end_time}` : null;
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, line), {}, {
		product_bundle: (line.product_bundle || []).map(normalizeBundleForDisplay),
		_identity: require_identity.pickIdentity(line),
		_extend: { booking: matched !== null && matched !== void 0 ? matched : null },
		title: (_ref10 = (_ref11 = (_ref12 = (_ref13 = (_detail$product_title = detail === null || detail === void 0 ? void 0 : detail.product_title) !== null && _detail$product_title !== void 0 ? _detail$product_title : catalog === null || catalog === void 0 ? void 0 : catalog.title) !== null && _ref13 !== void 0 ? _ref13 : lineOrigin === null || lineOrigin === void 0 ? void 0 : lineOrigin.title) !== null && _ref12 !== void 0 ? _ref12 : lineOrigin === null || lineOrigin === void 0 ? void 0 : lineOrigin.name) !== null && _ref11 !== void 0 ? _ref11 : lineExtend === null || lineExtend === void 0 ? void 0 : lineExtend.product_name) !== null && _ref10 !== void 0 ? _ref10 : "",
		cover: (_ref14 = (_ref15 = (_detail$product_cover = detail === null || detail === void 0 ? void 0 : detail.product_cover) !== null && _detail$product_cover !== void 0 ? _detail$product_cover : catalog === null || catalog === void 0 ? void 0 : catalog.cover) !== null && _ref15 !== void 0 ? _ref15 : lineOrigin === null || lineOrigin === void 0 ? void 0 : lineOrigin.cover) !== null && _ref14 !== void 0 ? _ref14 : "",
		variant_title: (_ref16 = (_ref17 = (_detail$variant_title = detail === null || detail === void 0 ? void 0 : detail.variant_title) !== null && _detail$variant_title !== void 0 ? _detail$variant_title : variant === null || variant === void 0 ? void 0 : variant.title) !== null && _ref17 !== void 0 ? _ref17 : lineOrigin === null || lineOrigin === void 0 ? void 0 : lineOrigin.variant_title) !== null && _ref16 !== void 0 ? _ref16 : "",
		bundle_titles: bundleTitles,
		option_titles: optionTitles,
		note: (_ref18 = (_detail$note = detail === null || detail === void 0 ? void 0 : detail.note) !== null && _detail$note !== void 0 ? _detail$note : line.note) !== null && _ref18 !== void 0 ? _ref18 : "",
		duration: (_ref19 = (_productSummary$durat = productSummary === null || productSummary === void 0 ? void 0 : productSummary.duration) !== null && _productSummary$durat !== void 0 ? _productSummary$durat : catalog === null || catalog === void 0 ? void 0 : catalog.duration) !== null && _ref19 !== void 0 ? _ref19 : lineOrigin === null || lineOrigin === void 0 ? void 0 : lineOrigin.duration,
		product_resource: (_ref20 = (_productSummary$produ = productSummary === null || productSummary === void 0 ? void 0 : productSummary.product_resource) !== null && _productSummary$produ !== void 0 ? _productSummary$produ : catalog === null || catalog === void 0 ? void 0 : catalog.product_resource) !== null && _ref20 !== void 0 ? _ref20 : lineOrigin === null || lineOrigin === void 0 ? void 0 : lineOrigin.product_resource,
		capacity: (_productSummary$capac = productSummary === null || productSummary === void 0 ? void 0 : productSummary.capacity) !== null && _productSummary$capac !== void 0 ? _productSummary$capac : lineExtend === null || lineExtend === void 0 ? void 0 : lineExtend.capacity,
		start_at: startAt,
		end_at: endAt,
		resource_id: (_matched$relation_id = matched === null || matched === void 0 ? void 0 : matched.relation_id) !== null && _matched$relation_id !== void 0 ? _matched$relation_id : null
	});
}
/**
* 批量 enrich，保留行顺序。
*/
function enrichCartItems(lines, ctx) {
	if (!Array.isArray(lines) || lines.length === 0) return [];
	return lines.map((line) => enrichCartItem(line, ctx));
}
/**
* 客户视图：selected 优先（OS CustomerModule 给出的最完整客户对象），
* 缺失时回落到 tempOrder.customer_*（编辑态服务端 hydrate 后写入 tempOrder 顶层）。
*
* 不复用 ticketBooking 老 _formatBookingDetail 中的兜底链
* （`customer_id || customer?.id || customer?.customer_id || 1`），避免盲推断 walk-in 语义。
*/
function enrichCustomer(selected, tempOrder) {
	if (selected && selected.id !== void 0 && selected.id !== null && selected.id !== "" && selected.id !== 0) {
		var _selected$name;
		return {
			id: selected.id,
			name: (_selected$name = selected.name) !== null && _selected$name !== void 0 ? _selected$name : "",
			phone: selected.phone,
			email: selected.email,
			country_calling_code: selected.country_calling_code,
			cover: selected.cover,
			isWalkIn: require_selectors.isWalkInCustomer(selected)
		};
	}
	if (tempOrder && (tempOrder.customer_id != null || tempOrder.customer_name)) {
		var _tempOrder$customer_i, _tempOrder$customer_n;
		const id = (_tempOrder$customer_i = tempOrder.customer_id) !== null && _tempOrder$customer_i !== void 0 ? _tempOrder$customer_i : "";
		return {
			id,
			name: (_tempOrder$customer_n = tempOrder.customer_name) !== null && _tempOrder$customer_n !== void 0 ? _tempOrder$customer_n : "",
			phone: tempOrder.phone,
			email: tempOrder.email,
			country_calling_code: tempOrder.country_calling_code,
			cover: void 0,
			isWalkIn: require_selectors.isWalkInCustomer({ id })
		};
	}
	return null;
}
//#endregion
exports.enrichCartItem = enrichCartItem;
exports.enrichCartItems = enrichCartItems;
exports.enrichCustomer = enrichCustomer;
