import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useAvatarStore } from "src/Stores";

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

  const { t } = useTranslation();

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

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

  return isLoading;
};

export default useSession;
