import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { switchMap, tap } from 'rxjs/operators';
import { TreeNode } from 'primeng/api';
import { BehaviorSubject, Observable } from 'rxjs';
import { Candidate } from 'src/app/shared/models/candidate';

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

    private _candidats = new BehaviorSubject<Candidate[]>([]);
    private dataStore: { candidats: Candidate[] } = { candidats: [] };
    readonly candidats = this._candidats.asObservable();

    constructor(private http: HttpClient) {}

    loadAll() {
        this.http.get<Candidate[]>('assets/demo/data/candidates.json').subscribe(
          data => {
            this.dataStore.candidats = data;
            this._candidats.next(Object.assign({}, this.dataStore).candidats);
          },
          error => console.log('Could not load Candidats.')
        );
      }
    getCandidats(): Observable<Candidate[]>{
     this.loadAll();
     return this.candidats
    }
}
