import {useMemo} from 'react';
import { useCommon } from '@common/contexts/useCommon.ts';
import ENV from '@common/env/env.ts';

function useEnv() {
  const {
    state: {info},
  } = useCommon();

  const env = useMemo(() => {
    if (ENV.filter(e => e.env === 'production').length === 0) {
      throw new Error('"production" environment must be specified!');
    }

    const envs = ENV.filter(e => e.env === info.env);
    if (envs.length > 0) {
      return envs[0];
    }
    return ENV.filter(e => e.env === 'production')[0];
  }, [info.env]);

  return env;
}

export {useEnv};
