import { Component, Inject, PLATFORM_ID } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { StorageService } from './shared/services/storage.service';
import { isPlatformBrowser } from '@angular/common';
import { directions, supportedLanguages } from './utils/enums';
import { LoaderComponent } from './shared/components/loader/loader.component';

@Component({
  selector: 'app-root',
  imports: [RouterOutlet, LoaderComponent],
  templateUrl: './app.component.html',
  styleUrl: './app.component.scss'
})
export class AppComponent {
  direction: any;
  title = 'ACONNECT-UX';

  constructor(private translateService: TranslateService, private storageService: StorageService,
    @Inject(PLATFORM_ID) private platformId: object
  ) { }

  ngOnInit() {
    if (this.storageService.getItem('language')) {
      const currentLanguage = this.storageService.getItem('language')!;
      if (isPlatformBrowser(this.platformId)) {
        this.translateService.setDefaultLang(currentLanguage);
        this.translateService.use(currentLanguage);
      }

      this.direction = this.storageService.getItem('direction');
    }
    else {
      if (isPlatformBrowser(this.platformId)) {
        this.translateService.setDefaultLang('English');
        this.translateService.use('English');
      }

      this.storageService.setItem('language', supportedLanguages.ENGLISH);
      this.direction = directions.LTR;
      this.storageService.setItem('direction', this.direction);
    }
  }
}
