import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '@msfa-environment';
import { ActivityResponse } from '@msfa-models/api/activity-response.model';
import { Activity, mapResponseToActivity } from '@msfa-models/activity.model';
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})

export class ActivityApiService {

  private _apiBaseUrl = `${environment.api.url}`;

  public getActivities$(): Observable<Activity[]> {  // endpoint ska uppdateras
    return this.httpClient.get<{ data: ActivityResponse[] }>(`${this._apiBaseUrl}/aktiviteter`).pipe(
      filter(response => !!response?.data),
      map(({ data }) => data.map(aktivitet => mapResponseToActivity(aktivitet)))
    )
  }

  constructor(private httpClient: HttpClient) { }

}
