import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
//#region src/components/scanOrder/utils/transformProductDetail.ts
function toNumber(value, fallback = 0) {
	const parsedValue = Number(value);
	if (Number.isNaN(parsedValue)) return fallback;
	return parsedValue;
}
function toProductId(value) {
	if (value === null || value === void 0 || value === "") return null;
	const parsedValue = Number(value);
	if (Number.isNaN(parsedValue)) return null;
	return parsedValue;
}
function toSafePositiveInt(value, fallback = 1) {
	const parsedValue = Math.floor(Number(value));
	if (Number.isNaN(parsedValue) || parsedValue < 1) return fallback;
	return parsedValue;
}
function toPriceString(value, fallback = "0.00") {
	const parsedValue = Number(value);
	if (Number.isNaN(parsedValue)) return fallback;
	return parsedValue.toFixed(2);
}
function normalizeOptionItems(optionItems) {
	if (!Array.isArray(optionItems)) return [];
	return optionItems.map((optionItem) => {
		var _optionItem$product_o;
		return {
			product_option_item_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),
			option_group_id: toNumber(optionItem === null || optionItem === void 0 ? void 0 : optionItem.option_group_id),
			num: toSafePositiveInt(optionItem === null || optionItem === void 0 ? void 0 : optionItem.num, 1),
			price: toNumber(optionItem === null || optionItem === void 0 ? void 0 : optionItem.price, 0)
		};
	});
}
function normalizeBundleItems(bundleItems) {
	if (!Array.isArray(bundleItems)) return [];
	return bundleItems.map((bundleItem) => {
		return _objectSpread2(_objectSpread2({}, bundleItem), {}, { option: normalizeOptionItems(bundleItem === null || bundleItem === void 0 ? void 0 : bundleItem.option) });
	});
}
/**
* 在商品 origin 数据中按 variant_id 命中对应的规格行，用于取「规格级」字段（price / base_price / is_charge_tax 等）。
* 主商品字段仅作为无规格或无法命中时的兜底，避免把主商品价误当成规格价写入订单行。
*/
function findVariantById(origin, variantId) {
	if (!origin || !variantId || variantId <= 0) return null;
	return (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) === variantId) || null;
}
function transformProductDetailToOrderProduct(params) {
	var _ref, _ref2, _payload$product_id, _ref3, _payload$product_vari, _payload$_origin, _findVariantById, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9, _payload$price, _ref10, _ref11, _payload$tax_fee, _ref12, _ref13, _payload$is_charge_ta, _payload$quantity, _payload$option, _payload$bundle;
	const { payload, fallbackProductId = null, sourceItem = {} } = params;
	if (!payload || typeof payload !== "object") return null;
	const productId = toProductId((_ref = (_ref2 = (_payload$product_id = payload.product_id) !== null && _payload$product_id !== void 0 ? _payload$product_id : payload.id) !== null && _ref2 !== void 0 ? _ref2 : sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.product_id) !== null && _ref !== void 0 ? _ref : fallbackProductId);
	if (productId === null) return null;
	const productVariantId = toNumber((_ref3 = (_payload$product_vari = payload.product_variant_id) !== null && _payload$product_vari !== void 0 ? _payload$product_vari : payload.variant_id) !== null && _ref3 !== void 0 ? _ref3 : sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.product_variant_id, 0);
	const callbackOrigin = (payload === null || payload === void 0 ? void 0 : payload.origin) || (payload === null || payload === void 0 || (_payload$_origin = payload._origin) === null || _payload$_origin === void 0 ? void 0 : _payload$_origin.sourceItem) || (payload === null || payload === void 0 ? void 0 : payload._origin) || {};
	const matchedVariant = (_findVariantById = findVariantById(callbackOrigin, productVariantId)) !== null && _findVariantById !== void 0 ? _findVariantById : findVariantById(sourceItem, productVariantId);
	const sellingPrice = toPriceString((_ref4 = (_ref5 = (_ref6 = (_ref7 = (_ref8 = (_ref9 = (_payload$price = payload === null || payload === void 0 ? void 0 : payload.price) !== null && _payload$price !== void 0 ? _payload$price : payload === null || payload === void 0 ? void 0 : payload.selling_price) !== null && _ref9 !== void 0 ? _ref9 : matchedVariant === null || matchedVariant === void 0 ? void 0 : matchedVariant.price) !== null && _ref8 !== void 0 ? _ref8 : matchedVariant === null || matchedVariant === void 0 ? void 0 : matchedVariant.base_price) !== null && _ref7 !== void 0 ? _ref7 : callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.price) !== null && _ref6 !== void 0 ? _ref6 : sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.price) !== null && _ref5 !== void 0 ? _ref5 : sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.selling_price) !== null && _ref4 !== void 0 ? _ref4 : sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.base_price, "0.00");
	const originalPrice = sellingPrice;
	const taxFee = toPriceString((_ref10 = (_ref11 = (_payload$tax_fee = payload === null || payload === void 0 ? void 0 : payload.tax_fee) !== null && _payload$tax_fee !== void 0 ? _payload$tax_fee : matchedVariant === null || matchedVariant === void 0 ? void 0 : matchedVariant.tax_fee) !== null && _ref11 !== void 0 ? _ref11 : callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.tax_fee) !== null && _ref10 !== void 0 ? _ref10 : sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.tax_fee, "0.00");
	const isChargeTax = toNumber((_ref12 = (_ref13 = (_payload$is_charge_ta = payload === null || payload === void 0 ? void 0 : payload.is_charge_tax) !== null && _payload$is_charge_ta !== void 0 ? _payload$is_charge_ta : matchedVariant === null || matchedVariant === void 0 ? void 0 : matchedVariant.is_charge_tax) !== null && _ref13 !== void 0 ? _ref13 : callbackOrigin === null || callbackOrigin === void 0 ? void 0 : callbackOrigin.is_charge_tax) !== null && _ref12 !== void 0 ? _ref12 : sourceItem === null || sourceItem === void 0 ? void 0 : sourceItem.is_charge_tax, 0);
	return {
		product_id: productId,
		product_variant_id: productVariantId,
		num: toSafePositiveInt((_payload$quantity = payload.quantity) !== null && _payload$quantity !== void 0 ? _payload$quantity : payload.num, 1),
		product_option_item: normalizeOptionItems((_payload$option = payload.option) !== null && _payload$option !== void 0 ? _payload$option : payload.product_option_item),
		product_bundle: normalizeBundleItems((_payload$bundle = payload.bundle) !== null && _payload$bundle !== void 0 ? _payload$bundle : payload.product_bundle),
		selling_price: sellingPrice,
		original_price: originalPrice,
		tax_fee: taxFee,
		is_charge_tax: isChargeTax,
		metadata: {
			unique: payload.unique,
			session: payload.session,
			form: payload.form,
			service_times: payload.service_times,
			machine_code: payload.machine_code,
			schedule_id: payload.schedule_id,
			duration: payload.duration,
			policy_id: payload.policy_id,
			source_shop_id: payload.source_shop_id,
			origin: payload.origin
		},
		_origin: _objectSpread2(_objectSpread2({}, sourceItem || {}), {}, { callbackData: payload })
	};
}
//#endregion
export { transformProductDetailToOrderProduct };
