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("../editService/utils.js");
const require_utils$1 = require("../../../utils.js");
const require_model = require("../../../info/model.js");
const require_index = require("../../../../../pro/skuDetailModal/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
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 modalRef = (0, react.useRef)(null);
	const createPayloadRef = (0, react.useRef)(null);
	/**
	* @description 记录最后一次打开弹窗时外部传入的 onClose，用于关闭时回调清理外部状态
	*/
	const openOnCloseRef = (0, react.useRef)(void 0);
	(0, react.useImperativeHandle)(ref, () => ({ init: (payload) => {
		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;
			modalRef.current.open({
				id: payload.productId,
				date: payload.date,
				is_eject: payload.isEject
			});
			return;
		}
		createPayloadRef.current = null;
		if ((payload === null || payload === void 0 ? void 0 : payload.mode) === "edit") {
			modalRef.current.open(payload.cacheItem);
			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
			}
		});
	});
	const handleConfirm = (0, ahooks.useMemoizedFn)((data) => {
		var _ref2, _state$service2;
		if (createPayloadRef.current) {
			var _data$_extend, _data$_extend2, _data$_extend3, _data$_extend4;
			const payload = createPayloadRef.current;
			createPayloadRef.current = null;
			const skuValue = (data === null || data === void 0 || (_data$_extend = data._extend) === null || _data$_extend === void 0 ? void 0 : _data$_extend.skuValue) || {
				product_id: payload.productId,
				product_variant_id: 0,
				bundle: [],
				option: []
			};
			const quantity = Number((data === null || data === void 0 || (_data$_extend2 = data._extend) === null || _data$_extend2 === void 0 ? void 0 : _data$_extend2.quantity) || 1);
			const 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 === null || data === void 0 || (_data$_extend3 = data._extend) === null || _data$_extend3 === void 0 ? void 0 : _data$_extend3.origin_total) || (data === null || data === void 0 || (_data$_extend4 = data._extend) === null || _data$_extend4 === void 0 ? void 0 : _data$_extend4.total),
					quantity
				}),
				origin_total: data.total
			});
			const e = {
				key: payload.productId,
				rowKey: payload.productId,
				product_id: payload.productId,
				price: skuValue.price || 0,
				product_variant_id: skuValue.product_variant_id || 0,
				bundle: skuValue.bundle || [],
				option: skuValue.option || [],
				quantity
			};
			payload.callback(e, void 0, detail, false);
			return;
		}
		dispatch({
			type: "setService",
			payload: {
				editModal: false,
				value: (_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$_extend5, _data$_extend6, _data$_extend7;
						const total = require_utils$1.getProductTotalPrice(data);
						data._extend.total = total;
						data._extend.origin_total = ((_data$_extend5 = data._extend) === null || _data$_extend5 === void 0 ? void 0 : _data$_extend5.origin_total) || total;
						data = require_utils.setProductPrice(data, {
							newTotal: total,
							origin_total: (_data$_extend6 = data._extend) === null || _data$_extend6 === void 0 ? void 0 : _data$_extend6.origin_total
						});
						data.note = (_data$_extend7 = data._extend) === null || _data$_extend7 === void 0 ? void 0 : _data$_extend7.note;
						if (data === null || data === void 0 ? void 0 : data.is_bundle) data._extend.bundle_edit = 1;
						return data;
					}
					return item;
				}),
				edit: true,
				cacheItem: null
			}
		});
	});
	const handleClose = (0, ahooks.useMemoizedFn)(() => {
		var _openOnCloseRef$curre;
		dispatch({
			type: "setService",
			payload: {
				editModal: false,
				cacheItem: null,
				changeService: false
			}
		});
		createPayloadRef.current = null;
		(_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;
