import { Injectable } from '@angular/core';
import { EventPerson } from '../models/eventperson.model';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { AuthService } from '../services/auth.service';
import { LoginUserRespose } from '../models/user.model';

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

  authheader = {};
  loggeduser: LoginUserRespose;
  headers: HttpHeaders;

  constructor(private http: HttpClient, user: AuthService) { 
    this.loggeduser = user.ValidatedUser;
    this.headers = new HttpHeaders();
    this.headers = this.headers.set('Authorization', this.loggeduser.BasicAuthKey);
  }

  getContacts(namefilter:string): Observable<EventPerson[]>{
    const geturl = environment.apiURL + 'contacts/get';
    let params = new HttpParams();

    params = params.set("namefilter", namefilter);
    params = params.set("domain", environment.domain);
 
    let options = { 
      headers: this.headers, 
      params: params 
    };

    return this.http.get<EventPerson[]>(`${geturl}`, options);
  }
}
