import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Feature } from '@msfa-enums/feature.enum';
import { environment } from '@msfa-environment';
import { News } from '@msfa-models/news.model';
import { Role } from '@msfa-models/role.model';
import { UserService } from '@msfa-services/api/user.service';
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { StartService } from './start.service';

@Component({
  selector: 'msfa-start',
  templateUrl: './start.component.html',
  styleUrls: ['./start.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StartComponent {
  private _activeFeatures: Feature[] = environment.activeFeatures;
  userFullName$: Observable<string> = this.userService.user$.pipe(
    filter(user => !!user),
    map(userName => userName.fullName)
  );
  userRoles$: Observable<Role[]> = this.userService.userRoles$;
  news$: Observable<News> = this.startService.fetchNews$();
  newsLoading$: Observable<boolean> = this.startService.newsLoading$;

  constructor(private userService: UserService, private startService: StartService) {}

  isAuthorizedUser(userRoles: Role[]): boolean {
    return !!userRoles.length;
  }

  get newsVisible(): boolean {
    return this._activeFeatures.includes(Feature.NEWS);
  }
}
