import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { ModeEnum, PayRouteEnum, StatusEnum } from "../const.js";
import { cloneDeep } from "../helper.js";
import useThunkReducer from "./useThunkReducer.js";
import { _objectDestructuringEmpty } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectDestructuringEmpty.js";
import { _extends } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/extends.js";
import React, { createContext, useContext } from "react";
//#region src/components/eftposPay/store/index.tsx
let EActionTypes = /* @__PURE__ */ function(EActionTypes) {
	EActionTypes["UPDATE_INIT"] = "UPDATE_INIT";
	EActionTypes["UPDATE_CUSTOM"] = "UPDATE_CUSTOM";
	EActionTypes["UPDATE_KEY"] = "UPDATE_KEY";
	EActionTypes["UPDATE_STEP_KEY"] = "UPDATE_STEP_KEY";
	EActionTypes["UPDATE_DEVICE"] = "UPDATE_DEVICE";
	EActionTypes["UPDATE_NEXT_STEP"] = "UPDATE_NEXT_STEP";
	EActionTypes["UPDATE_STATUS"] = "UPDATE_STATUS";
	EActionTypes["UPDATE_TITLE"] = "UPDATE_TITLE";
	EActionTypes["UPDATE_LOADING"] = "UPDATE_LOADING";
	EActionTypes["UPDATE_COMPONENT"] = "UPDATE_COMPONENT";
	EActionTypes["UPDATE_STEPS"] = "UPDATE_STEPS";
	EActionTypes["UPDATE_STEP_CURRENT_ITEM_CUSTOM"] = "UPDATE_STEP_CURRENT_ITEM_CUSTOM";
	EActionTypes["UPDATE_STEP_ITEM"] = "UPDATE_STEP_ITEM";
	EActionTypes["BACKUP_PRODUCTION"] = "BACKUP_PRODUCTION";
	EActionTypes["BACKUP_FREE"] = "BACKUP_FREE";
	EActionTypes["UPDATE_CLIENT"] = "UPDATE_CLIENT";
	return EActionTypes;
}({});
/**
* @title: 刷新 key
* @description: 用户重置组件生命周期,
* @param {number} key
* @Author: hongbing.wang
* @Date: 2024-08-19 09:56
*/
const updateKey = (key) => ({
	type: EActionTypes.UPDATE_KEY,
	payload: { key }
});
/**
* @title: 更新设备信息
* @description: 用户选择设备将回填到此处
* @param {object} payload
* @Author: hongbing.wang
* @Date: 2024-08-19 09:57
*/
const updateDevice = (payload) => ({
	type: EActionTypes.UPDATE_DEVICE,
	payload
});
/**
* @title: 更新至下一步
* @description: 更新至下一步
* @param {string} warn?: 错误信息
* @Author: hongbing.wang
* @Date: 2024-08-19 09:58
*/
const updateNextStep = (warn) => ({
	type: EActionTypes.UPDATE_NEXT_STEP,
	payload: warn ? { warn } : {}
});
/**
* @title: 更新自定义信息
* @description: 更新自定义信息
* @param {object} payload
* @Author: hongbing.wang
* @Date: 2024-08-19 10:03
*/
const updateCustom = (payload) => ({
	type: EActionTypes.UPDATE_CUSTOM,
	payload
});
/**
* @title: 更新 全局状态
* @description: 更新 全局状态
* @param {string} status : 状态
*  * @Author: hongbing.wang
* @Date: 2024-08-19 10:03
*/
const updateStatus = (status) => ({
	type: EActionTypes.UPDATE_LOADING,
	payload: { status }
});
/**
* @title: 更新自定义组件
* @description: 更新自定义组件
* @param {string} component 组件名称
* @param {boolean} render : true : 隐藏 原组件(维持原组件内状态)  ;false : 卸载
* @return {*}
* @Author: hongbing.wang
* @Date: 2024-08-19 10:04
*/
const updateComponent = (component, render) => ({
	type: EActionTypes.UPDATE_COMPONENT,
	payload: component ? {
		component,
		type: "unset",
		render: typeof render === "boolean" ? render : false
	} : {
		type: "step",
		render: typeof render === "boolean" ? render : true
	}
});
/**
* @title: 更新步骤信息
* @description: 更新某一步骤信息
* @param {State} steps 某一步骤信息
* @Author: hongbing.wang
* @Date: 2024-08-19 10:07
*/
const updateSteps = (steps) => ({
	type: EActionTypes.UPDATE_STEPS,
	payload: { steps }
});
/**
* @title: 更新状态管理信息
* @description: 更新状态管理信息
* @param {Partial} payload
* @return {*}
* @Author: hongbing.wang
* @Date: 2024-08-19 10:08
*/
const updateState = (payload) => ({
	type: EActionTypes.UPDATE_INIT,
	payload
});
/**
* @title: 更新当前步骤信息
* @description: 更新当前步骤信息
* @Author: hongbing.wang
* @Date: 2024-08-19 10:09
*/
const updateClient = (payload) => ({
	type: EActionTypes.UPDATE_CLIENT,
	payload: { client: payload }
});
/**
* @title: 更新备份信息
* @description: 更新备份信息
* @Author: hongbing.wang
* @Date: 2024-08-19 10:25
*/
const backUpProduction = (payload) => ({
	type: EActionTypes.BACKUP_PRODUCTION,
	payload
});
/**
* @title: 释放备份信息
* @description: 释放备份信息
* @param {Partial} payload
* @Author: hongbing.wang
* @Date: 2024-08-19 10:27
*/
const backUpFree = (payload) => ({
	type: EActionTypes.BACKUP_FREE,
	payload: _objectSpread2({}, payload)
});
const initialState = {
	name: "",
	symbol: "",
	amount: "",
	order_id: "",
	mode: ModeEnum.Pay,
	eftpos: "",
	key: 0,
	step: 0,
	type: PayRouteEnum.Step,
	title: "",
	net: true,
	component: "",
	status: StatusEnum.Loading,
	steps: [],
	warn: "",
	render: true
};
const StateContext = createContext(void 0);
const DispatchContext = createContext(void 0);
function reducers(state, action) {
	switch (action.type) {
		case EActionTypes.UPDATE_INIT: return _objectSpread2(_objectSpread2({}, state), action.payload);
		case EActionTypes.UPDATE_NEXT_STEP:
			const currentStep = state.step + 1;
			if (state.step === state.steps.length - 1) {
				state.steps[state.steps.length - 1].status = "success";
				return _objectSpread2(_objectSpread2({}, state), {}, {
					status: "success",
					steps: [...state.steps]
				});
			}
			state.steps[state.step].status = "success";
			return _objectSpread2(_objectSpread2({}, state), {}, {
				warn: action.payload.warn || "",
				steps: [...state.steps],
				status: state.steps[currentStep].status,
				step: currentStep
			});
		case EActionTypes.UPDATE_KEY: return _objectSpread2(_objectSpread2({}, state), action.payload);
		case EActionTypes.UPDATE_LOADING: return _objectSpread2(_objectSpread2({}, state), action.payload);
		case EActionTypes.UPDATE_COMPONENT: return _objectSpread2(_objectSpread2(_objectSpread2({}, state), action.payload), {}, { form: state.component });
		case EActionTypes.UPDATE_STEPS: return _objectSpread2(_objectSpread2({}, state), action.payload);
		case EActionTypes.UPDATE_STEP_ITEM:
			state.steps[state.step] = _objectSpread2(_objectSpread2({}, state.steps[state.step]), action.payload);
			return _objectSpread2(_objectSpread2({}, state), {}, { steps: [...state.steps] });
		case EActionTypes.UPDATE_STEP_CURRENT_ITEM_CUSTOM:
			if (state.steps[state.step].custom) state.steps[state.step].custom = _objectSpread2(_objectSpread2({}, state.steps[state.step].custom), action.payload);
			else state.steps[state.step].custom = _objectSpread2({}, action.payload);
			return _objectSpread2(_objectSpread2({}, state), {}, { steps: [...state.steps] });
		case EActionTypes.UPDATE_STEP_KEY:
			state.steps[state.step].key = Date.now();
			return _objectSpread2(_objectSpread2({}, state), {}, { steps: [...state.steps] });
		case EActionTypes.UPDATE_DEVICE: return _objectSpread2(_objectSpread2({}, state), action.payload);
		case EActionTypes.UPDATE_CUSTOM: return _objectSpread2(_objectSpread2({}, state), {}, { custom: state.custom ? _objectSpread2(_objectSpread2({}, state.custom), action.payload) : _objectSpread2({}, action.payload) });
		case EActionTypes.BACKUP_PRODUCTION: return _objectSpread2(_objectSpread2({}, state), {}, { backup: cloneDeep(state) });
		case EActionTypes.BACKUP_FREE:
			if (!state.backup) return state;
			const _state$backup = state.backup;
			return _objectSpread2(_objectSpread2({}, _extends({}, (_objectDestructuringEmpty(_state$backup), _state$backup))), action.payload);
		case EActionTypes.UPDATE_CLIENT: return _objectSpread2(_objectSpread2({}, state), {}, { client: action.payload["client"] });
		default: throw new Error(`Unhandled action type: ${action.type}`);
	}
}
const Provider = ({ children, state: providerState }) => {
	const [state, dispatch] = useThunkReducer(reducers, providerState || initialState);
	return /* @__PURE__ */ React.createElement(StateContext.Provider, { value: state }, /* @__PURE__ */ React.createElement(DispatchContext.Provider, { value: dispatch }, children));
};
function useContextData() {
	const context = useContext(StateContext);
	if (context === void 0) throw new Error("useState must be used within a Provider");
	return context;
}
function useDispatch() {
	const context = useContext(DispatchContext);
	if (context === void 0) throw new Error("useDispatch must be used within a Provider");
	return context;
}
//#endregion
export { Provider, backUpFree, backUpProduction, updateClient, updateComponent, updateCustom, updateDevice, updateKey, updateNextStep, updateState, updateStatus, updateSteps, useContextData, useDispatch };
