import { HttpClient, HttpParams } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { environment } from "src/environments/environment";

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

    constructor(private http: HttpClient) {
    }

    fetch(path: string, limit: number, offset: number, sort: string, descending: boolean, filter: any | undefined) {
        let httpParams = new HttpParams();
        if (limit != undefined) {
            httpParams = httpParams.set("limit", "" + limit);
        }
        if (offset) {
            httpParams = httpParams.set("offset", "" + offset);
        }

        if (sort) {
            httpParams = httpParams.set("sort", "" + sort);
        }

        if (descending) {
            httpParams = httpParams.set("descending", "" + descending);
        }

        if (filter) {
            for (const param in filter) {
                if (filter[param]) {
                    httpParams = httpParams.set(param, "" + filter[param]);
                }
            }
        }

        return this.http.get(environment.apiUrl + path, { params: httpParams });
    }
}