import { createContext, useContext, useState } from "react";
import type { QueryStatus } from "./SignOrder";

interface ActionStatusContextType {
	status: QueryStatus;
	setStatus: React.Dispatch<React.SetStateAction<QueryStatus>>;
}

const ActionStatusContext = createContext<ActionStatusContextType | undefined>(
	undefined,
);

export const ActionStatusProvider: React.FC<{ children: React.ReactNode }> = ({
	children,
}) => {
	const [status, setStatus] = useState<QueryStatus>("idle");

	return (
		<ActionStatusContext.Provider value={{ status, setStatus }}>
			{children}
		</ActionStatusContext.Provider>
	);
};

// Custom hook to use the ActionStatusContext
export const useActionStatusContext = () => {
	const context = useContext(ActionStatusContext);
	if (!context) {
		throw new Error(
			"useActionStatusContext must be used within an ActionStatusProvider",
		);
	}
	return context;
};
