import { isDevMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';
import { devTools } from '@ngneat/elf-devtools';

function setBootStatus(message: string): void {
  document.getElementById('boot-status')!.textContent = message;
}

setBootStatus('Starting frontend bundle...');

window.addEventListener('error', (event) => {
  setBootStatus(`Frontend error: ${event.message || 'unknown'}`);
});

window.addEventListener('unhandledrejection', (event) => {
  setBootStatus(`Frontend rejection: ${String(event.reason || 'unknown')}`);
});

const bootTimeout = window.setTimeout(() => {
  setBootStatus('Bootstrap still running...');
}, 3000);

bootstrapApplication(AppComponent, appConfig)
  .then(() => {
    window.clearTimeout(bootTimeout);
    document.getElementById('boot-status')?.remove();
  })
  .catch((err) => {
    window.clearTimeout(bootTimeout);
    console.error(err);
    setBootStatus(`Bootstrap failed: ${String(err)}`);
  });

if (isDevMode()) {
  devTools();
}
