"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 TestSearcher = (function (_super) {
    __extends(TestSearcher, _super);
    function TestSearcher() {
        var _this = _super.call(this) || this;
        _this.UI.searchInput.focus();
        return _this;
    }
    TestSearcher.prototype.autoComplete = function (query) {
        if (query === void 0) { query = ""; }
        var tests = LeafTestContext.scripts.filter(function (item) { return item.scriptPath.indexOf("test.js") >= 0; }).map(function (item) { return item.scriptPath; });
        tests = tests.filter(function (item) { return item.indexOf(query) >= 0; });
        tests = tests.slice(0, 5);
        this.searchResultList.length = 0;
        for (var _i = 0, tests_1 = tests; _i < tests_1.length; _i++) {
            var path = tests_1[_i];
            this.searchResultList.push(new SearchResultItem(path));
        }
        this.VM.empty = tests.length === 0;
    };
    TestSearcher.prototype.onChildAddSearchResultList = function (child) {
        var _this = this;
        child.events.listenBy(this, "select", function () {
            _this.select(child.path);
        });
    };
    TestSearcher.prototype.select = function (path) {
        this.events.emit("test", path);
        this.searchResultList.length = 0;
    };
    TestSearcher.prototype.applyDefault = function () {
        if (this.UI.searchInput.value === "all") {
            this.select("all");
            return;
        }
        if (this.searchResultList.length === 0)
            return;
        var item = this.searchResultList[0];
        this.select(item.path);
    };
    TestSearcher.prototype.onKeyupSearchInput = function (e) {
        this.autoComplete(this.UI.searchInput.value);
        if (e.which === Leaf.Key.enter) {
            this.applyDefault();
        }
    };
    return TestSearcher;
}(R.TestSearcher));
exports.TestSearcher = TestSearcher;
var SearchResultItem = (function (_super) {
    __extends(SearchResultItem, _super);
    function SearchResultItem(path) {
        var _this = _super.call(this) || this;
        _this.path = path;
        _this.VM.path = path;
        return _this;
    }
    SearchResultItem.prototype.onClick = function () {
        this.events.emit("select");
    };
    return SearchResultItem;
}(R.TestSearcher.SearchResultListItem));
//# sourceMappingURL=testSearcher.js.map