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 skuDetailModal_default from "../../pro/skuDetailModal/index.js";
import React, { forwardRef, useImperativeHandle, useRef } from "react";
import { cloneDeep } from "lodash";
import { useMemoizedFn } from "ahooks";
//#region src/plus/normalProductDetailModal/index.tsx
/**
* 普通商品详情弹窗：基于 SkuDetailModal，仅 open / close / confirm / remove 回调，不依赖 Booking Context。
*/
const NormalProductDetailModal = forwardRef((props, ref) => {
	const { onConfirm: onConfirmProp, onRemove: onRemoveProp, onClose: onCloseProp } = props;
	const modalRef = useRef(null);
	const createPayloadRef = useRef(null);
	const openOnCloseRef = useRef(void 0);
	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") {
			const p = payload;
			createPayloadRef.current = p;
			modalRef.current.open({
				id: p.productId,
				date: p.date,
				is_eject: p.isEject,
				productData: p.productData
			});
			return;
		}
		createPayloadRef.current = null;
		if ((payload === null || payload === void 0 ? void 0 : payload.mode) === "edit") {
			const p = payload;
			modalRef.current.open(p.cacheItem);
			return;
		}
		modalRef.current.open(payload);
	} }));
	const handleRemove = useMemoizedFn((productData) => {
		onRemoveProp === null || onRemoveProp === void 0 || onRemoveProp(productData);
	});
	const handleConfirm = useMemoizedFn((data) => {
		var _data$_extend$origin_2, _data$_extend7, _data$_extend8, _data$_extend9, _updated$_extend;
		if (createPayloadRef.current) {
			var _data$_extend, _data$_extend$origin_, _data$_extend2, _data$_extend3, _data$_extend4, _data$_extend5, _data$_extend6;
			const payload = createPayloadRef.current;
			createPayloadRef.current = null;
			const quantity = Number((data === null || data === void 0 || (_data$_extend = data._extend) === null || _data$_extend === void 0 ? void 0 : _data$_extend.quantity) || 1);
			let detail = _objectSpread2(_objectSpread2({}, data || {}), {}, {
				id: payload.productId,
				product_id: payload.productId,
				_extend: _objectSpread2(_objectSpread2({}, (data === null || data === void 0 ? void 0 : data._extend) || {}), {}, {
					origin_total: (_data$_extend$origin_ = data === null || data === void 0 || (_data$_extend2 = data._extend) === null || _data$_extend2 === void 0 ? void 0 : _data$_extend2.origin_total) !== null && _data$_extend$origin_ !== void 0 ? _data$_extend$origin_ : data === null || data === void 0 || (_data$_extend3 = data._extend) === null || _data$_extend3 === void 0 ? void 0 : _data$_extend3.total,
					quantity,
					skuValue: cloneDeep((data === null || data === void 0 || (_data$_extend4 = data._extend) === null || _data$_extend4 === void 0 ? void 0 : _data$_extend4.skuValue) || {})
				})
			});
			data._extend.total = getProductTotalPrice(data);
			detail = setProductPrice(detail, {
				newTotal: data === null || data === void 0 || (_data$_extend5 = data._extend) === null || _data$_extend5 === void 0 ? void 0 : _data$_extend5.total,
				origin_total: (_data$_extend6 = data._extend) === null || _data$_extend6 === void 0 ? void 0 : _data$_extend6.origin_total
			});
			let formattedLine = _objectSpread2(_objectSpread2({ product_id: payload.productId }, detail._extend.other), {}, { quantity });
			formattedLine = formatDataKey(formattedLine);
			onConfirmProp === null || onConfirmProp === void 0 || onConfirmProp({
				mode: "create",
				data: formattedLine,
				detail
			});
			return;
		}
		const total = getProductTotalPrice(data);
		data._extend.total = total;
		data._extend.origin_total = (_data$_extend$origin_2 = (_data$_extend7 = data._extend) === null || _data$_extend7 === void 0 ? void 0 : _data$_extend7.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 = cloneDeep((data === null || data === void 0 || (_data$_extend8 = data._extend) === null || _data$_extend8 === void 0 ? void 0 : _data$_extend8.skuValue) || {});
		const updated = setProductPrice(data, {
			newTotal: total,
			origin_total: (_data$_extend9 = data._extend) === null || _data$_extend9 === void 0 ? void 0 : _data$_extend9.origin_total
		});
		updated.note = (_updated$_extend = updated._extend) === null || _updated$_extend === void 0 ? void 0 : _updated$_extend.note;
		onConfirmProp === null || onConfirmProp === void 0 || onConfirmProp({
			mode: "edit",
			data: updated
		});
	});
	const handleClose = useMemoizedFn(() => {
		var _openOnCloseRef$curre;
		createPayloadRef.current = null;
		(_openOnCloseRef$curre = openOnCloseRef.current) === null || _openOnCloseRef$curre === void 0 || _openOnCloseRef$curre.call(openOnCloseRef);
		openOnCloseRef.current = void 0;
		onCloseProp === null || onCloseProp === void 0 || onCloseProp();
	});
	return /* @__PURE__ */ React.createElement(skuDetailModal_default, {
		onClose: handleClose,
		onConfirm: handleConfirm,
		onRemove: handleRemove,
		ref: modalRef
	});
});
NormalProductDetailModal.displayName = "NormalProductDetailModal";
//#endregion
export { NormalProductDetailModal as default };
