import { Directive, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { ActivatedRoute, NavigationEnd, NavigationStart, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { filter, startWith, switchMap, tap } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
import { AppTokenService } from '../service/security/app-token.service';
@Directive()
export class BaseListRole {
    isProduction = environment.production
    @ViewChild(MatSort,{ static: true }) sort: MatSort;
    @ViewChild(MatPaginator,{ static: true }) paginator: MatPaginator;
    role;
    protected updateMatTable(res: any){
        res = new MatTableDataSource(res)
        res.paginator = this.paginator
        res.sort = this.sort
        return res

    }

    constructor(
      public router: Router,
      public activeRoute: ActivatedRoute,
      public apptokenSV: AppTokenService
    ){
      this.router.events.pipe(
        filter(r => r instanceof NavigationEnd),
        startWith(this.router),
        // tap(x => console.log(this.activeRoute.firstchild.data)),
        switchMap(r => this.activeRoute.data),
        // tap(x => console.log(x)),
        tap(x => this.role = this.apptokenSV.getPerm(x.role))
      ).subscribe()
    }

}
