import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '@msfa-environment';
import { PeriodiskRedovisningRequest } from '@msfa-models/api/periodisk-redovisning.request.model';
import { CustomError } from '@msfa-models/error/custom-error';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable({
  providedIn: 'root',
})
export class PeriodiskRedovisningApiService {
  private _apiBaseUrl = `${environment.api.url}/rapporter/periodisk-redovisning`;

  constructor(private httpClient: HttpClient) {}

  public postPeriodiskRedovisning$(data: PeriodiskRedovisningRequest): Observable<void> {
    return this.httpClient.post<void>(this._apiBaseUrl, data).pipe(
      catchError((error: Error) => {
        throw new CustomError({
          error,
          message: `Kunde inte spara Periodisk redovisning.\n\n${error.message}`,
          name: `POST ${this._apiBaseUrl}`,
          data,
          method: 'PeriodiskRedovisningApiService.postPeriodiskRedovisning$',
        });
      })
    );
  }
}
