import { useGlobal } from 'hooks/useGlobal';

export type ServerEnvironment = 'dev' | 'alpha' | 'edge' | 'production';

function detectEnvironment(): ServerEnvironment {
  const host = window.location.host;
  if (host === 'officer.dev' || host === 'www.officer.dev') return 'production';
  if (host.startsWith('edge.')) return 'edge';
  if (host.startsWith('alpha.')) return 'alpha';
  return 'dev';
}

const env = detectEnvironment();

if (env !== 'production') {
  const prefix = env.toUpperCase();
  if (!document.title.startsWith(prefix)) {
    document.title = `${prefix} | ${document.title}`;
  }
}

export const useServerEnvironment = () => {
  const [environment] = useGlobal<ServerEnvironment>('SERVER_ENVIRONMENT', env);
  return environment;
};
