"use client";

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

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

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

export function useSidebarUserBootstrap({
  auth,
  userDispatch,
}: {
  auth: SidebarAuthBootstrapState;
  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,
  ]);
}
