import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Params } from '@msfa-models/api/params.model';
import { DeltagareAvrop } from '@msfa-models/avrop.model';
import { AvvikelseReport } from '@msfa-models/avvikelse.model';
import { Observable } from 'rxjs';
import { map, shareReplay, switchMap } from 'rxjs/operators';
import { AvvikelseReportViewService } from './avvikelse-report-view.service';

@Component({
  selector: 'msfa-avvikelse-report-view',
  templateUrl: './avvikelse-report-view.component.html',
  styleUrls: ['./avvikelse-report-view.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AvvikelseReportViewComponent {
  params$: Observable<Params> = this.activatedRoute.params.pipe(
    map(params => ({
      handlingId: params.handlingId as string,
      genomforandeReferens: params.genomforandeReferens as string,
    }))
  );
  avrop$: Observable<DeltagareAvrop> = this.params$.pipe(
    switchMap(({ genomforandeReferens }) =>
      this.avvikelseReportViewService.fetchAvropInformation$(+genomforandeReferens)
    ),
    shareReplay(1)
  );
  avvikelseReport$: Observable<AvvikelseReport> = this.params$.pipe(
    switchMap(({ handlingId }) => this.avvikelseReportViewService.fetchAvvikelseReport$(handlingId as string)),
    shareReplay(1)
  );

  constructor(private avvikelseReportViewService: AvvikelseReportViewService, private activatedRoute: ActivatedRoute) {}
}
