import { Component, OnInit } from '@angular/core';
import { AuthServiceService } from '../services/auth-service.service';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {

  languages = {
    en: 'EN',
    no: 'NO',
    sm: 'SM'
  }

  preferredLanguage = 'en';
  constructor(public translate:TranslateService, private authService:AuthServiceService) {
    translate.addLangs(['en', 'no','sm']);
    translate.setDefaultLang('en');

    const browserLang = translate.getBrowserLang();
    translate.use(browserLang.match(/en|no|sm/) ? browserLang : 'en');
    if (navigator.language.includes('en')) {
      translate.use('en');
    } else {
      translate.use('no');
    }

    const language = localStorage.getItem('language')
    if(language){
      translate.use(language);
      this.preferredLanguage = language;
    }
  }

  ngOnInit(): void {
  }

  logout(){
    this.authService.logOut();
  }

  changeLanguage(language) {
    this.translate.use(language);
    localStorage.setItem('language', language);
  }

}
