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

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

  constructor(private http: HttpClient) { }

  getActivatorsByType(type: string, idUser: string, line: number){
    return this.getQuery(`get-activators-by-type/${type}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getActivator(idActivator: string, idUser: string, line: number){
    return this.getQuery(`get-activator/${idActivator}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  registerActivator(body: any){
    return this.postQuery("register-activator", body).pipe(map((data: any) => data));
  }

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

  postQuery(query: string, body: any){
    const JWT = `Bearer ${localStorage.getItem('token')}`;
    const URL = `${apiTemp}${query}`;
    return this.http.post(URL, body, {
      headers: new HttpHeaders({
        Authorization: JWT!,
      }),
    });
  }
}
