"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var WidgetSearcher = (function (_super) {
    __extends(WidgetSearcher, _super);
    function WidgetSearcher() {
        var _this = _super.call(this) || this;
        _this.UI.input.focus();
        return _this;
    }
    WidgetSearcher.prototype.isInitPath = function (path) {
        this.events.emit("widget", path);
    };
    WidgetSearcher.prototype.onChildAddWidgetList = function (child) {
        var _this = this;
        child.events.listenBy(this, "select", function () {
            _this.select(child);
        });
    };
    WidgetSearcher.prototype.onChildRemoveWidgetList = function (child) {
        child.events.stopListenBy(this);
    };
    WidgetSearcher.prototype.onKeyupInput = function (e) {
        if (e.which === Leaf.Key.enter) {
            this.applyFirst();
        }
        else {
            this.displayUIByName(this.UI.input.value);
        }
    };
    WidgetSearcher.prototype.displayUIByName = function (name) {
        var _this = this;
        if (!TestR) {
            return;
        }
        this.widgetList.length = 0;
        var matches = TestR.widgets.filter(function (item) { return item.toLowerCase().indexOf(name.toLowerCase()) >= 0; }).slice(0, 10);
        matches.forEach(function (name) { return _this.widgetList.push(new WidgetListItem(name)); });
        if (matches.length === 0) {
            this.VM.empty = true;
        }
        else {
            this.VM.empty = false;
        }
        this.VM.open = true;
    };
    WidgetSearcher.prototype.applyFirst = function () {
        var first = this.widgetList[0];
        if (!first)
            return;
        this.select(first);
    };
    WidgetSearcher.prototype.select = function (item) {
        console.log(item.path);
        this.VM.open = false;
        this.events.emit("widget", item.path);
    };
    return WidgetSearcher;
}(R.WidgetSearcher));
exports.WidgetSearcher = WidgetSearcher;
var WidgetListItem = (function (_super) {
    __extends(WidgetListItem, _super);
    function WidgetListItem(path) {
        var _this = _super.call(this) || this;
        _this.path = path;
        _this.VM.widgetPath = path;
        return _this;
    }
    WidgetListItem.prototype.onClick = function () {
        console.error("select?");
        this.events.emit("select");
    };
    return WidgetListItem;
}(R.WidgetSearcher.WidgetListItem));
exports.WidgetListItem = WidgetListItem;
