import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ErrorType } from '@msfa-enums/error-type.enum';
import { environment } from '@msfa-environment';
import { SignalRequest } from '@msfa-models/api/signal.request.model';
import { CustomError } from '@msfa-models/error/custom-error';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';

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

  public postSignal$(requestData: SignalRequest): Observable<void> {
    return this.httpClient.post<void>(this._apiBaseUrl, requestData).pipe(
      catchError((error: Error) => {
        throw new CustomError({
          error,
          message: `Kunde inte spara Signal om arbete eller studier.\n\n${error.message}`,
          type: ErrorType.API,
        });
      })
    );
  }

  constructor(private httpClient: HttpClient) {}
}
