import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { useSales } from "../../../businessModel/SalesModel/Sales/index.js";
import Step_default from "../Step/index.js";
import { useCustomer } from "../Customer/index.js";
import Cart_default from "../Cart/index.js";
import Checkout_default from "../Checkout/index.js";
import { getBoardSettingApi } from "./serve.js";
import Holder_default from "../Holder/index.js";
import Event_default from "../Event/index.js";
import Resource_default from "../Resource/index.js";
import Summary_default from "../Summary/index.js";
import React, { createContext, useEffect, useMemo } from "react";
import { useRequest } from "ahooks";
//#region src/components/Sales/Init/index.tsx
const InitContext = createContext(void 0);
const InitProvider = ({ children }) => {
	const { data, setData, hooks, getCurrentData, getSessionStorage } = useSales();
	const { appUtils } = useCustomer();
	const { useCustomInit } = hooks;
	useCustomInit();
	const initData = useMemo(() => {
		return data.initData || {};
	}, [data]);
	const init = () => {
		const storageData = getSessionStorage();
		setData(_objectSpread2({
			initData: Step_default.defaultState,
			stepData: Step_default.defaultState,
			cartData: Cart_default.defaultState,
			checkoutData: Checkout_default.defaultState,
			holderData: Holder_default.defaultState,
			eventData: Event_default.defaultState,
			resourceData: Resource_default.defaultState,
			summaryData: Summary_default.defaultState
		}, storageData || {}));
	};
	const setInitData = (newInitData) => {
		setData({ initData: _objectSpread2(_objectSpread2({}, data.initData), newInitData) });
	};
	/**
	* 返回第一步后需要重置部分数据
	*/
	const resetInitPartData = () => {
		var _getCurrentData$resou;
		setData(_objectSpread2(_objectSpread2({}, getCurrentData() || {}), {}, {
			cartData: Cart_default.defaultState,
			checkoutData: Checkout_default.defaultState,
			holderData: Holder_default.defaultState,
			eventData: Event_default.defaultState,
			summaryData: Summary_default.defaultState,
			resourceData: {
				resourceList: (_getCurrentData$resou = getCurrentData().resourceData) === null || _getCurrentData$resou === void 0 ? void 0 : _getCurrentData$resou.resourceList,
				currentResourceItem: null
			}
		}));
	};
	useEffect(() => {
		init();
	}, []);
	const contextValue = {
		initData,
		getBoardSetting: useRequest(getBoardSettingApi, {
			manual: true,
			onSuccess: (res) => {
				var _res$data, _res$data2, _appUtils$getStore, _res$data3;
				setInitData({
					boardSetting: res === null || res === void 0 ? void 0 : res.data,
					form_id: res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 || (_res$data = _res$data.config) === null || _res$data === void 0 || (_res$data = _res$data.resource) === null || _res$data === void 0 || (_res$data = _res$data.booking_event_v2_resource) === null || _res$data === void 0 ? void 0 : _res$data.form_id,
					shop_id: res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.shop_id,
					shop: (_appUtils$getStore = appUtils.getStore()) === null || _appUtils$getStore === void 0 || (_appUtils$getStore = _appUtils$getStore.getState()) === null || _appUtils$getStore === void 0 || (_appUtils$getStore = _appUtils$getStore.core) === null || _appUtils$getStore === void 0 || (_appUtils$getStore = _appUtils$getStore.core) === null || _appUtils$getStore === void 0 ? void 0 : _appUtils$getStore.shop,
					scheduleDisplayType: res === null || res === void 0 || (_res$data3 = res.data) === null || _res$data3 === void 0 || (_res$data3 = _res$data3.config) === null || _res$data3 === void 0 || (_res$data3 = _res$data3.product_display_setting) === null || _res$data3 === void 0 || (_res$data3 = _res$data3.booking_event_v2_schedule_display_format) === null || _res$data3 === void 0 ? void 0 : _res$data3.value
				});
			}
		}),
		resetInitPartData
	};
	return /* @__PURE__ */ React.createElement(InitContext.Provider, { value: contextValue }, children);
};
/**
* 使用步骤管理的Hook
* @throws {Error} 如果在StepProvider外部使用会抛出错误
* @returns {StepContextType} 步骤管理上下文
*/
const useInit = () => {
	const context = React.useContext(InitContext);
	if (!context) throw new Error("useInit must be used within a InitProvider");
	return context;
};
//#endregion
export { InitProvider, useInit };
