import { Injectable } from '@angular/core';
import { AvvikelseReportRequest } from '@msfa-models/api/avvikelse-request.model';
import { DeltagareAvrop } from '@msfa-models/avrop.model';
import { AvvikelseQuestion } from '@msfa-models/avvikelse-question.model';
import { AvvikelseReason, sortAvvikelseReasons } from '@msfa-models/avvikelse-reason.model';
import { AvvikelseApiService } from '@msfa-services/api/avvikelse.api.service';
import { DeltagareApiService } from '@msfa-services/api/deltagare.api.service';
import { Observable } from 'rxjs';
import { map, shareReplay } from 'rxjs/operators';

@Injectable()
export class AvvikelseReportFormService {
  avvikelseQuestions$: Observable<AvvikelseQuestion[]> = this.avvikelseApiService
    .fetchAvvikelseQuestions$()
    .pipe(shareReplay(1));

  fetchAvvikelseReasons$: Observable<AvvikelseReason[]> = this.avvikelseApiService.fetchAvvikelseReasons$().pipe(
    map(reasons => sortAvvikelseReasons(reasons)),
    shareReplay(1)
  );

  constructor(private avvikelseApiService: AvvikelseApiService, private deltagareApiService: DeltagareApiService) {}

  createAvvikelse$(avvikelse: AvvikelseReportRequest): Observable<unknown> {
    return this.avvikelseApiService.createAvvikelse$(avvikelse);
  }

  fetchAvropInformation$(genomforandeReferens: number): Observable<DeltagareAvrop> {
    return this.deltagareApiService.fetchAvropInformation$(genomforandeReferens);
  }
}
