import { Component, ElementRef, Injectable, Input, Pipe, PipeTransform, Renderer2, ViewChild } from '@angular/core';

import { ControlValueAccessor, FormControl, NG_VALIDATORS, NG_VALUE_ACCESSOR, ValidationErrors } from '@angular/forms';

import { CurrencyService, IAnalysisTag, ICurrency } from '../shared';

import { Observable } from 'rxjs/Observable';

@Pipe({
    name: 'knkFilter'
})
@Injectable()
export class FilterPipe implements PipeTransform {
    transform(items: IAnalysisTag[], value: string): IAnalysisTag[] {
      if (!items) {
          return [];
      }
      const propertyName: string = 'name';
      if (!value || value.length === 0) {
          return items;
      }
      return items.filter(it =>
        it[propertyName].toString().toLowerCase().indexOf(value.toLowerCase()) !== -1);
    }
}

@Pipe({
    name: 'knkCurrencyFilter'
})
@Injectable()
export class CurrencyFilterPipe implements PipeTransform {
    constructor(private currencyService: CurrencyService) {
    }
    transform(items: ICurrency[], value: string): Observable<ICurrency[]> {
    //   if (!items) {
    //       return [];
    //   }
      const propertyName: string = 'name';
    //   if (!value || value.length === 0) {
    //       return items;
    //   }
        return this.currencyService.getCurrencies(value).
            map((currencies) => {
                return currencies.items;
            // return items.filter(it =>
            //     it[propertyName].toString().toLowerCase().indexOf(value.toLowerCase()) !== -1);
        });
    }
}
