import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Observable} from "rxjs";
import {ServiceResponse} from "../models/ServiceResponse";
import {map, tap} from "rxjs/operators";
import {Permission} from "../models/Permission";
import {environment} from "../../environments/environment";

@Injectable({
  providedIn: 'root'
})
export class PermissionService {

  constructor(private http: HttpClient) { }

  public getAll(): Observable<Permission[]> {
    return this.http.get<ServiceResponse<any>>(environment.baseUrl + "/lecservice/permissions/admin/list")
        .pipe(
            tap(
                (serviceResponse: ServiceResponse<Permission[]>) => {
                  if (!serviceResponse.success)
                    throw new Error(serviceResponse.messages?.join(", "));
                }),
            map((serviceResponse: ServiceResponse<Permission[]>) => {
              return serviceResponse.success ? serviceResponse.result: [];
            })
        );
  }
}
