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 { InformativRapport } from '@msfa-models/informativ-rapport.model';
import { Observable } from 'rxjs';
import { map, shareReplay, switchMap } from 'rxjs/operators';
import { InformativRapportViewService } from './informativ-rapport-view.service';

@Component({
  selector: 'msfa-informativ-rapport-view',
  templateUrl: './informativ-rapport-view.component.html',
  styleUrls: ['./informativ-rapport-view.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class InformativRapportViewComponent {
  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.informativRapportViewService.fetchAvropInformation$(+genomforandeReferens)
    ),
    shareReplay(1)
  );
  informativRapport$: Observable<InformativRapport> = this.params$.pipe(
    switchMap(({ handlingId }) => this.informativRapportViewService.fetchInformativRapport$(handlingId as string)),
    shareReplay(1)
  );

  constructor(
    private informativRapportViewService: InformativRapportViewService,
    private activatedRoute: ActivatedRoute
  ) {}
}
