//#region src/plus/salesSdk/utils/editCartLine.ts
function pickCartLineUid(item) {
	var _item$_identity$uniqu, _item$_identity, _item$metadata;
	const uid = (_item$_identity$uniqu = (_item$_identity = item._identity) === null || _item$_identity === void 0 ? void 0 : _item$_identity.unique_identification_number) !== null && _item$_identity$uniqu !== void 0 ? _item$_identity$uniqu : (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.unique_identification_number;
	if (uid === void 0 || uid === null || uid === "") return null;
	return String(uid);
}
function pickBookingProductUid(booking) {
	var _booking$product_uid, _booking$metadata;
	const uid = (_booking$product_uid = booking.product_uid) !== null && _booking$product_uid !== void 0 ? _booking$product_uid : (_booking$metadata = booking.metadata) === null || _booking$metadata === void 0 ? void 0 : _booking$metadata.product_uid;
	if (uid === void 0 || uid === null || uid === "") return null;
	return String(uid);
}
function findItemByUid(uid, items) {
	return items.find((line) => pickCartLineUid(line) === uid);
}
function findItemByBookingId(booking, items) {
	const bookingId = booking.booking_id;
	if (bookingId === void 0 || bookingId === null || bookingId === "") return;
	return items.find((line) => {
		var _line$_extend$booking, _line$_extend;
		return String((_line$_extend$booking = (_line$_extend = line._extend) === null || _line$_extend === void 0 || (_line$_extend = _line$_extend.booking) === null || _line$_extend === void 0 ? void 0 : _line$_extend.booking_id) !== null && _line$_extend$booking !== void 0 ? _line$_extend$booking : "") === String(bookingId);
	});
}
/**
* ProductCard LineItem 会把商品行 convert 后再触发 onCard，原始 enriched 行挂在 `_origin`。
*/
function normalizeProductCartTarget(target) {
	if (target === null || target === void 0 ? void 0 : target._identity) return target;
	const origin = target === null || target === void 0 ? void 0 : target._origin;
	if (origin === null || origin === void 0 ? void 0 : origin._identity) return origin;
	return null;
}
/**
* 将 booking 行或商品行统一解析为可编辑的 `SalesSdkCartItemView`。
*
* 解析顺序：
* 1. 已是 enriched 商品行（含 ProductCard `_origin` 还原）
* 2. booking 卡片：`getCart` 挂在 `_extend.product` 上的 enriched 行（items 可能为空）
* 3. 通过 uid / booking_id 在 items 中反查（历史兼容）
*/
function resolveEditCartLineTarget(target, items) {
	var _extend, _target$metadata$uniq, _target$metadata;
	const normalized = normalizeProductCartTarget(target);
	if (normalized) return normalized;
	const bookingProduct = target === null || target === void 0 || (_extend = target._extend) === null || _extend === void 0 ? void 0 : _extend.product;
	if (bookingProduct) {
		var _normalizeProductCart;
		const productLine = (_normalizeProductCart = normalizeProductCartTarget(bookingProduct)) !== null && _normalizeProductCart !== void 0 ? _normalizeProductCart : bookingProduct;
		if (pickCartLineUid(productLine)) return productLine;
	}
	const uidFromCard = (_target$metadata$uniq = target === null || target === void 0 || (_target$metadata = target.metadata) === null || _target$metadata === void 0 ? void 0 : _target$metadata.unique_identification_number) !== null && _target$metadata$uniq !== void 0 ? _target$metadata$uniq : typeof (target === null || target === void 0 ? void 0 : target._id) === "string" ? target._id : null;
	if (uidFromCard) {
		const matched = findItemByUid(String(uidFromCard), items);
		if (matched) return matched;
	}
	const booking = target;
	const productUid = pickBookingProductUid(booking);
	if (productUid) {
		const matched = findItemByUid(productUid, items);
		if (matched) return matched;
	}
	const matchedByBookingId = findItemByBookingId(booking, items);
	if (matchedByBookingId) return matchedByBookingId;
	throw new Error("[SalesSdk] 未找到对应购物车商品行，无法编辑");
}
/** 是否应走资源/时间编辑（openBookingEdit）而非普通商品弹窗。 */
function shouldEditCartLineAsBooking(item) {
	var _item$_extend;
	return ((_item$_extend = item._extend) === null || _item$_extend === void 0 ? void 0 : _item$_extend.booking) != null;
}
//#endregion
exports.resolveEditCartLineTarget = resolveEditCartLineTarget;
exports.shouldEditCartLineAsBooking = shouldEditCartLineAsBooking;
