import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { formatDataKey } from "../../../components/ticketBooking/components/addServiceVariant/utils.js";
import { setProductPrice } from "../../../components/booking/info2/service/editService/utils.js";
import { getProductTotalPrice } from "../../../components/booking/utils.js";
import { cloneDeep } from "lodash";
//#region src/plus/salesSdk/normalProductDetail/transformNormalProductConfirm.ts
/** 将 setProductPrice 结果同步到 skuValue / _skuValue（与 ticketBooking 卡片一致） */
function syncSkuValueFromOther(data) {
	var _data$_extend, _ref, _other$product_id, _other$product_varian, _other$product_option;
	const other = data === null || data === void 0 || (_data$_extend = data._extend) === null || _data$_extend === void 0 ? void 0 : _data$_extend.other;
	if (!other || !(data === null || data === void 0 ? void 0 : data._extend)) return data;
	const snapshot = {
		product_id: (_ref = (_other$product_id = other.product_id) !== null && _other$product_id !== void 0 ? _other$product_id : data.product_id) !== null && _ref !== void 0 ? _ref : data.id,
		product_variant_id: (_other$product_varian = other.product_variant_id) !== null && _other$product_varian !== void 0 ? _other$product_varian : 0,
		product_option_string: (_other$product_option = other.product_option_string) !== null && _other$product_option !== void 0 ? _other$product_option : "",
		bundle: cloneDeep(other.bundle || []),
		option: cloneDeep(other.option || [])
	};
	data._extend.skuValue = snapshot;
	data._extend._skuValue = cloneDeep(snapshot);
	return data;
}
/**
* 手动改价时 origin_total 不能回退为折后 total，否则 setProductPrice 比例为 1、子商品不会重算。
*/
function resolveOriginTotalForSetPrice(extend, lineTotal) {
	const hasOverride = (extend === null || extend === void 0 ? void 0 : extend.priceOverride) !== void 0 && (extend === null || extend === void 0 ? void 0 : extend.priceOverride) !== null;
	const origin = Number(extend === null || extend === void 0 ? void 0 : extend.origin_total);
	if (Number.isFinite(origin) && origin > 0) return origin;
	if (hasOverride) return lineTotal;
	return lineTotal;
}
/**
* create 模式：SkuDetailModal 确认结果 → `cart.confirmDetail` 所需的 detailResult。
* 对齐 info2 EditNormalService create 分支，不依赖 miniRedux。
*/
function transformNormalProductCreateConfirm(productId, data) {
	var _data$_extend2, _data$_extend$origin_, _data$_extend3, _data$_extend4, _data$_extend5, _data$_extend6, _data$_extend7;
	const quantity = Number((data === null || data === void 0 || (_data$_extend2 = data._extend) === null || _data$_extend2 === void 0 ? void 0 : _data$_extend2.quantity) || 1);
	let detail = _objectSpread2(_objectSpread2({}, data || {}), {}, {
		id: productId,
		product_id: productId,
		_extend: _objectSpread2(_objectSpread2({}, (data === null || data === void 0 ? void 0 : data._extend) || {}), {}, {
			origin_total: (_data$_extend$origin_ = data === null || data === void 0 || (_data$_extend3 = data._extend) === null || _data$_extend3 === void 0 ? void 0 : _data$_extend3.origin_total) !== null && _data$_extend$origin_ !== void 0 ? _data$_extend$origin_ : data === null || data === void 0 || (_data$_extend4 = data._extend) === null || _data$_extend4 === void 0 ? void 0 : _data$_extend4.total,
			quantity,
			skuValue: cloneDeep((data === null || data === void 0 || (_data$_extend5 = data._extend) === null || _data$_extend5 === void 0 ? void 0 : _data$_extend5.skuValue) || {})
		})
	});
	data._extend.total = getProductTotalPrice(data);
	detail = setProductPrice(detail, {
		newTotal: data === null || data === void 0 || (_data$_extend6 = data._extend) === null || _data$_extend6 === void 0 ? void 0 : _data$_extend6.total,
		origin_total: (_data$_extend7 = data._extend) === null || _data$_extend7 === void 0 ? void 0 : _data$_extend7.origin_total
	});
	let e = _objectSpread2(_objectSpread2({ product_id: productId }, detail._extend.other), {}, { quantity });
	e = formatDataKey(e);
	return {
		e,
		extension_type: void 0,
		detail
	};
}
/**
* edit 模式：SkuDetailModal 确认结果 → 写回 OS 所需的 cacheItem。
* 对齐 info2 EditNormalService edit 分支的价格处理。
*/
function prepareNormalProductEditCacheItem(data) {
	var _extend$other$bundle$, _extend$other, _prepared$_extend;
	const extend = (data === null || data === void 0 ? void 0 : data._extend) || {};
	const lineTotal = getProductTotalPrice(data);
	data._extend = _objectSpread2(_objectSpread2({}, extend), {}, {
		total: lineTotal,
		origin_total: resolveOriginTotalForSetPrice(extend, lineTotal)
	});
	if ((data === null || data === void 0 ? void 0 : data.is_bundle) || ((_extend$other$bundle$ = extend === null || extend === void 0 || (_extend$other = extend.other) === null || _extend$other === void 0 || (_extend$other = _extend$other.bundle) === null || _extend$other === void 0 ? void 0 : _extend$other.length) !== null && _extend$other$bundle$ !== void 0 ? _extend$other$bundle$ : 0) > 0) data._extend.bundle_edit = 1;
	const prepared = setProductPrice(data, {
		newTotal: lineTotal,
		origin_total: data._extend.origin_total
	});
	syncSkuValueFromOther(prepared);
	prepared.note = (_prepared$_extend = prepared._extend) === null || _prepared$_extend === void 0 ? void 0 : _prepared$_extend.note;
	return prepared;
}
//#endregion
export { prepareNormalProductEditCacheItem, transformNormalProductCreateConfirm };
