const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_utils = require("../utils.js");
const require_utils$1 = require("../../booking/utils.js");
const require_server = require("../server.js");
const require_index = require("../../../plus/skuOptionsSelection/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let ahooks = require("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] = (0, react.useState)(false);
	const [currentEditProduct, setCurrentEditProduct] = (0, react.useState)(null);
	const [skuValue, setSkuValue] = (0, react.useState)({});
	const skuOptionsSelectionRef = (0, react.useRef)(null);
	const localProductDataRef = (0, react.useRef)(null);
	const [skuDefaultValue, setSkuDefaultValue] = (0, react.useState)(defaultSkuValue);
	/**
	* @description: 获取商品详情请求
	*/
	const getProductDetailRequest = (0, ahooks.useRequest)(require_server.getProductDetail, {
		manual: true,
		onSuccess: (res) => {
			const detail = require_utils.isGiftProductPrice(res || {});
			setCurrentEditProduct(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, res), detail));
		},
		onError: (error) => {
			console.error("getProductDetail error:", error);
		}
	});
	/**
	* @description: 计算商品价格（包含规格）
	*/
	const getProductPrice = (0, ahooks.useMemoizedFn)((item) => {
		return require_utils$1.getProductTotalPrice(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { _extend: {
			price: item === null || item === void 0 ? void 0 : item.price,
			other: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), skuValue)
		} }));
	});
	/**
	* @description: 打开 SKU 选择弹窗
	* @param {any} product - 商品数据（可能包含本地的 _skuValue）
	*/
	const openSkuModal = (0, react.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 = (0, react.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 = (0, react.useCallback)(require_asyncToGenerator._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: (0, react.useMemo)(() => {
			return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
				width: 660,
				open: skuModalOpen,
				title: _pisell_utils.locales.getText("pisell2.text.shopping-cart.sku.modal.title"),
				onOk: handleSkuConfirm,
				okText: _pisell_utils.locales.getText("pisell2.text.shopping-cart.confirm"),
				cancelText: _pisell_utils.locales.getText("pisell2.text.shopping-cart.cancel"),
				destroyOnClose: true,
				onCancel: closeSkuModal,
				className: "shopping-cart-sku-modal"
			}, getProductDetailRequest.loading ? /* @__PURE__ */ react.default.createElement(antd.Skeleton, {
				active: true,
				loading: true
			}) : skuModalOpen && currentEditProduct && /* @__PURE__ */ react.default.createElement(require_index.default, {
				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
exports.useSkuModal = useSkuModal;
