export class WidgetSearcher extends R.WidgetSearcher {
    events: Leaf.EventEmitter<"widget">
    constructor() {
        super()
        this.UI.input.focus()
    }
    public isInitPath(path) {
        this.events.emit("widget", path)
    }
    onChildAddWidgetList(child) {
        child.events.listenBy(this, "select", () => {
            this.select(child)
        })
    }
    onChildRemoveWidgetList(child) {
        child.events.stopListenBy(this)
    }
    onKeyupInput(e) {
        if (e.which === Leaf.Key.enter) {
            this.applyFirst()
        } else {
            this.displayUIByName(this.UI.input.value)
        }
    }
    displayUIByName(name) {
        if (!TestR) {
            return
        }
        this.widgetList.length = 0
        let matches = TestR.widgets.filter(item => item.toLowerCase().indexOf(name.toLowerCase()) >= 0).slice(0, 10)
        matches.forEach(name => this.widgetList.push(new WidgetListItem(name)))
        if (matches.length === 0) {
            this.VM.empty = true
        } else {
            this.VM.empty = false
        }
        this.VM.open = true
    }
    private applyFirst() {
        let first = this.widgetList[0] as WidgetListItem
        if (!first) return
        this.select(first)
    }
    private select(item: WidgetListItem) {
        console.log(item.path)
        this.VM.open = false
        this.events.emit("widget", item.path)
    }
}
export class WidgetListItem extends R.WidgetSearcher.WidgetListItem {
    constructor(public path: string) {
        super()
        this.VM.widgetPath = path
    }
    onClick() {
        console.error("select?")
        this.events.emit("select")
    }
}
