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

const AuthContext = createContext({
  isAuthenticated: false,
  login: (_token: string) => {},
  logout: () => {}
});

export const AuthProvider= ({ children }: { children: React.ReactNode }) => {
  const [isAuthenticated, setIsAuthenticated] = useState(!!localStorage.getItem('token'));

  const login = (token: string) => {
    localStorage.setItem('token', token)
    setIsAuthenticated(true);
  }
  const logout = () => {
    localStorage.removeItem('token');
    setIsAuthenticated(false);
  }
  return (
    <AuthContext.Provider value={{ isAuthenticated, login, logout }}>
      {children}
    </AuthContext.Provider>
  );
};

export const useAuth = () => useContext(AuthContext);