const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_utils = require("../../../../ticketBooking/components/addServiceVariant/utils.js");
const require_utils$1 = require("../editService/utils.js");
const require_utils$2 = require("../../../utils.js");
const require_model = require("../../../info/model.js");
const require_useCartChangeEffect = require("../../../../ticketBooking/hooks/pisellos/useCartChangeEffect.js");
const require_index = require("../../../../../pro/skuDetailModal/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let lodash = require("lodash");
let ahooks = require("ahooks");
//#region src/components/booking/info2/service/editNormalService/index.tsx
const EditNormalService = (0, react.forwardRef)((props, ref) => {
	const { state, dispatch } = (0, react.useContext)(require_model.Context);
	const { onCartChange } = require_useCartChangeEffect.default();
	const modalRef = (0, react.useRef)(null);
	const createPayloadRef = (0, react.useRef)(null);
	/**
	* @description 记录最后一次打开弹窗时外部传入的 onClose，用于关闭时回调清理外部状态
	*/
	const openOnCloseRef = (0, react.useRef)(void 0);
	/**
	* @description 记录 edit 模式下的 callback，用于编辑完成后回调
	*/
	const editCallbackRef = (0, react.useRef)(void 0);
	const [skuDetailConfig, setSkuDetailConfig] = (0, react.useState)(null);
	(0, react.useImperativeHandle)(ref, () => ({ init: (payload) => {
		setSkuDetailConfig((payload === null || payload === void 0 ? void 0 : payload.skuDetailConfig) || {});
		openOnCloseRef.current = payload === null || payload === void 0 ? void 0 : payload.onClose;
		if ((payload === null || payload === void 0 ? void 0 : payload.mode) === "create") {
			createPayloadRef.current = payload;
			editCallbackRef.current = void 0;
			modalRef.current.open({
				id: payload.productId,
				date: payload.date,
				is_eject: payload.isEject,
				productData: payload.productData,
				skuDetailConfig: (payload === null || payload === void 0 ? void 0 : payload.skuDetailConfig) || {}
			});
			return;
		}
		createPayloadRef.current = null;
		editCallbackRef.current = payload === null || payload === void 0 ? void 0 : payload.callback;
		if ((payload === null || payload === void 0 ? void 0 : payload.mode) === "edit") {
			const cacheItemWithConfig = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, payload.cacheItem), {}, { skuDetailConfig: (payload === null || payload === void 0 ? void 0 : payload.skuDetailConfig) || {} });
			modalRef.current.open(cacheItemWithConfig);
			return;
		}
		modalRef.current.open(payload);
	} }));
	const handleRemove = (0, ahooks.useMemoizedFn)((productData) => {
		var _ref, _state$service;
		dispatch({
			type: "setService",
			payload: {
				editModal: false,
				value: (_ref = (state === null || state === void 0 || (_state$service = state.service) === null || _state$service === void 0 ? void 0 : _state$service.value) || []) === null || _ref === void 0 ? void 0 : _ref.filter((item) => {
					return item._id !== productData._id;
				}),
				edit: true,
				cacheItem: null
			}
		});
		if (skuDetailConfig === null || skuDetailConfig === void 0 ? void 0 : skuDetailConfig.onRemove) skuDetailConfig === null || skuDetailConfig === void 0 || skuDetailConfig.onRemove(productData);
	});
	const handleConfirm = (0, ahooks.useMemoizedFn)((data) => {
		var _data$_extend8, _ref2, _state$service2;
		if (createPayloadRef.current) {
			var _data$_extend, _data$_extend2, _data$_extend$origin_, _data$_extend3, _data$_extend4, _data$_extend5, _data$_extend6, _data$_extend7;
			const payload = createPayloadRef.current;
			createPayloadRef.current = null;
			!(data === null || data === void 0 || (_data$_extend = data._extend) === null || _data$_extend === void 0) && _data$_extend.skuValue || payload.productId;
			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 = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, data || {}), {}, {
				id: payload.productId,
				product_id: payload.productId,
				_extend: require_objectSpread2._objectSpread2(require_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: (0, lodash.cloneDeep)((data === null || data === void 0 || (_data$_extend5 = data._extend) === null || _data$_extend5 === void 0 ? void 0 : _data$_extend5.skuValue) || {})
				})
			});
			data._extend.total = require_utils$2.getProductTotalPrice(data);
			detail = require_utils$1.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 = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({ product_id: payload.productId }, detail._extend.other), {}, { quantity });
			e = require_utils.formatDataKey(e);
			payload.callback(e, void 0, detail, false);
			if (skuDetailConfig === null || skuDetailConfig === void 0 ? void 0 : skuDetailConfig.onConfirm) skuDetailConfig === null || skuDetailConfig === void 0 || skuDetailConfig.onConfirm(data);
			return;
		}
		console.log("handleConfirm", data, data === null || data === void 0 || (_data$_extend8 = data._extend) === null || _data$_extend8 === void 0 ? void 0 : _data$_extend8.origin_total);
		const newProducts = (_ref2 = (state === null || state === void 0 || (_state$service2 = state.service) === null || _state$service2 === void 0 ? void 0 : _state$service2.value) || []) === null || _ref2 === void 0 ? void 0 : _ref2.map((item) => {
			if (item._id == data._id) {
				var _data$_extend$origin_2, _data$_extend9, _data$_extend10, _data$_extend11, _data$_extend12;
				const total = require_utils$2.getProductTotalPrice(data);
				data._extend.total = total;
				data._extend.origin_total = (_data$_extend$origin_2 = (_data$_extend9 = data._extend) === null || _data$_extend9 === void 0 ? void 0 : _data$_extend9.origin_total) !== null && _data$_extend$origin_2 !== void 0 ? _data$_extend$origin_2 : total;
				if (data === null || data === void 0 ? void 0 : data.is_bundle) data._extend.bundle_edit = 1;
				data._extend._skuValue = (0, lodash.cloneDeep)((data === null || data === void 0 || (_data$_extend10 = data._extend) === null || _data$_extend10 === void 0 ? void 0 : _data$_extend10.skuValue) || {});
				data = require_utils$1.setProductPrice(data, {
					newTotal: total,
					origin_total: (_data$_extend11 = data._extend) === null || _data$_extend11 === void 0 ? void 0 : _data$_extend11.origin_total
				});
				data.note = (_data$_extend12 = data._extend) === null || _data$_extend12 === void 0 ? void 0 : _data$_extend12.note;
				return data;
			}
			return item;
		});
		if (editCallbackRef.current) {
			editCallbackRef.current(data);
			editCallbackRef.current = void 0;
		}
		if (skuDetailConfig === null || skuDetailConfig === void 0 ? void 0 : skuDetailConfig.onConfirm) {
			skuDetailConfig === null || skuDetailConfig === void 0 || skuDetailConfig.onConfirm(data);
			return;
		}
		onCartChange("update", newProducts);
		dispatch({
			type: "setService",
			payload: {
				editModal: false,
				value: newProducts,
				edit: true,
				cacheItem: null
			}
		});
	});
	const handleClose = (0, ahooks.useMemoizedFn)(() => {
		var _openOnCloseRef$curre;
		dispatch({
			type: "setService",
			payload: {
				editModal: false,
				cacheItem: null,
				changeService: false
			}
		});
		if (skuDetailConfig === null || skuDetailConfig === void 0 ? void 0 : skuDetailConfig.onClose) skuDetailConfig === null || skuDetailConfig === void 0 || skuDetailConfig.onClose();
		createPayloadRef.current = null;
		editCallbackRef.current = void 0;
		(_openOnCloseRef$curre = openOnCloseRef.current) === null || _openOnCloseRef$curre === void 0 || _openOnCloseRef$curre.call(openOnCloseRef);
		openOnCloseRef.current = void 0;
	});
	return /* @__PURE__ */ react.default.createElement(require_index.default, {
		onClose: handleClose,
		onConfirm: handleConfirm,
		onRemove: handleRemove,
		ref: modalRef
	});
});
//#endregion
exports.default = EditNormalService;
