import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
//#region src/components/scanOrder/utils/toShoppingCartProducts.ts
function toNumber(value, fallback = 0) {
	const parsedValue = Number(value);
	if (Number.isNaN(parsedValue)) return fallback;
	return parsedValue;
}
function toSafeNum(value) {
	const parsedValue = Math.floor(Number(value));
	if (Number.isNaN(parsedValue) || parsedValue < 1) return 1;
	return parsedValue;
}
function getDisplayName(product) {
	var _origin$resources, _product$metadata, _product$product_id;
	const origin = (product === null || product === void 0 ? void 0 : product._origin) || {};
	const callbackData = (origin === null || origin === void 0 ? void 0 : origin.callbackData) || {};
	const callbackOrigin = (callbackData === null || callbackData === void 0 ? void 0 : callbackData.origin) || {};
	const sourceItem = (origin === null || origin === void 0 ? void 0 : origin.sourceItem) || {};
	return (callbackData === null || callbackData === void 0 ? void 0 : callbackData.name) || (callbackData === null || callbackData === void 0 ? void 0 : callbackData.title) || (callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.name) || (callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.title) || (sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.name) || (sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.title) || (origin === null || origin === void 0 ? void 0 : origin.name) || (origin === null || origin === void 0 ? void 0 : origin.title) || (origin === null || origin === void 0 || (_origin$resources = origin.resources) === null || _origin$resources === void 0 || (_origin$resources = _origin$resources[0]) === null || _origin$resources === void 0 ? void 0 : _origin$resources.main_field) || (product === null || product === void 0 || (_product$metadata = product.metadata) === null || _product$metadata === void 0 ? void 0 : _product$metadata.resource_name) || `Product #${(_product$product_id = product.product_id) !== null && _product$product_id !== void 0 ? _product$product_id : "unknown"}`;
}
function getDisplayImage(product) {
	const origin = (product === null || product === void 0 ? void 0 : product._origin) || {};
	const callbackData = (origin === null || origin === void 0 ? void 0 : origin.callbackData) || {};
	const callbackOrigin = (callbackData === null || callbackData === void 0 ? void 0 : callbackData.origin) || {};
	const sourceItem = (origin === null || origin === void 0 ? void 0 : origin.sourceItem) || {};
	return (callbackData === null || callbackData === void 0 ? void 0 : callbackData.image) || (callbackData === null || callbackData === void 0 ? void 0 : callbackData.cover) || (callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.image) || (callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.cover) || (sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.image) || (sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.cover) || (origin === null || origin === void 0 ? void 0 : origin.image) || (origin === null || origin === void 0 ? void 0 : origin.cover) || "";
}
function getUnitPrice(product) {
	var _product$_origin, _product$_origin2;
	const callbackData = (product === null || product === void 0 || (_product$_origin = product._origin) === null || _product$_origin === void 0 ? void 0 : _product$_origin.callbackData) || {};
	const callbackOrigin = (callbackData === null || callbackData === void 0 ? void 0 : callbackData.origin) || {};
	const sourceItem = (product === null || product === void 0 || (_product$_origin2 = product._origin) === null || _product$_origin2 === void 0 ? void 0 : _product$_origin2.sourceItem) || {};
	return toNumber(product.selling_price, NaN) || toNumber(product.original_price, NaN) || toNumber(callbackData === null || callbackData === void 0 ? void 0 : callbackData.price, NaN) || toNumber(callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.price, NaN) || toNumber(sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.price, 0);
}
function normalizeOptions(product) {
	var _product$_origin3;
	const callbackData = (product === null || product === void 0 || (_product$_origin3 = product._origin) === null || _product$_origin3 === void 0 ? void 0 : _product$_origin3.callbackData) || {};
	return (Array.isArray(callbackData === null || callbackData === void 0 ? void 0 : callbackData.option) ? callbackData.option : (product === null || product === void 0 ? void 0 : product.product_option_item) || []).map((optionItem) => {
		var _optionItem$product_o;
		const optionPrice = toNumber(optionItem === null || optionItem === void 0 ? void 0 : optionItem.price, 0);
		const optionNum = toSafeNum(optionItem === null || optionItem === void 0 ? void 0 : optionItem.num);
		return {
			id: toNumber((_optionItem$product_o = optionItem === null || optionItem === void 0 ? void 0 : optionItem.product_option_item_id) !== null && _optionItem$product_o !== void 0 ? _optionItem$product_o : optionItem === null || optionItem === void 0 ? void 0 : optionItem.option_group_item_id),
			name: (optionItem === null || optionItem === void 0 ? void 0 : optionItem.name) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.title) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.label) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.option_group_item_name) || "",
			num: optionNum,
			price: optionPrice,
			total: optionPrice * optionNum,
			origin_total: optionPrice * optionNum
		};
	});
}
/** 与 getDisplayName 一致：优先 metadata 上的完整商品快照 */
function getProductOriginRich(product) {
	var _product$metadata2, _product$_origin4;
	return (product === null || product === void 0 || (_product$metadata2 = product.metadata) === null || _product$metadata2 === void 0 ? void 0 : _product$metadata2.origin) || (product === null || product === void 0 || (_product$_origin4 = product._origin) === null || _product$_origin4 === void 0 || (_product$_origin4 = _product$_origin4.callbackData) === null || _product$_origin4 === void 0 ? void 0 : _product$_origin4.origin) || {};
}
function findVariantGroupById(groups, groupId) {
	return groups.find((g) => Number(g === null || g === void 0 ? void 0 : g.id) === Number(groupId));
}
function findVariantItemDisplayName(variantGroup, variantItemId) {
	var _ref, _found$name;
	const found = (Array.isArray(variantGroup === null || variantGroup === void 0 ? void 0 : variantGroup.variant_item) ? variantGroup.variant_item : []).find((it) => Number(it === null || it === void 0 ? void 0 : it.id) === Number(variantItemId));
	const raw = (_ref = (_found$name = found === null || found === void 0 ? void 0 : found.name) !== null && _found$name !== void 0 ? _found$name : typeof (found === null || found === void 0 ? void 0 : found.title) === "string" ? found.title : found === null || found === void 0 ? void 0 : found.title) !== null && _ref !== void 0 ? _ref : "";
	return String(raw !== null && raw !== void 0 ? raw : "").trim();
}
/**
* 按 sku_ids（如 "7006:19533" 或 "g1:i1_g2:i2"）与 variant_group 拼「组名: 选项名」，与选品器 transformSelectorValueToValue 风格一致（", " 连接）。
*/
function buildVariantStringFromSkuIds(origin, skuIdsRaw) {
	const groups = Array.isArray(origin === null || origin === void 0 ? void 0 : origin.variant_group) ? origin.variant_group : [];
	const parts = [];
	const segments = String(skuIdsRaw || "").split("_").map((s) => s.trim()).filter(Boolean);
	for (const seg of segments) {
		var _vg$name;
		const colonIdx = seg.indexOf(":");
		if (colonIdx <= 0) continue;
		const groupId = Number(seg.slice(0, colonIdx));
		const itemId = Number(seg.slice(colonIdx + 1));
		if (Number.isNaN(groupId) || Number.isNaN(itemId)) continue;
		const vg = findVariantGroupById(groups, groupId);
		if (!vg) continue;
		const groupName = String((_vg$name = vg === null || vg === void 0 ? void 0 : vg.name) !== null && _vg$name !== void 0 ? _vg$name : "").trim();
		const itemName = findVariantItemDisplayName(vg, itemId);
		if (groupName && itemName) parts.push(`${groupName}: ${itemName}`);
		else if (itemName) parts.push(itemName);
		else if (groupName) parts.push(groupName);
	}
	return parts.join(", ");
}
/** sku_ids 解析失败时用 variant 的 name/title 与 variant_group.position_id 对齐 */
function fallbackVariantLabelFromTitles(origin, variantRow) {
	const groups = Array.isArray(origin === null || origin === void 0 ? void 0 : origin.variant_group) ? origin.variant_group : [];
	const nameObj = (variantRow === null || variantRow === void 0 ? void 0 : variantRow.name) || (variantRow === null || variantRow === void 0 ? void 0 : variantRow.title);
	if (!nameObj || typeof nameObj !== "object") return "";
	const parts = [];
	for (const g of groups) {
		var _String, _g$name;
		const pid = g === null || g === void 0 ? void 0 : g.position_id;
		if (pid == null) continue;
		const val = (_String = nameObj[String(pid)]) !== null && _String !== void 0 ? _String : nameObj[pid];
		if (val == null || val === "") continue;
		const groupName = String((_g$name = g === null || g === void 0 ? void 0 : g.name) !== null && _g$name !== void 0 ? _g$name : "").trim();
		const itemLabel = String(val).trim();
		if (groupName && itemLabel) parts.push(`${groupName}: ${itemLabel}`);
		else if (itemLabel) parts.push(itemLabel);
	}
	if (parts.length) return parts.join(", ");
	return Object.values(nameObj).filter((v) => v != null && String(v).trim() !== "").map((v) => String(v).trim()).join(" / ");
}
function getVariantProductOptionString(product) {
	const variantId = product === null || product === void 0 ? void 0 : product.product_variant_id;
	if (variantId == null || variantId === "" || Number(variantId) <= 0) return "";
	const origin = getProductOriginRich(product);
	const matched = (Array.isArray(origin === null || origin === void 0 ? void 0 : origin.variant) ? origin.variant : []).find((v) => Number(v === null || v === void 0 ? void 0 : v.id) === Number(variantId));
	if (!matched) return "";
	const skuIds = matched === null || matched === void 0 ? void 0 : matched.sku_ids;
	if (skuIds != null && String(skuIds).trim() !== "") {
		const fromSku = buildVariantStringFromSkuIds(origin, String(skuIds));
		if (fromSku) return fromSku;
	}
	return fallbackVariantLabelFromTitles(origin, matched);
}
function normalizeBundles(product) {
	var _product$_origin5;
	const callbackData = (product === null || product === void 0 || (_product$_origin5 = product._origin) === null || _product$_origin5 === void 0 ? void 0 : _product$_origin5.callbackData) || {};
	return (Array.isArray(callbackData === null || callbackData === void 0 ? void 0 : callbackData.bundle) ? callbackData.bundle : (product === null || product === void 0 ? void 0 : product.product_bundle) || []).map((bundleItem, bundleIndex) => {
		var _ref2, _bundleItem$price, _bundleItem$id;
		const bundlePrice = toNumber((_ref2 = (_bundleItem$price = bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.price) !== null && _bundleItem$price !== void 0 ? _bundleItem$price : bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.product_price) !== null && _ref2 !== void 0 ? _ref2 : bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.custom_price, 0);
		const bundleNum = toSafeNum(bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.num);
		const bundleOptions = Array.isArray(bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.option) ? bundleItem.option : [];
		return {
			id: toNumber((_bundleItem$id = bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.id) !== null && _bundleItem$id !== void 0 ? _bundleItem$id : bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.bundle_product_id, bundleIndex),
			name: (bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.name) || (bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.title) || "",
			num: bundleNum,
			price: bundlePrice,
			total: bundlePrice * bundleNum,
			origin_total: bundlePrice * bundleNum,
			options: bundleOptions.map((optionItem) => {
				var _optionItem$product_o2;
				const optionPrice = toNumber(optionItem === null || optionItem === void 0 ? void 0 : optionItem.price, 0);
				const optionNum = toSafeNum(optionItem === null || optionItem === void 0 ? void 0 : optionItem.num);
				return {
					id: toNumber((_optionItem$product_o2 = optionItem === null || optionItem === void 0 ? void 0 : optionItem.product_option_item_id) !== null && _optionItem$product_o2 !== void 0 ? _optionItem$product_o2 : optionItem === null || optionItem === void 0 ? void 0 : optionItem.option_group_item_id),
					name: (optionItem === null || optionItem === void 0 ? void 0 : optionItem.name) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.title) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.label) || (optionItem === null || optionItem === void 0 ? void 0 : optionItem.option_group_item_name) || "",
					num: optionNum,
					price: optionPrice,
					total: optionPrice * optionNum,
					origin_total: optionPrice * optionNum
				};
			})
		};
	});
}
function toShoppingCartProducts(params) {
	const { orderProducts, amountSymbol = "$" } = params;
	if (!Array.isArray(orderProducts)) return [];
	return orderProducts.map((productItem, index) => {
		var _productItem$_origin, _productItem$product_, _productItem$metadata;
		const productNum = toSafeNum(productItem === null || productItem === void 0 ? void 0 : productItem.num);
		const unitPrice = getUnitPrice(productItem);
		const callbackData = (productItem === null || productItem === void 0 || (_productItem$_origin = productItem._origin) === null || _productItem$_origin === void 0 ? void 0 : _productItem$_origin.callbackData) || {};
		const identityKey = typeof (productItem === null || productItem === void 0 ? void 0 : productItem.identity_key) === "string" && productItem.identity_key.length > 0 ? productItem.identity_key : void 0;
		const rowKey = identityKey !== null && identityKey !== void 0 ? identityKey : `${(_productItem$product_ = productItem.product_id) !== null && _productItem$product_ !== void 0 ? _productItem$product_ : "p"}_${productItem.product_variant_id}_${index}`;
		const productOptionString = (typeof (callbackData === null || callbackData === void 0 ? void 0 : callbackData.product_option_string) === "string" ? callbackData.product_option_string.trim() : "") || getVariantProductOptionString(productItem) || void 0;
		const base = _objectSpread2({
			_id: rowKey,
			rowKey,
			identity_key: identityKey,
			id: productItem.product_id,
			product_id: productItem.product_id,
			product_variant_id: productItem.product_variant_id,
			note: (productItem === null || productItem === void 0 ? void 0 : productItem.note) != null ? String(productItem.note) : "",
			name: getDisplayName(productItem),
			image: getDisplayImage(productItem),
			num: productNum,
			price: unitPrice,
			total: unitPrice,
			origin_total: toNumber(productItem.original_price, unitPrice),
			symbol: amountSymbol,
			options: normalizeOptions(productItem),
			bundle: normalizeBundles(productItem),
			_origin: productItem === null || productItem === void 0 ? void 0 : productItem._origin,
			isNormalProduct: true
		}, productOptionString ? { product_option_string: productOptionString } : {});
		if (Array.isArray(productItem.product_option_item) && productItem.product_option_item.length) base.product_option_item = productItem.product_option_item;
		if (Array.isArray(productItem.product_bundle) && productItem.product_bundle.length) base.product_bundle = productItem.product_bundle;
		if (productItem === null || productItem === void 0 || (_productItem$metadata = productItem.metadata) === null || _productItem$metadata === void 0 ? void 0 : _productItem$metadata.venue_booking) {
			var _origin$resources2;
			const origin = productItem._origin || {};
			const meta = productItem.metadata || {};
			const startDate = origin.start_date;
			const startTime = origin.start_time;
			const endDate = origin.end_date;
			const endTime = origin.end_time;
			base.start_date = startDate && startTime ? `${startDate} ${startTime}` : meta.start_time || "";
			base.end_date = endDate && endTime ? `${endDate} ${endTime}` : meta.end_time || "";
			base.start_time = startTime || "";
			base.end_time = endTime || "";
			base.duration_type = origin.sub_type || "minutes";
			base.resource_id = meta.resource_id;
			base.relation_form_name = meta.resource_name || ((_origin$resources2 = origin.resources) === null || _origin$resources2 === void 0 || (_origin$resources2 = _origin$resources2[0]) === null || _origin$resources2 === void 0 ? void 0 : _origin$resources2.main_field) || "";
			if (Array.isArray(origin.resources)) base.resources = origin.resources;
		}
		return base;
	});
}
//#endregion
export { toShoppingCartProducts };
