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 useEngineContext from "../../../hooks/useEngineContext.js";
import { useSales } from "../../../businessModel/SalesModel/Sales/index.js";
import { checkBeforeBookingApi, createBookingApi, freePayApi } from "./serve.js";
import { useCart } from "../Cart/index.js";
import { Toast } from "../../appointmentBooking/utils.js";
import React, { createContext, useRef } from "react";
import { useRequest } from "ahooks";
//#region src/components/Sales/Checkout/index.tsx
const defaultState = {};
const CheckoutContext = createContext(void 0);
const CheckoutProvider = ({ children }) => {
	const utils = useEngineContext().appHelper.utils || {};
	const { data, setData } = useSales();
	const { updateDisabledCartItem } = useCart();
	const checkoutCallback = useRef(null);
	const checkRef = useRef(null);
	const payRef = useRef(null);
	const checkBeforeBooking = useRequest(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 = useRequest(createBookingApi, {
		manual: true,
		onSuccess: (res) => {
			onPay(res.data);
			checkoutCallback.current({ success: true });
		},
		onError: (err) => {
			checkoutCallback.current({
				success: false,
				err
			});
		}
	});
	const freePay = useRequest(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 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 = _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: _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.createElement(CheckoutContext.Provider, { value: contextValue }, children);
};
/**
* 使用步骤管理的Hook
* @throws {Error} 如果在StepProvider外部使用会抛出错误
* @returns {StepContextType} 步骤管理上下文
*/
const useCheckout = () => {
	const context = React.useContext(CheckoutContext);
	if (!context) throw new Error("useCheckout must be used within a CheckoutProvider");
	return context;
};
var Checkout_default = { defaultState };
//#endregion
export { CheckoutProvider, Checkout_default as default, useCheckout };
