/* eslint-disable no-useless-escape */
/*
 * Copyright (C) 2019 Robert Bosch GmbH Copyright (C) 2019 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { of } from 'rxjs';
import { debounceTime, distinctUntilChanged, map, startWith } from 'rxjs/operators';
// import { BOSCH_ICONS } from '@bci-web-core/common-styles/fonts/bosch-ic';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const BOSCH_ICONS_JSON = require('@bci-web-core/common-styles/fonts/bosch-ic-names.json') as any;

@Component({
  selector: 'bci-bosch-icons',
  templateUrl: './bosch-icons.component.html',
  styleUrls: ['./bosch-icons.component.scss'],
})
export class BoschIconsComponent implements OnInit {
  icons: string[] = this.parseIconJson(BOSCH_ICONS_JSON);
  filteredIcons = of(this.icons);

  searchForm: FormGroup;

  constructor(private fb: FormBuilder) {
    this.searchForm = this.fb.group({
      search: '',
    });
  }

  ngOnInit() {
    this.filteredIcons = this.searchForm.controls.search.valueChanges.pipe(
      debounceTime(300),
      startWith(''),
      distinctUntilChanged(),
      map((term) => this.icons.filter((icon) => icon.includes(term)))
    );
  }

  private parseIconJson(json: any) {
    return Object.keys(json)
      .filter((icon) => !icon.startsWith('_'))
  }
}
