"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        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 extendStatics(d, b);
    };
    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 Defaults_1 = require("../support/Defaults");
var api_1 = require("../api");
var FlexSearchIndex_1 = require("./autocomplete/index/FlexSearchIndex");
var AutocompleteDropdown_1 = require("./autocomplete/AutocompleteDropdown");
/**
 * The autocomplete behaviour adds an autocomplete mechanism
 * for the user input. The possible autocomplete entries
 * need to be added via add method.
 */
var AutocompleteBehaviour = /** @class */ (function (_super) {
    __extends(AutocompleteBehaviour, _super);
    function AutocompleteBehaviour(config, textbox) {
        if (textbox === void 0) { textbox = Defaults_1.Defaults.textbox(); }
        var _this = _super.call(this, textbox, [
            { type: "input", handler: function () { return _this.onKeydown(textbox); } }
        ]) || this;
        _this.index = config.index || new FlexSearchIndex_1.FlexSearchIndex();
        _this.maxNumberOfResults = config.maxNumberOfResults || 5;
        _this.autocompleteDropdown = new AutocompleteDropdown_1.AutocompleteDropdown(config.dropdownElement || Defaults_1.Defaults.autocomplete(), function (event) {
            textbox.value = event.currentTarget.innerText;
            (config.invokerElement || Defaults_1.Defaults.invoker()).click();
        });
        return _this;
    }
    AutocompleteBehaviour.prototype.add = function (element) {
        this.index.add(element);
    };
    AutocompleteBehaviour.prototype.clear = function () {
        this.index.clear();
    };
    AutocompleteBehaviour.prototype.bind = function (gateway) {
        _super.prototype.bind.call(this, gateway);
        this.autocompleteDropdown.bind();
    };
    AutocompleteBehaviour.prototype.unbind = function () {
        _super.prototype.unbind.call(this);
        this.autocompleteDropdown.unbind();
    };
    AutocompleteBehaviour.prototype.onKeydown = function (textbox) {
        var _this = this;
        if (textbox.value.length > 0) {
            this.index.search(textbox.value, this.maxNumberOfResults)
                .then(function (elements) { return _this.autocompleteDropdown.updateElements(elements); });
        }
        else {
            this.autocompleteDropdown.updateElements([]);
        }
    };
    return AutocompleteBehaviour;
}(api_1.IBehaviour));
exports.AutocompleteBehaviour = AutocompleteBehaviour;
//# sourceMappingURL=AutocompleteBehaviour.js.map