import Logger from 'src/application/logging/logger';
import { useNewOrgConfig } from 'src/hooks/NewOrgConfig';
import { OrgUIConfigV3 } from '../typesV3';

const logger = new Logger('uiConfigContext');
interface UiConfigHookResult {
  getUiConfig: () => Promise<OrgUIConfigV3 | undefined>;
  isLoading: boolean;
}

export const useUiConfig = (): UiConfigHookResult => {
  const { frontendConfig, loading } = useNewOrgConfig();

  const getUiConfig = async (): Promise<OrgUIConfigV3 | undefined> => {
    logger.logDebug('uiConfigContext | retrieving ui config');

    if (loading) {
      logger.logDebug('uiConfigContext | frontend config still loading');
      throw new Error('Frontend config is loading');
    }

    const response = frontendConfig?.uiConfigs as OrgUIConfigV3 | undefined;
    logger.logDebug('uiConfigContext | returning ui config response', response);

    return response;
  };

  return {
    getUiConfig,
    isLoading: loading,
  };
};
