import React, { createContext, useState, useContext } from 'react';

const ErrorContext = createContext<{
  error: string | null;
  showError: (message: string, loginNeeded: boolean) => void;
  hideError: () => void;
  navigateToLogin: boolean;
}>({
  error: null,
  showError: (message: string, loginNeeded: boolean) => {},
  hideError: () => {},
  navigateToLogin: false
});

export const ErrorProvider = ({ children }: { children: React.ReactNode }) => {
  const [error, setError] = useState<string | null>(null);
  const [navigateToLogin, setNavigateToLogin] = useState<boolean>(false);

  const showError = (message: string, loginNeeded: boolean) => {
    setError(message);
    setNavigateToLogin(loginNeeded);
  };

  const hideError = () => {
    setError(null);
  };

  return (
    <ErrorContext.Provider value={{ error, showError, hideError, navigateToLogin }}>
      {children}
    </ErrorContext.Provider>
  );
};

export const useError = () => useContext(ErrorContext);
