import { cssClass } from '../../styles';
var DialogManager = /** @class */ (function () {
    function DialogManager(doc) {
        var _this = this;
        if (doc === void 0) { doc = document; }
        this.escKeyListener = function (evt) {
            if (evt.which === 27) {
                _this.removeLast();
            }
        };
        this.openDialogs = [];
        this.node = doc.createElement('div');
        this.node.classList.add(cssClass('backdrop'));
        this.node.innerHTML = "<div class=\"" + cssClass('backdrop-bg') + "\"></div>";
        this.node.onclick = function () {
            _this.removeAll();
        };
    }
    DialogManager.prototype.setHighlight = function (mask) {
        var area = this.node.firstElementChild;
        // @see http://bennettfeely.com/clippy/ -> select `Frame` example
        // use webkit prefix for safari
        area.style.clipPath = area.style.webkitClipPath = "polygon(\n      0% 0%,\n      0% 100%,\n      " + mask.left + "px 100%,\n      " + mask.left + "px " + mask.top + "px,\n      " + (mask.left + mask.width) + "px " + mask.top + "px,\n      " + (mask.left + mask.width) + "px " + (mask.top + mask.height) + "px,\n      " + mask.left + "px " + (mask.top + mask.height) + "px,\n      " + mask.left + "px 100%,\n      100% 100%,\n      100% 0%\n    )";
    };
    DialogManager.prototype.setHighlightColumn = function (column) {
        var root = this.node.parentElement;
        if (!root) {
            this.clearHighlight();
            return;
        }
        var header = root.querySelector("." + cssClass('header') + "[data-col-id=\"" + column.id + "\"]");
        if (!header) {
            this.clearHighlight();
            return;
        }
        var base = header.getBoundingClientRect();
        var offset = root.getBoundingClientRect();
        this.setHighlight({
            left: base.left - offset.left,
            top: base.top - offset.top,
            width: base.width,
            height: offset.height
        });
    };
    DialogManager.prototype.clearHighlight = function () {
        var area = this.node.firstElementChild;
        area.style.clipPath = null;
    };
    DialogManager.prototype.removeLast = function () {
        if (this.openDialogs.length === 0) {
            return;
        }
        this.remove(this.openDialogs[this.openDialogs.length - 1]);
    };
    DialogManager.prototype.removeAll = function () {
        if (this.openDialogs.length === 0) {
            return;
        }
        var all = this.openDialogs.splice(0, this.openDialogs.length);
        all.forEach(function (d) { return d.destroy(); });
        this.takeDown();
    };
    DialogManager.prototype.remove = function (dialog) {
        var index = this.openDialogs.indexOf(dialog);
        if (index < 0) {
            return false;
        }
        // destroy self and all levels below that = after that
        var destroyed = this.openDialogs.splice(index, this.openDialogs.length - index);
        destroyed.reverse().forEach(function (d) { return d.destroy(); });
        if (this.openDialogs.length === 0) {
            this.takeDown();
        }
        return true;
    };
    DialogManager.prototype.removeAboveLevel = function (level) {
        var _this = this;
        // hide all dialogs which have a higher or equal level to the newly opened one
        this.openDialogs.filter(function (d) { return d.level >= level; }).reverse().forEach(function (d) { return _this.remove(d); });
    };
    DialogManager.prototype.removeLike = function (dialog) {
        var similar = this.openDialogs.find(function (d) { return dialog.equals(d); });
        if (!similar) {
            return false;
        }
        this.remove(similar);
        return true;
    };
    DialogManager.prototype.setUp = function () {
        this.node.ownerDocument.addEventListener('keyup', this.escKeyListener, {
            passive: true
        });
        this.node.style.display = 'block';
    };
    DialogManager.prototype.takeDown = function () {
        this.clearHighlight();
        this.node.ownerDocument.removeEventListener('keyup', this.escKeyListener);
        this.node.style.display = null;
    };
    DialogManager.prototype.push = function (dialog) {
        this.removeAboveLevel(dialog.level);
        if (this.openDialogs.length === 0) {
            this.setUp();
        }
        this.openDialogs.push(dialog);
    };
    return DialogManager;
}());
export default DialogManager;
//# sourceMappingURL=DialogManager.js.map