import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { isNormalProductByDurationSchedule } from "../../../components/booking/info/service2/utils.js";
import { buildProductLineUpdates } from "./buildProductLineUpdates.js";
import { requestOpenNormalProductDetail } from "../normalProductDetail/normalProductDetailHostController.js";
//#region src/plus/salesSdk/utils/editCartLineProduct.ts
function findOrderLine(item, tempOrder) {
	var _item$_identity$uniqu, _item$_identity, _item$metadata, _tempOrder$products, _tempOrder$products$f;
	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 || !(tempOrder === null || tempOrder === void 0 || (_tempOrder$products = tempOrder.products) === null || _tempOrder$products === void 0 ? void 0 : _tempOrder$products.length)) return null;
	return (_tempOrder$products$f = tempOrder.products.find((line) => {
		var _line$metadata;
		return String((_line$metadata = line.metadata) === null || _line$metadata === void 0 ? void 0 : _line$metadata.unique_identification_number) === String(uid);
	})) !== null && _tempOrder$products$f !== void 0 ? _tempOrder$products$f : null;
}
function findCatalogProduct(productId, catalog) {
	if (productId == null) return void 0;
	return catalog.find((p) => String(p.id) === String(productId));
}
/**
* 已加车普通商品行：反查 cacheItem → SkuDetailModal edit → updateOrderProduct（无 booking）。
*/
function editCartLineProduct(_x) {
	return _editCartLineProduct.apply(this, arguments);
}
function _editCartLineProduct() {
	_editCartLineProduct = _asyncToGenerator(function* (input) {
		var _ref, _bookingTicket$getTem, _bookingTicket$getTem2, _bookingTicket$getPro, _bookingTicket$getPro2;
		const { item, bookingTicket, updateItem } = input;
		const orderLine = findOrderLine(item, (_ref = (_bookingTicket$getTem = (_bookingTicket$getTem2 = bookingTicket.getTempOrder) === null || _bookingTicket$getTem2 === void 0 ? void 0 : _bookingTicket$getTem2.call(bookingTicket)) !== null && _bookingTicket$getTem !== void 0 ? _bookingTicket$getTem : input.tempOrder) !== null && _ref !== void 0 ? _ref : null);
		if (!orderLine) throw new Error("[SalesSdk] 未在 tempOrder.products 找到对应商品行");
		if (typeof bookingTicket.buildNormalProductCacheItemFromOrderLine !== "function") throw new Error("[SalesSdk] bookingTicket.buildNormalProductCacheItemFromOrderLine 不可用，请升级 pisellos");
		const catalog = (_bookingTicket$getPro = (_bookingTicket$getPro2 = bookingTicket.getProductCatalog) === null || _bookingTicket$getPro2 === void 0 || (_bookingTicket$getPro2 = _bookingTicket$getPro2.call(bookingTicket)) === null || _bookingTicket$getPro2 === void 0 ? void 0 : _bookingTicket$getPro2.products) !== null && _bookingTicket$getPro !== void 0 ? _bookingTicket$getPro : [];
		const sourceProduct = findCatalogProduct(orderLine.product_id, catalog);
		if (sourceProduct && !isNormalProductByDurationSchedule(sourceProduct)) throw new Error("[SalesSdk] 该商品不是普通商品，请使用 editCartLineBooking");
		const editedResult = yield requestOpenNormalProductDetail({
			mode: "edit",
			cacheItem: bookingTicket.buildNormalProductCacheItemFromOrderLine({
				product: orderLine,
				sourceProduct: sourceProduct !== null && sourceProduct !== void 0 ? sourceProduct : null
			}),
			skuDetailConfig: sourceProduct === null || sourceProduct === void 0 ? void 0 : sourceProduct.skuDetailConfig
		});
		if (!editedResult || editedResult.mode !== "edit") return { status: "cancelled" };
		const edited = editedResult.cacheItem;
		if (typeof bookingTicket.transformDetailToProductAndBooking !== "function") throw new Error("[SalesSdk] bookingTicket.transformDetailToProductAndBooking 不可用");
		const catalogItem = sourceProduct !== null && sourceProduct !== void 0 ? sourceProduct : { id: orderLine.product_id };
		const { product: productPatch } = bookingTicket.transformDetailToProductAndBooking({
			item: catalogItem,
			cacheItem: edited
		});
		const identity = item._identity;
		return {
			status: "updated",
			products: yield updateItem({
				product_id: identity.product_id,
				product_variant_id: identity.product_variant_id,
				unique_identification_number: identity.unique_identification_number,
				product_option_item: identity.product_option_item,
				product_bundle: identity.product_bundle,
				updates: buildProductLineUpdates(orderLine, productPatch)
			})
		};
	});
	return _editCartLineProduct.apply(this, arguments);
}
//#endregion
export { editCartLineProduct };
