import useAutoLogIn from "@/query/useAutoLogIn";

import { useTaehuiStore } from "@/state/Stores";
import { useEffect, useState } from "react";

export default function useSession() {
  const { loadTotem, totem } = useTaehuiStore();
  const [isLoading, setLoading] = useState(true);

  const autoLogIn = useAutoLogIn();

  useEffect(() => {
    (async () => {
      await loadTotem();
      setLoading(false);
    })();
  }, [loadTotem]);

  useEffect(() => {
    (async () => {
      if (!isLoading && !totem) {
        await autoLogIn();
      }
    })();
  }, [autoLogIn, isLoading, totem]);

  return isLoading;
}
