import { useCallback, useRef, useState } from "react";
//#region src/components/eftposPay/store/useThunkReducer.ts
function useThunkReducer(reducer, initialArg, init) {
	const [hookState, setHookState] = useState(init ? init(initialArg) : initialArg);
	const state = useRef(hookState);
	const getState = useCallback(() => state.current, [state]);
	const setState = useCallback((newState) => {
		state.current = newState;
		setHookState(newState);
	}, [state, setHookState]);
	const reduce = useCallback((action) => reducer(getState(), action), [reducer, getState]);
	const dispatch = useCallback((action) => {
		if (typeof action === "function") return action(dispatch, getState);
		setState(reduce(action));
	}, [
		getState,
		setState,
		reduce
	]);
	return [hookState, dispatch];
}
//#endregion
export { useThunkReducer as default };
