import { useState, useEffect } from 'react';
import { ICoreInitApiArgs, init, IUserProfile } from '@livelike/javascript';

export function useInit(initArg: ICoreInitApiArgs): {
  profile: IUserProfile;
  loaded: boolean;
} {
  const [initResult, setInitResult] = useState({
    profile: null,
    loaded: false,
  });

  // Load any resources or data that we need prior to rendering the app
  useEffect(() => {
    async function initLLSdk() {
      try {
        // SplashScreen.preventAutoHideAsync();

        await init(initArg).then((profile) => {
          setInitResult({ profile, loaded: true });
        });
      } catch (e) {
        // We might want to provide this error information to an error reporting service
        console.warn(e);
        setInitResult({ profile: null, loaded: true });
      }
    }

    initLLSdk();
  }, []);

  return initResult;
}
