export class TestSearcher extends R.TestSearcher {
    events: Leaf.EventEmitter<"test">
    searchResultList: Leaf.List<SearchResultItem>
    constructor() {
        super()
        this.UI.searchInput.focus()
    }
    autoComplete(query: string = "") {
        let tests = LeafTestContext.scripts.filter(item => item.scriptPath.indexOf("test.js") >= 0).map(item => item.scriptPath)
        tests = tests.filter(item => item.indexOf(query) >= 0)
        tests = tests.slice(0, 5)
        this.searchResultList.length = 0
        for (let path of tests) {
            this.searchResultList.push(new SearchResultItem(path))
        }
        this.VM.empty = tests.length === 0
    }
    onChildAddSearchResultList(child: SearchResultItem) {
        child.events.listenBy(this, "select", () => {
            this.select(child.path)
        })
    }
    select(path: string) {
        this.events.emit("test", path)
        this.searchResultList.length = 0
    }
    applyDefault() {
        if (this.UI.searchInput.value === "all") {
            this.select("all")
            return
        }
        if (this.searchResultList.length === 0) return
        let item = this.searchResultList[0]
        this.select(item.path)
    }
    onKeyupSearchInput(e) {
        this.autoComplete(this.UI.searchInput.value)
        if (e.which === Leaf.Key.enter) {
            this.applyDefault()
        }
    }
}
class SearchResultItem extends R.TestSearcher.SearchResultListItem {
    constructor(public path: string) {
        super()
        this.VM.path = path
    }
    onClick() {
        this.events.emit("select")
    }
}
