/* Copyright (C) Click & Push Accessibility, Inc - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * Written and maintained by the Click & Push Development team 
 * <dev@clicknpush.ca>, January 2022
 */

import { observer } from 'mobx-react';
import React from 'react';
import { QueryClient } from 'react-query';
import { Loading } from './Loading';
import { useAuth } from '../state/external/auth-provider';
import BaseStackNavigator from '../navigation/base-stack-navigator';
import { Error } from './Error';

import { navigationRef } from '../navigation/root-navigator';
import { usePermissions } from '../state/external/PermissionsContext';

export enum TokenState {
  CheckingToken,
  ValidToken,
  InvalidToken
}

const queryClient = new QueryClient();

/**
 * Sub-root component of the app. Contains all global providers (NavigationContainer and SafeAreaProvider for React Navigation, QueryClientProvider for react-query) and is responsible for restricting unauthenticated users to the Intro screen by listening to {@link AuthStore}'s accessToken value.
 * @component
 */
const Atlas : React.FC = () => {
  const {authStateLoading} = useAuth()
  const {permissionsLoading} = usePermissions()

  // useEffect(() => {
  //   let isMounted = true
  //   const checkAuthStateOnAccessTokenUpdate = async () => {
  //     let accessToken = authStore.accessToken
  //     if (!accessToken) {
  //       accessToken = await getItemAsync(SECURESTORE_ACCESSTOKEN)
  //     }

  //     if (accessToken)
  //       await checkAuthenticationState({})
  //   }
    
  //   if (isMounted)
  //     checkAuthStateOnAccessTokenUpdate()

  //   return () => { isMounted = false }
  // }, [])

  return (
    <>
      {authStateLoading || permissionsLoading ? <Loading/> : <BaseStackNavigator />}
    </>
  );
}

export default observer(Atlas);