import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '@msfa-environment';
import {
  Authorization,
  AuthorizationApiResponse,
  mapAuthorizationApiResponseToAuthorization,
} from '@msfa-models/authorization.model';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root',
})
export class AuthorizationService {
  private _apiBaseUrl = `${environment.api.url}/authorizations`;
  public authorizations$: Observable<Authorization[]> = this.httpClient
    .get<AuthorizationApiResponse>(this._apiBaseUrl)
    .pipe(map(({ data }) => data.map(authorization => mapAuthorizationApiResponseToAuthorization(authorization))));

  constructor(private httpClient: HttpClient) {}
}
