import { apiUriQA, apiTemp } from './../../environments/environment';
import { map } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';

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

  constructor(private http: HttpClient) {}

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

  getModule(module: string, id: string, linea: number){
    return this.getQuery(`get-module/${module}/${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));
  }

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

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

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

  getMenus(module: string, submodule: string){
    return this.getQuery(`get-menus/${module}/${submodule}`).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));
  }

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