import { SearchService } from "./SearchService";
// import * as Fuse from 'fuse.js';
import Fuse = require('fuse.js');

export class FuseSearchService<T> implements SearchService<T> {
    private readonly _fuse: Fuse<T, Object>;
    private readonly _params: Fuse.FuseOptions<T>;

    constructor(keys: string[], params: Fuse.FuseOptions<T> = {
        shouldSort: true,
        threshold: 0.01,
        location: 0,
        distance: 100,
        maxPatternLength: 32,
        minMatchCharLength: 1,
        // keys: [
        //     "userName",
        // ]
    }) {
        this._params = params;
        this._params.keys = keys;
        this._fuse = new Fuse([], this._params);
    }

    search(list: T[], searchTerm: string): T[] {
        // const fuse = new Fuse<T, Fuse.FuseOptions<any>>(list, this._params);
        this._fuse.setCollection(list);
        // fuse.search(searchTerm);
        return this._fuse.search(searchTerm);
    }

    
    /**
     *
     *
     * @static
     * @param keys
     * The keys that must be searched for in the given list
     * @template G
     * @returns {SearchService<G>}
     * @memberof FuseSearchService
     */
    static getInstance<G>(keys: string[]): SearchService<G> {
        return new FuseSearchService<G>(keys);
    }


}