import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { isGiftProductPrice } from "../utils.js";
import { getProductTotalPrice } from "../../booking/utils.js";
import { getProductDetail } from "../server.js";
import SKUOptionsSelection from "../../../plus/skuOptionsSelection/index.js";
import React, { useCallback, useMemo, useRef, useState } from "react";
import { Skeleton } from "antd";
import { locales } from "@pisell/utils";
import { PisellModal } from "@pisell/materials";
import { useMemoizedFn, useRequest } from "ahooks";
//#region src/components/shoppingCart/hooks/useSkuModal.tsx
const defaultSkuValue = {
	product_id: 0,
	price: 0,
	product_variant_id: 0,
	bundle: [],
	option: []
};
const useSkuModal = (options) => {
	const [skuModalOpen, setSkuModalOpen] = useState(false);
	const [currentEditProduct, setCurrentEditProduct] = useState(null);
	const [skuValue, setSkuValue] = useState({});
	const skuOptionsSelectionRef = useRef(null);
	const localProductDataRef = useRef(null);
	const [skuDefaultValue, setSkuDefaultValue] = useState(defaultSkuValue);
	/**
	* @description: 获取商品详情请求
	*/
	const getProductDetailRequest = useRequest(getProductDetail, {
		manual: true,
		onSuccess: (res) => {
			const detail = isGiftProductPrice(res || {});
			setCurrentEditProduct(_objectSpread2(_objectSpread2({}, res), detail));
		},
		onError: (error) => {
			console.error("getProductDetail error:", error);
		}
	});
	/**
	* @description: 计算商品价格（包含规格）
	*/
	const getProductPrice = useMemoizedFn((item) => {
		return getProductTotalPrice(_objectSpread2(_objectSpread2({}, item), {}, { _extend: {
			price: item === null || item === void 0 ? void 0 : item.price,
			other: _objectSpread2(_objectSpread2({}, item), skuValue)
		} }));
	});
	/**
	* @description: 打开 SKU 选择弹窗
	* @param {any} product - 商品数据（可能包含本地的 _skuValue）
	*/
	const openSkuModal = useCallback((product) => {
		setSkuDefaultValue((product === null || product === void 0 ? void 0 : product._skuValue) || setSkuDefaultValue);
		setSkuModalOpen(true);
		getProductDetailRequest.run({ id: product === null || product === void 0 ? void 0 : product.id });
	}, [getProductDetailRequest]);
	/**
	* @description: 关闭 SKU 选择弹窗
	*/
	const closeSkuModal = useCallback(() => {
		var _options$onCancel;
		setSkuModalOpen(false);
		setCurrentEditProduct(null);
		setSkuValue(null);
		setSkuDefaultValue(defaultSkuValue);
		localProductDataRef.current = null;
		options === null || options === void 0 || (_options$onCancel = options.onCancel) === null || _options$onCancel === void 0 || _options$onCancel.call(options);
	}, [options === null || options === void 0 ? void 0 : options.onCancel]);
	/**
	* @description: SKU 选择确认
	*/
	const handleSkuConfirm = useCallback(_asyncToGenerator(function* () {
		var _skuOptionsSelectionR, _options$onConfirm;
		if (!(yield skuOptionsSelectionRef === null || skuOptionsSelectionRef === void 0 || (_skuOptionsSelectionR = skuOptionsSelectionRef.current) === null || _skuOptionsSelectionR === void 0 ? void 0 : _skuOptionsSelectionR.validate())) return;
		const _price = getProductPrice(currentEditProduct);
		yield options === null || options === void 0 || (_options$onConfirm = options.onConfirm) === null || _options$onConfirm === void 0 ? void 0 : _options$onConfirm.call(options, currentEditProduct === null || currentEditProduct === void 0 ? void 0 : currentEditProduct.id, skuValue, _price);
		closeSkuModal();
	}), [
		skuValue,
		currentEditProduct,
		getProductPrice,
		options === null || options === void 0 ? void 0 : options.onConfirm,
		closeSkuModal
	]);
	return {
		openSkuModal,
		closeSkuModal,
		SkuModal: useMemo(() => {
			return /* @__PURE__ */ React.createElement(PisellModal, {
				width: 660,
				open: skuModalOpen,
				title: locales.getText("pisell2.text.shopping-cart.sku.modal.title"),
				onOk: handleSkuConfirm,
				okText: locales.getText("pisell2.text.shopping-cart.confirm"),
				cancelText: locales.getText("pisell2.text.shopping-cart.cancel"),
				destroyOnClose: true,
				onCancel: closeSkuModal,
				className: "shopping-cart-sku-modal"
			}, getProductDetailRequest.loading ? /* @__PURE__ */ React.createElement(Skeleton, {
				active: true,
				loading: true
			}) : skuModalOpen && currentEditProduct && /* @__PURE__ */ React.createElement(SKUOptionsSelection, {
				ref: skuOptionsSelectionRef,
				dataSource: currentEditProduct,
				value: skuDefaultValue,
				skuCardConfig: {
					title: { lineClamp: 1 },
					subTitle: { lineClamp: 1 }
				},
				customConfig: { modalWidth: "660px" },
				onChange: (v) => {
					console.log("SKUOptionsSelection onChange:", v);
					setSkuValue(v);
				}
			}));
		}, [
			skuModalOpen,
			currentEditProduct,
			skuDefaultValue,
			getProductDetailRequest.loading,
			handleSkuConfirm,
			closeSkuModal
		]),
		isSkuModalOpen: skuModalOpen,
		currentEditProduct,
		skuValue
	};
};
//#endregion
export { useSkuModal };
