/* eslint-disable react-refresh/only-export-components */
import { createContext, useContext, useMemo, type ReactNode } from "react";

import type { AuthState } from "../../api";

const AuthContext = createContext<{
  auth: AuthState;
  logout: () => void;
} | null>(null);

export function useAuth() {
  const context = useContext(AuthContext);
  if (!context) {
    throw new Error("useAuth must be used inside AuthContext");
  }
  return context;
}

export function AuthProvider({ auth, logout, children }: { auth: AuthState; logout: () => void; children: ReactNode }) {
  const value = useMemo(() => ({ auth, logout }), [auth, logout]);
  return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}
