import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { map, Observable } from 'rxjs';
import { ModulesInterface } from '../interfaces/modules.interface';
import { apiTemp } from '../../environments/environment.prod';

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

  constructor(private _http: HttpClient) {}

  getModules(id: string, linea: number): Observable<ModulesInterface>{
    return this.getQuery(`get-modules/${id}/${linea}`).pipe(map((data: any) => data));
  }

  getSubmodules(module: string, id: string, linea: number){
    return this.getQuery(`get-submodules/${module}/${id}/${linea}`).pipe(map((data: any) => data));
  }

  getMenu(id: string, linea: number){
    return this.getQuery(`get-menu/${id}/${linea}`).pipe(map((data: any) => data));
  }

  getModuleFunctions(idMod: string, id: string, linea: number){
    return this.getQuery(`get-module-functions/${idMod}/${id}/${linea}`).pipe(map((data: any) => data));
  }

  getSubmoduleFunctions(idMod: string, idSub: string, idUser: string, linea: number){
    return this.getQuery(`get-submodule-functions/${idMod}/${idSub}/${idUser}/${linea}`).pipe(map((data: any) => data));
  }

  getQuery(query: string){
    const URL = `${apiTemp}${query}`;
    const TOKEN = localStorage.getItem('token');
    
    return this._http.get(URL, {
      headers: new HttpHeaders({
        Authorization: "Bearer " + TOKEN!
      })
    });
  }
}
