import { createContext, useContext, useMemo, useState } from "react";
import { STORAGE_KEYS } from "../lib/storage";
const AuthContext = createContext(null);
export function AuthProvider({ children }) {
  const [token, setToken] = useState(() => localStorage.getItem(STORAGE_KEYS.token));
  const value = useMemo(
    () => ({
      token,
      login(nextToken) {
        localStorage.setItem(STORAGE_KEYS.token, nextToken);
        setToken(nextToken);
      },
      logout() {
        localStorage.removeItem(STORAGE_KEYS.token);
        setToken(null);
      }
    }),
    [token]
  );
  return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}
export function useAuth() {
  return useContext(AuthContext);
}
