import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Candidate } from '../models/candidate';

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

  constructor(private http: HttpClient) { }

  listOfCandidates() {
    return this.http.get<any>('assets/demo/data/candidates.json')
            .toPromise()
            .then(res => res.data as Candidate[])
            .then(data => data);
  }
  
  candidatGetById(id: string): Observable<Candidate> {
     return null;
  }
  async Search(query: string): Promise<Candidate[]> {
    let candidates = this.listOfCandidates();
    let candidatesFilter: Candidate[] ;
    for (let item of await candidates) 
    {
      if (item.firstName == query || item.lastName == query) 
      {
        candidatesFilter.push(item);
      }
    }  
    return candidatesFilter
  }
}
