import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { KeyValue } from '../models/key-value';

@Injectable({ providedIn: 'root' })
export class FilterService {
  private _values: KeyValue[] = [];
  private set filters(value: KeyValue[]) {
    this._values = value;
    this.filterSubject.next(this._values);
  }
  private get filters(): KeyValue[] {
    return this._values;
  }
  private filterSubject = new Subject<KeyValue[]>();
  readonly filters$ = this.filterSubject.asObservable();

  constructor() {}

  set(key: string, value: any): void {
    const update = this.filters.filter((f: KeyValue) => f.key !== key);
    if (!!value) update.push(new KeyValue({ key: key, value: value }));
    this.filters = update;
  }
}
