"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PopupControl = void 0;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
var constants_1 = require("./constants");
var Utils = require("../utils");
var host_config_1 = require("../host-config");
var PopupControl = /** @class */ (function () {
    function PopupControl() {
        this._isOpen = false;
    }
    PopupControl.prototype.keyDown = function (e) {
        switch (e.key) {
            case constants_1.Constants.keys.escape:
                this.closePopup(true);
                break;
        }
    };
    PopupControl.prototype.render = function (_rootElementBounds) {
        var _this = this;
        var element = document.createElement("div");
        element.tabIndex = 0;
        element.className = this.hostConfig.makeCssClassName("ac-ctrl", "ac-ctrl-popup-container");
        element.setAttribute("role", "dialog");
        element.setAttribute("aria-modal", "true");
        element.onkeydown = function (e) {
            _this.keyDown(e);
            return !e.cancelBubble;
        };
        element.appendChild(this.renderContent());
        return element;
    };
    PopupControl.prototype.focus = function () {
        if (this._popupElement) {
            this._popupElement.firstElementChild.focus();
        }
    };
    PopupControl.prototype.popup = function (rootElement) {
        var _a, _b, _c, _d, _f;
        var _this = this;
        if (!this._isOpen) {
            this._overlayElement = document.createElement("div");
            this._overlayElement.className = this.hostConfig.makeCssClassName("ac-ctrl-overlay");
            this._overlayElement.tabIndex = 0;
            this._overlayElement.style.width = document.documentElement.scrollWidth + "px";
            this._overlayElement.style.height = document.documentElement.scrollHeight + "px";
            this._overlayElement.onfocus = function (_e) {
                _this.closePopup(true);
            };
            document.body.appendChild(this._overlayElement);
            var rootElementBounds = rootElement.getBoundingClientRect();
            this._popupElement = this.render(rootElementBounds);
            (_a = this._popupElement.classList).remove.apply(_a, this.hostConfig.makeCssClassNames("ac-ctrl-slide", "ac-ctrl-slideLeftToRight", "ac-ctrl-slideRightToLeft", "ac-ctrl-slideTopToBottom", "ac-ctrl-slideRightToLeft"));
            window.addEventListener("resize", function (_e) {
                _this.closePopup(true);
            });
            var rootElementLabel = rootElement.getAttribute("aria-label");
            if (rootElementLabel) {
                this._popupElement.setAttribute("aria-label", rootElementLabel);
            }
            this._overlayElement.appendChild(this._popupElement);
            var popupElementBounds = this._popupElement.getBoundingClientRect();
            var availableSpaceBelow = window.innerHeight - rootElementBounds.bottom;
            var availableSpaceAbove = rootElementBounds.top;
            var availableSpaceRight = window.innerWidth - rootElementBounds.right;
            var availableSpaceLeft = rootElementBounds.left;
            var left = rootElementBounds.left + Utils.getScrollX();
            var top_1;
            if (availableSpaceAbove < popupElementBounds.height &&
                availableSpaceBelow < popupElementBounds.height) {
                // Not enough space above or below root element
                var actualPopupHeight = Math.min(popupElementBounds.height, window.innerHeight);
                this._popupElement.style.maxHeight = actualPopupHeight + "px";
                if (actualPopupHeight < popupElementBounds.height) {
                    top_1 = Utils.getScrollY();
                }
                else {
                    top_1 =
                        Utils.getScrollY() +
                            rootElementBounds.top +
                            (rootElementBounds.height - actualPopupHeight) / 2;
                }
                if (availableSpaceLeft < popupElementBounds.width &&
                    availableSpaceRight < popupElementBounds.width) {
                    // Not enough space left or right of root element
                    var actualPopupWidth = Math.min(popupElementBounds.width, window.innerWidth);
                    this._popupElement.style.maxWidth = actualPopupWidth + "px";
                    if (actualPopupWidth < popupElementBounds.width) {
                        left = Utils.getScrollX();
                    }
                    else {
                        left =
                            Utils.getScrollX() +
                                rootElementBounds.left +
                                (rootElementBounds.width - actualPopupWidth) / 2;
                    }
                }
                else {
                    // Enough space on the left or right of the root element
                    if (availableSpaceRight >= popupElementBounds.width) {
                        left = Utils.getScrollX() + rootElementBounds.right;
                        (_b = this._popupElement.classList).add.apply(_b, this.hostConfig.makeCssClassNames("ac-ctrl-slide", "ac-ctrl-slideLeftToRight"));
                    }
                    else {
                        left =
                            Utils.getScrollX() + rootElementBounds.left - popupElementBounds.width;
                        (_c = this._popupElement.classList).add.apply(_c, this.hostConfig.makeCssClassNames("ac-ctrl-slide", "ac-ctrl-slideRightToLeft"));
                    }
                }
            }
            else {
                // Enough space above or below root element
                if (availableSpaceBelow >= popupElementBounds.height) {
                    top_1 = Utils.getScrollY() + rootElementBounds.bottom;
                    (_d = this._popupElement.classList).add.apply(_d, this.hostConfig.makeCssClassNames("ac-ctrl-slide", "ac-ctrl-slideTopToBottom"));
                }
                else {
                    top_1 = Utils.getScrollY() + rootElementBounds.top - popupElementBounds.height;
                    (_f = this._popupElement.classList).add.apply(_f, this.hostConfig.makeCssClassNames("ac-ctrl-slide", "ac-ctrl-slideBottomToTop"));
                }
                if (availableSpaceRight < popupElementBounds.width) {
                    left = Utils.getScrollX() + rootElementBounds.right - popupElementBounds.width;
                }
            }
            this._popupElement.style.left = left + "px";
            this._popupElement.style.top = top_1 + "px";
            this._popupElement.focus();
            this._isOpen = true;
        }
    };
    PopupControl.prototype.closePopup = function (wasCancelled) {
        if (this._isOpen) {
            document.body.removeChild(this._overlayElement);
            this._isOpen = false;
            if (this.onClose) {
                this.onClose(this, wasCancelled);
            }
        }
    };
    Object.defineProperty(PopupControl.prototype, "hostConfig", {
        get: function () {
            return this._hostConfig ? this._hostConfig : host_config_1.defaultHostConfig;
        },
        set: function (value) {
            this._hostConfig = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(PopupControl.prototype, "isOpen", {
        get: function () {
            return this._isOpen;
        },
        enumerable: false,
        configurable: true
    });
    return PopupControl;
}());
exports.PopupControl = PopupControl;
//# sourceMappingURL=popup-control.js.map