//// @deno-types="https://deno.land/x/fuse@v6.4.1/dist/fuse.d.ts"
import Fuse from 'https://deno.land/x/fuse@v6.4.1/dist/fuse.esm.min.js';
import { Doc } from "./collect.ts";

export class Index{
    private index: Fuse;

    private constructor(index: Fuse){
        this.index = index; 
    }

    public setDocs(docs: Doc[]){
        this.index.setCollection(docs);
    }

    public search(query: string): Doc[]{
        return this.index.search(query, {limit: 10}).map((
            result) => 
                result.item as Doc);
    }

    public static createIndex(docs: Doc[]){
        const index = new Fuse(docs,{
            keys: ["attributes.title", "attributes.japanese_title", "attributes.tags", "attributes.rjcode", "attributes.author", "path"],
            includeScore: true,
            includeMatches: true,
        })

        return new Index(index);
    }
}
