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_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../../../businessModel/SalesModel/Sales/index.js");
const require_serve = require("./serve.js");
const require_index$1 = require("../Cart/index.js");
const require_utils = require("../../appointmentBooking/utils.js");
let react = require("react");
react = require_runtime.__toESM(react);
let ahooks = require("ahooks");
//#region src/components/Sales/Checkout/index.tsx
const defaultState = {};
const CheckoutContext = (0, react.createContext)(void 0);
const CheckoutProvider = ({ children }) => {
	const utils = require_useEngineContext.default().appHelper.utils || {};
	const { data, setData } = require_index.useSales();
	const { updateDisabledCartItem } = require_index$1.useCart();
	const checkoutCallback = (0, react.useRef)(null);
	const checkRef = (0, react.useRef)(null);
	const payRef = (0, react.useRef)(null);
	const checkBeforeBooking = (0, ahooks.useRequest)(require_serve.checkBeforeBookingApi, {
		manual: true,
		onSuccess: (res) => {
			var _res$data;
			if ((_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.length) {
				var _res$data2;
				updateDisabledCartItem(res.data);
				checkRef.current({
					result: false,
					num: (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.length
				});
			} else checkRef.current({ result: true });
		}
	});
	const createBooking = (0, ahooks.useRequest)(require_serve.createBookingApi, {
		manual: true,
		onSuccess: (res) => {
			onPay(res.data);
			checkoutCallback.current({ success: true });
		},
		onError: (err) => {
			checkoutCallback.current({
				success: false,
				err
			});
		}
	});
	const freePay = (0, ahooks.useRequest)(require_serve.freePayApi, {
		manual: true,
		onSuccess: (res) => {
			if (res === null || res === void 0 ? void 0 : res.status) {
				var _payRef$current, _payRef$current2;
				goPaymentFinish((_payRef$current = payRef.current) === null || _payRef$current === void 0 ? void 0 : _payRef$current.order_id, (_payRef$current2 = payRef.current) === null || _payRef$current2 === void 0 ? void 0 : _payRef$current2.total_amount);
			} else require_utils.Toast(res === null || res === void 0 ? void 0 : res.message);
		}
	});
	/**
	* @title: 下单前校验
	* @param {any} data
	* @return {*}
	* @Author: xiangfeng.xue
	*/
	const onCheckBeforeBooking = (data) => {
		return new Promise((resolve) => {
			checkRef.current = (res) => {
				resolve(res);
				checkRef.current = null;
			};
			checkBeforeBooking.runAsync(data);
		});
	};
	/**
	* @title: 下单
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	*/
	const onCheckout = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (data) {
			return new Promise((resolve) => {
				checkoutCallback.current = (e) => {
					resolve(e);
					checkoutCallback.current = null;
				};
				createBooking.runAsync(data);
			});
		});
		return function onCheckout(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const goPaymentFinish = (order_id, amount) => {
		var _utils$history;
		const url = `/payment-finish/success?order_id=${order_id}&amount=${Number(amount)}`;
		utils === null || utils === void 0 || (_utils$history = utils.history) === null || _utils$history === void 0 || _utils$history.replace(url);
	};
	/**
	* @title: 支付
	* @description:
	* @param {PayProps} payProps
	* @return {*}
	* @Author: zhiwei.Wang
	*/
	const onPay = (payProps) => {
		payRef.current = payProps;
		const { is_deposit, deposit_amount, total_amount } = payProps;
		const _total = is_deposit ? deposit_amount || 0 : total_amount || 0;
		if (_total > 0) utils.action({
			type: "pisell1.payment",
			data: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, payProps), {}, { total_amount: _total }),
			callback: (e) => {
				checkoutCallback.current && checkoutCallback.current(e);
			}
		});
		else {
			if (is_deposit) {
				goPaymentFinish(payProps.order_id, _total);
				return;
			}
			freePay.runAsync(payProps.order_id);
		}
	};
	const contextValue = {
		onCheckBeforeBooking,
		onCheckout,
		onPay,
		createBooking,
		checkBeforeBooking,
		freePay
	};
	return /* @__PURE__ */ react.default.createElement(CheckoutContext.Provider, { value: contextValue }, children);
};
/**
* 使用步骤管理的Hook
* @throws {Error} 如果在StepProvider外部使用会抛出错误
* @returns {StepContextType} 步骤管理上下文
*/
const useCheckout = () => {
	const context = react.default.useContext(CheckoutContext);
	if (!context) throw new Error("useCheckout must be used within a CheckoutProvider");
	return context;
};
var Checkout_default = { defaultState };
//#endregion
exports.CheckoutProvider = CheckoutProvider;
exports.default = Checkout_default;
exports.useCheckout = useCheckout;
