import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { DeltagareHandelse, DeltagareHandelseApiResponse } from '@msfa-models/deltagare-handelse.model';
import { environment } from '@msfa-environment';

@Injectable({
  providedIn: 'root',
})
export class DeltagareHandelserApiService {
  private _apiBaseUrl = `${environment.api.url}/deltagareHandelser`;
  constructor(private httpClient: HttpClient) {}

  fetchDeltagareHandelser(genomforandereferens: string): Observable<DeltagareHandelse[]> {
    return this.httpClient
      .get<DeltagareHandelseApiResponse>(`${this._apiBaseUrl}?genomforandereferens=${genomforandereferens}`)
      .pipe(
        map(({ data }) => {
          if (data) {
            return data;
          }
        })
      );
  }
}
