import { isPlatformBrowser } from '@angular/common';
import { Component, Inject, PLATFORM_ID } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { tap } from 'rxjs/operators';
import { ModalSessionExpiredComponent } from 'src/app/components/modal-session-expired/modal-session-expired.component';
import { UserStore } from 'src/app/stores/user.store';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {
  title = 'ESTAIR-CONNECT';
  static isBrowser = new BehaviorSubject<boolean>(false);

  constructor(
    @Inject(PLATFORM_ID) private platformId: any,
    private userStore: UserStore,
    private router: Router,
    public dialog: MatDialog
  ) {
    AppComponent.isBrowser.next(isPlatformBrowser(platformId));
    this.userStore
      .select('user')
      .pipe(
        tap((res: any) => {
         if (!res) this.router.navigate(['/login']);
          if (res.isExpired) {
            this.dialog.open(ModalSessionExpiredComponent, {
              disableClose: true,
            });
          }
        })
      )
      .subscribe();
  }
}
