const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../../../businessModel/SalesModel/Sales/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
//#region src/components/Sales/Step/index.tsx
const steps = [
	{
		key: "select-category",
		title: "Step 1",
		description: "Select category"
	},
	{
		key: "select-event",
		title: "Step 2",
		description: "Select event"
	},
	{
		key: "confirm-information",
		title: "Step 3",
		description: "Confirm information"
	}
];
const stepMaps = steps.reduce((acc, step) => {
	acc[step.key] = step;
	return acc;
}, {});
const defaultState = {
	currentStep: steps[0].key,
	isAutoOperate: true
};
const StepContext = (0, react.createContext)(void 0);
const StepProvider = ({ children }) => {
	const { data, setData } = require_index.useSales();
	const utils = require_useEngineContext.default().appHelper.utils || {};
	const stepData = (0, react.useMemo)(() => {
		return data.stepData || {};
	}, [data]);
	const currentStepData = (0, react.useMemo)(() => {
		return stepMaps[stepData.currentStep];
	}, [stepData]);
	const setStepData = (newStepData) => {
		setData({ stepData: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, data.stepData), newStepData) });
	};
	const getNextStep = () => {
		const currentStepIndex = steps.findIndex((step) => step.key === stepData.currentStep);
		if (currentStepIndex < steps.length - 1) return steps[currentStepIndex + 1].key;
		return null;
	};
	const getPrevStep = () => {
		const currentStepIndex = steps.findIndex((step) => step.key === stepData.currentStep);
		if (currentStepIndex > 0) return steps[currentStepIndex - 1].key;
		return null;
	};
	const nextStep = () => {
		const nextStep = getNextStep();
		if (nextStep) setStepData({
			currentStep: nextStep,
			isAutoOperate: true
		});
	};
	const prevStep = () => {
		const prevStep = getPrevStep();
		if (prevStep) setStepData({
			currentStep: prevStep,
			isAutoOperate: false
		});
		else closePage();
	};
	const toFirstStep = () => {
		setStepData({ currentStep: steps[0].key });
	};
	const closePage = () => {
		var _utils$history;
		utils === null || utils === void 0 || (_utils$history = utils.history) === null || _utils$history === void 0 || _utils$history.replace("/");
	};
	/**
	* @title: 是否是第一步
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-18 22:23
	*/
	const isFirstStep = (0, react.useMemo)(() => {
		return (currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key) === steps[0].key;
	}, [currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key]);
	/**
	* @title: 是否是最后一步
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-18 22:24
	*/
	const isLastStep = (0, react.useMemo)(() => {
		return (currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key) === steps[steps.length - 1].key;
	}, [currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key]);
	/**
	* @title: 是否是第二步
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-18 22:39
	*/
	const isTwoStep = (0, react.useMemo)(() => {
		return (currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key) === steps[1].key;
	}, [currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key]);
	const changeAutoOperate = (newAutoOperate) => {
		setStepData({ isAutoOperate: newAutoOperate });
	};
	const contextValue = {
		setStepData,
		currentStepData,
		nextStep,
		prevStep,
		closePage,
		toFirstStep,
		isFirstStep,
		isLastStep,
		isTwoStep,
		changeAutoOperate
	};
	return /* @__PURE__ */ react.default.createElement(StepContext.Provider, { value: contextValue }, children);
};
/**
* 使用步骤管理的Hook
* @throws {Error} 如果在StepProvider外部使用会抛出错误
* @returns {StepContextType} 步骤管理上下文
*/
const useStep = () => {
	const context = react.default.useContext(StepContext);
	if (!context) throw new Error("useStep must be used within a StepProvider");
	return context;
};
var Step_default = { defaultState };
//#endregion
exports.StepProvider = StepProvider;
exports.default = Step_default;
exports.useStep = useStep;
