import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { setDefaultCapacitys } from "../../../components/booking/info2/service/addService/utils.js";
import { getProductTotalPrice } from "../../../components/booking/utils.js";
import { editSessionProduct } from "../../../components/booking/info/utils.js";
//#region src/plus/salesSdk/bookingEditService/applyProductDetailEditInDrawer.ts
/**
* 预约商品规格弹窗确认后，合并到抽屉内 cacheItem（对齐 Info2 editService handleEdit callback）。
*
* @example
* const { cacheItem: next, formPatch } = applyProductDetailEditInDrawer({
*   cacheItem,
*   formValues: form.getFieldsValue(),
*   detailResult,
* });
* onCacheItemChange(next);
* form.setFieldsValue(formPatch);
*/
function applyProductDetailEditInDrawer(input) {
	var _item$id, _e$key, _detail$_extend$price, _detail$_extend, _item$_extend, _item$new, _nextItem$_extend, _nextItem$_extend2, _nextItem$_extend3, _item$_extend2, _nextItem$_extend4;
	const { cacheItem: item, formValues, detailResult } = input;
	const { e, detail } = detailResult;
	const _detail = detail || item;
	const other = JSON.parse(JSON.stringify(e || {}));
	other.session = e === null || e === void 0 ? void 0 : e.session;
	const nextItem = _objectSpread2(_objectSpread2(_objectSpread2({}, item), _detail), {}, {
		product_id: (_item$id = item.id) !== null && _item$id !== void 0 ? _item$id : item.product_id,
		_id: item === null || item === void 0 ? void 0 : item._id,
		_key: (_e$key = e === null || e === void 0 ? void 0 : e.key) !== null && _e$key !== void 0 ? _e$key : item._key,
		_extend: _objectSpread2(_objectSpread2(_objectSpread2({}, item._extend), formValues), {}, {
			quantity: (e === null || e === void 0 ? void 0 : e.quantity) || (formValues === null || formValues === void 0 ? void 0 : formValues.quantity) || 1,
			other,
			priceOverride: (_detail$_extend$price = (_detail$_extend = _detail._extend) === null || _detail$_extend === void 0 ? void 0 : _detail$_extend.priceOverride) !== null && _detail$_extend$price !== void 0 ? _detail$_extend$price : (_item$_extend = item._extend) === null || _item$_extend === void 0 ? void 0 : _item$_extend.priceOverride
		}),
		new: (_item$new = item === null || item === void 0 ? void 0 : item.new) !== null && _item$new !== void 0 ? _item$new : 0
	});
	const total = getProductTotalPrice(nextItem);
	const hadManualOverride = ((_nextItem$_extend = nextItem._extend) === null || _nextItem$_extend === void 0 ? void 0 : _nextItem$_extend.priceOverride) !== void 0 && ((_nextItem$_extend2 = nextItem._extend) === null || _nextItem$_extend2 === void 0 ? void 0 : _nextItem$_extend2.priceOverride) !== null && ((_nextItem$_extend3 = nextItem._extend) === null || _nextItem$_extend3 === void 0 ? void 0 : _nextItem$_extend3.priceOverride) !== "";
	const preservedOriginTotal = (_item$_extend2 = item._extend) === null || _item$_extend2 === void 0 ? void 0 : _item$_extend2.origin_total;
	nextItem._extend.total = total;
	nextItem._extend.origin_total = hadManualOverride ? preservedOriginTotal !== null && preservedOriginTotal !== void 0 ? preservedOriginTotal : total : total;
	try {
		var _nextItem$_data;
		nextItem._extend.capacity = setDefaultCapacitys({
			capacitys: (_nextItem$_data = nextItem._data) === null || _nextItem$_data === void 0 ? void 0 : _nextItem$_data.capacitys,
			cacheItem: nextItem
		});
	} catch (_unused) {}
	const formPatch = {
		total,
		origin_total: nextItem._extend.origin_total,
		capacity: (_nextItem$_extend4 = nextItem._extend) === null || _nextItem$_extend4 === void 0 ? void 0 : _nextItem$_extend4.capacity,
		quantity: (e === null || e === void 0 ? void 0 : e.quantity) || (formValues === null || formValues === void 0 ? void 0 : formValues.quantity) || 1
	};
	const sessionDetail = editSessionProduct(nextItem, e === null || e === void 0 ? void 0 : e.session);
	if (sessionDetail) {
		formPatch.start_time = sessionDetail.start_time;
		formPatch.duration = sessionDetail.duration;
	}
	return {
		cacheItem: nextItem,
		formPatch
	};
}
/**
* 普通商品 SkuDetailModal 确认后，保留抽屉内资源/时间表单值并写回 cacheItem。
*
* @example
* const { cacheItem: next, formPatch } = mergeNormalProductEditInDrawer({
*   cacheItem,
*   editedCacheItem: result.cacheItem,
*   formValues: form.getFieldsValue(),
* });
*/
function mergeNormalProductEditInDrawer(input) {
	var _editedCacheItem$_key, _item$new2, _item$_data, _formValues$resource, _item$_extend3, _ref, _formValues$start_tim, _item$_extend4, _ref2, _formValues$start_dat, _item$_extend5, _ref3, _formValues$end_date, _item$_extend6, _ref4, _formValues$duration, _item$_extend7, _formValues$holder_id, _item$_extend8, _formValues$note, _item$_extend9, _editedExtend$other, _item$_extend10, _ref5, _editedExtend$quantit, _item$_extend11;
	const { cacheItem: item, editedCacheItem, formValues } = input;
	const editedExtend = (editedCacheItem === null || editedCacheItem === void 0 ? void 0 : editedCacheItem._extend) || {};
	const nextItem = _objectSpread2(_objectSpread2(_objectSpread2({}, item), editedCacheItem), {}, {
		_id: item === null || item === void 0 ? void 0 : item._id,
		_key: (_editedCacheItem$_key = editedCacheItem._key) !== null && _editedCacheItem$_key !== void 0 ? _editedCacheItem$_key : item._key,
		new: (_item$new2 = item === null || item === void 0 ? void 0 : item.new) !== null && _item$new2 !== void 0 ? _item$new2 : 0,
		_data: (_item$_data = item._data) !== null && _item$_data !== void 0 ? _item$_data : editedCacheItem._data,
		_extend: _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, item._extend), editedExtend), formValues), {}, {
			resource: (_formValues$resource = formValues.resource) !== null && _formValues$resource !== void 0 ? _formValues$resource : (_item$_extend3 = item._extend) === null || _item$_extend3 === void 0 ? void 0 : _item$_extend3.resource,
			start_time: (_ref = (_formValues$start_tim = formValues.start_time) !== null && _formValues$start_tim !== void 0 ? _formValues$start_tim : editedExtend.start_time) !== null && _ref !== void 0 ? _ref : (_item$_extend4 = item._extend) === null || _item$_extend4 === void 0 ? void 0 : _item$_extend4.start_time,
			start_date: (_ref2 = (_formValues$start_dat = formValues.start_date) !== null && _formValues$start_dat !== void 0 ? _formValues$start_dat : editedExtend.start_date) !== null && _ref2 !== void 0 ? _ref2 : (_item$_extend5 = item._extend) === null || _item$_extend5 === void 0 ? void 0 : _item$_extend5.start_date,
			end_date: (_ref3 = (_formValues$end_date = formValues.end_date) !== null && _formValues$end_date !== void 0 ? _formValues$end_date : editedExtend.end_date) !== null && _ref3 !== void 0 ? _ref3 : (_item$_extend6 = item._extend) === null || _item$_extend6 === void 0 ? void 0 : _item$_extend6.end_date,
			duration: (_ref4 = (_formValues$duration = formValues.duration) !== null && _formValues$duration !== void 0 ? _formValues$duration : editedExtend.duration) !== null && _ref4 !== void 0 ? _ref4 : (_item$_extend7 = item._extend) === null || _item$_extend7 === void 0 ? void 0 : _item$_extend7.duration,
			holder_id: (_formValues$holder_id = formValues.holder_id) !== null && _formValues$holder_id !== void 0 ? _formValues$holder_id : (_item$_extend8 = item._extend) === null || _item$_extend8 === void 0 ? void 0 : _item$_extend8.holder_id,
			note: (_formValues$note = formValues.note) !== null && _formValues$note !== void 0 ? _formValues$note : (_item$_extend9 = item._extend) === null || _item$_extend9 === void 0 ? void 0 : _item$_extend9.note,
			other: (_editedExtend$other = editedExtend.other) !== null && _editedExtend$other !== void 0 ? _editedExtend$other : (_item$_extend10 = item._extend) === null || _item$_extend10 === void 0 ? void 0 : _item$_extend10.other,
			total: editedExtend.total,
			origin_total: editedExtend.origin_total,
			quantity: (_ref5 = (_editedExtend$quantit = editedExtend.quantity) !== null && _editedExtend$quantit !== void 0 ? _editedExtend$quantit : formValues.quantity) !== null && _ref5 !== void 0 ? _ref5 : (_item$_extend11 = item._extend) === null || _item$_extend11 === void 0 ? void 0 : _item$_extend11.quantity
		})
	});
	return {
		cacheItem: nextItem,
		formPatch: {
			total: nextItem._extend.total,
			origin_total: nextItem._extend.origin_total,
			quantity: nextItem._extend.quantity,
			capacity: nextItem._extend.capacity
		}
	};
}
//#endregion
export { applyProductDetailEditInDrawer, mergeNormalProductEditInDrawer };
