"use client";

import { type Dispatch, useEffect } from "react";

import type { LoginStatus } from "@/data/schemas/auth";

interface ProfileAuthBootstrapState {
  hasInitialized: boolean;
  isLoading: boolean;
  loginStatus: LoginStatus;
}

export function useProfileUserBootstrap({
  auth,
  userDispatch,
}: {
  auth: ProfileAuthBootstrapState;
  userDispatch: Dispatch<{ type: "UserFetch" }>;
}) {
  useEffect(() => {
    if (!auth.hasInitialized || auth.isLoading) return;
    if (auth.loginStatus === "notLoggedIn") return;

    userDispatch({ type: "UserFetch" });
  }, [
    auth.hasInitialized,
    auth.isLoading,
    auth.loginStatus,
    userDispatch,
  ]);
}
