import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { TranslateService } from 'ng2-translate/ng2-translate';
import * as _ from 'lodash';

import { Language } from '../models';

@Component({
  selector: 'yt-nav',
  templateUrl: 'nav.component.html',
  styleUrls: [ 'nav.component.scss' ]
})
export class NavComponent implements OnInit {

  siteLangs: Language[];

  constructor(
    public authService: AuthService,
    private translateService: TranslateService,
    private route: ActivatedRoute
  ) { }

  ngOnInit() {
    this.route.data.forEach((data: { siteLanguages: Language[] }) => {
      this.siteLangs = data.siteLanguages;
    });
  }

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

  changeLanguage(code: string) {
    // TODO: do a better global check for site languages here
    if (_.chain(this.siteLangs).map('code').includes(code).value()) {
      localStorage.setItem('siteLang', code);
      this.translateService.use(code);
    }
  }
}
import { AuthService } from '../auth.service';