"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var node_1 = require("../../support/node");
var AutocompleteDropdown = /** @class */ (function () {
    function AutocompleteDropdown(dropdownElement, onClickCallback) {
        this.dropdownElement = dropdownElement;
        this.onClickCallback = onClickCallback;
    }
    AutocompleteDropdown.prototype.bind = function () {
        window.addEventListener("resize", this.onResize.bind(this));
    };
    AutocompleteDropdown.prototype.unbind = function () {
        window.removeEventListener("resize", this.onResize.bind(this));
        this.clearElements();
    };
    AutocompleteDropdown.prototype.updateElements = function (elements) {
        var _this = this;
        this.clearElements();
        if (elements.length > 0) {
            elements.forEach(function (element) {
                var elementNode = node_1.default("div").addClasses("dropdown-item").innerText(element).unwrap();
                elementNode.addEventListener("click", _this.onClickCallback);
                _this.dropdownElement.appendChild(elementNode);
            });
            this.toggleDropDirection();
        }
    };
    AutocompleteDropdown.prototype.onResize = function () {
        clearTimeout(this.changeDropDirectionTimeout);
        this.changeDropDirectionTimeout = setTimeout(this.toggleDropDirection.bind(this), 100);
    };
    AutocompleteDropdown.prototype.clearElements = function () {
        var _this = this;
        this.dropdownElement.childNodes.forEach(function (node) { return node.removeEventListener("click", _this.onClickCallback); });
        this.dropdownElement.innerHTML = "";
    };
    AutocompleteDropdown.prototype.toggleDropDirection = function () {
        this.dropdownElement.classList.remove("lto-dropdirection-up");
        this.dropdownElement.classList.add("lto-dropdirection-down");
        var windowHeight = window.innerHeight
            || document.documentElement.clientHeight
            || document.body.clientHeight;
        if (this.dropdownElement.getBoundingClientRect().bottom > windowHeight) {
            this.dropdownElement.classList.remove("lto-dropdirection-down");
            this.dropdownElement.classList.add("lto-dropdirection-up");
        }
    };
    return AutocompleteDropdown;
}());
exports.AutocompleteDropdown = AutocompleteDropdown;
//# sourceMappingURL=AutocompleteDropdown.js.map