import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";

import { useAvatarStore } from "src/Stores";
import useAutoSignIn from "src/avatar/useAutoSignIn";

const useSession = () => {
  const { loadTotem, totem } = useAvatarStore();
  const [isLoading, setLoading] = useState(true);

  const { t } = useTranslation();

  const autoSignIn = useAutoSignIn();

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

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

  return isLoading;
};

export default useSession;
