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

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

  constructor(private _http: HttpClient) { }

  getNotification(idNotification: string, setRead: string, idUser: string, line: number){
    return this.getQuery(`get-notification/${idNotification}/${setRead}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getNotificationsByUser(idUser: string, line: number){
    return this.getQuery(`get-notifications-by-user/${idUser}/${line}`).pipe(map((data: any) => data));
  }
  
  private getQuery(query: string, tk?: string){
    const URL = `${apiTemp}${query}`;
    let token = "";
    if(tk == undefined || tk == null || tk == ""){
      token = `Bearer ${localStorage.getItem('token')!}`;
    }else{
      token = `Bearer ${tk!}`;
    }
    return this._http.get(URL, {
      headers: new HttpHeaders({
        Authorization: token,
      })
    })
  }

  private 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!
      })
    });
  }
}
