"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var react_dom_1 = __importDefault(require("react-dom"));
var Alert_1 = require("./popups/Alert");
var Confirm_1 = require("./popups/Confirm");
var PopupDispatcher_1 = require("./popups/PopupDispatcher");
var NotificationDispatcher_1 = require("./NotificationDispatcher");
var Notification_1 = require("./Notification");
var functions_1 = require("../utils/functions");
exports.popupContainerId = 'popup-container-' + Math.random();
var popupDispatcher;
var notificationDispatcher;
(function initializePopupContainer() {
    var popupContainer = document.createElement('div');
    popupContainer.id = exports.popupContainerId;
    var wrapper = document.querySelector('.bootstrap-rc');
    if (wrapper) {
        wrapper.appendChild(popupContainer);
    }
    else {
        // FIXME [german.smirnov-30.01.19] HOTFIX for storybook. It initializes script before html. It should be throw new Error
        var bootstrapRC = document.createElement('div');
        bootstrapRC.className = 'bootstrap-rc';
        bootstrapRC.appendChild(popupContainer);
        document.body.appendChild(bootstrapRC);
        // eslint-disable-next-line no-console
        console.error('Initialize div.bootstrap-rc before use web-modules-core');
    }
})();
function showPopup(componentOrElement, props, popupContext) {
    if (props === void 0) { props = {}; }
    if (popupContext === void 0) { popupContext = {}; }
    var _a = (props || {}), messageText = _a.messageText, rest = __rest(_a, ["messageText"]);
    var element = functions_1.isReactElement(componentOrElement)
        ? componentOrElement
        : react_1.default.createElement(componentOrElement, rest, messageText);
    // TODO: (UIA-70822) temporary solution caused by React 16 async rendering
    setTimeout(function () {
        if (!popupDispatcher || !element.props.stackable) {
            popupDispatcher = react_dom_1.default.render(react_1.default.createElement(PopupDispatcher_1.PopupDispatcher, { popupContext: popupContext }), document.getElementById(exports.popupContainerId));
        }
        popupDispatcher.showPopup(element);
    }, 0);
}
exports.showPopup = showPopup;
function closePopup() {
    if (popupDispatcher) {
        popupDispatcher.closePopup();
    }
}
exports.closePopup = closePopup;
function showAlert(params) {
    if (typeof params !== 'object') {
        params = { messageText: params };
    }
    showPopup(Alert_1.Alert, params);
}
exports.showAlert = showAlert;
function showConfirmation(params) {
    showPopup(Confirm_1.Confirm, params);
}
exports.showConfirmation = showConfirmation;
/**
 * Show "toast" notification at the top of the page
 *
 * @example Pre-rendered component
 * showNotification(
 *   <Notification success>
 *     {translate(langCommon.DONE)}
 *   </Notification>
 * );
 *
 * @example Props for Notification component
 * showNotification({
 *   messageText: translate(langCommon.DONE),
 *   success: true
 * });
 *
 * @example Custom component with props
 * showNotification(Panel, {
 *   messageText: translate(langCommon.DONE)
 * });
 *
 * @example Default notification
 * showNotification(translate(langCommon.DONE));
 *
 * @example Error notification
 * showNotification(translate(langCommon.DONE), 'error');
 *
 * @param {Object|string} componentOrElement - Component, Element, Notification properties or text message
 * @param {Object|string} props - Properties for Component or type of notification if componentOrElement is a text message
 */
function showNotification(componentOrElement, props) {
    var _a;
    if (props === void 0) { props = {}; }
    var element = null;
    if (functions_1.isReactElement(componentOrElement)) {
        element = componentOrElement;
    }
    else if (arguments.length === 1 && typeof componentOrElement === 'object') {
        var _b = componentOrElement, messageText = _b.messageText, rest = __rest(_b, ["messageText"]);
        element = react_1.default.createElement(Notification_1.Notification, rest, messageText);
    }
    else if (typeof componentOrElement === 'string') {
        var type = typeof props === 'string' ? props : 'default';
        element = react_1.default.createElement(Notification_1.Notification, (_a = {}, _a[type] = true, _a), componentOrElement);
    }
    else {
        var messageText = props.messageText, rest = __rest(props, ["messageText"]);
        element = react_1.default.createElement(componentOrElement, rest, messageText);
    }
    if (!notificationDispatcher) {
        notificationDispatcher = react_dom_1.default.render(react_1.default.createElement(NotificationDispatcher_1.NotificationDispatcher), document.getElementById('notification-container'));
    }
    notificationDispatcher.showNotification(element);
}
exports.showNotification = showNotification;
function getElementHeight(element, withMargin) {
    if (withMargin === void 0) { withMargin = false; }
    var height;
    var margin;
    if (document.all) {
        height = parseInt(element.currentStyle.height, 10);
        margin = withMargin
            ? parseInt(element.currentStyle.marginTop, 10) +
                parseInt(element.currentStyle.marginBottom, 10)
            : 0;
    }
    else {
        var styles = document.defaultView.getComputedStyle(element, '');
        height = parseInt(element.offsetHeight, 10);
        margin = withMargin
            ? parseInt(styles.getPropertyValue('margin-top')) +
                parseInt(styles.getPropertyValue('margin-bottom'))
            : 0;
    }
    return (height || 0) + (margin || 0);
}
exports.getElementHeight = getElementHeight;
function getElementMargin(node) {
    var style = window.getComputedStyle(node);
    return {
        marginTop: extractCSSProperty(style.marginTop),
        marginBottom: extractCSSProperty(style.marginBottom),
        marginLeft: extractCSSProperty(style.marginLeft),
        marginRight: extractCSSProperty(style.marginRight)
    };
}
exports.getElementMargin = getElementMargin;
function getElementPadding(node) {
    var style = window.getComputedStyle(node);
    return {
        paddingTop: extractCSSProperty(style.paddingTop),
        paddingBottom: extractCSSProperty(style.paddingBottom),
        paddingLeft: extractCSSProperty(style.paddingLeft),
        paddingRight: extractCSSProperty(style.paddingRight)
    };
}
exports.getElementPadding = getElementPadding;
function getElementBorder(node) {
    var style = window.getComputedStyle(node);
    return {
        borderTop: extractCSSProperty(style.borderTop),
        borderBottom: extractCSSProperty(style.borderBottom),
        borderLeft: extractCSSProperty(style.borderLeft),
        borderRight: extractCSSProperty(style.borderRight)
    };
}
exports.getElementBorder = getElementBorder;
function extractCSSProperty(propertyValue) {
    return parseInt(propertyValue, 10) || 0;
}
function scrollToTop() {
    document.body.scrollTop = document.documentElement.scrollTop = 0;
}
exports.scrollToTop = scrollToTop;
/**
 * Scrolls window to the elements node position
 * @param {React.Element} element || DOM node
 */
function scrollToNode(element) {
    if (!element) {
        return;
    }
    var position;
    if (element.getBoundingClientRect) {
        position = element.getBoundingClientRect();
    }
    else {
        var node = react_dom_1.default.findDOMNode(element);
        position = node && node.getBoundingClientRect();
    }
    if (position) {
        window.scrollTo(position.x, position.y);
    }
}
exports.scrollToNode = scrollToNode;
function getScrollbarWidth() {
    var outer = document.createElement('div');
    outer.style.visibility = 'hidden';
    outer.style.width = '100px';
    outer.style.msOverflowStyle = 'scrollbar';
    document.body.appendChild(outer);
    var widthNoScroll = outer.offsetWidth;
    outer.style.overflow = 'scroll';
    var inner = document.createElement('div');
    inner.style.width = '100%';
    outer.appendChild(inner);
    var widthWithScroll = inner.offsetWidth;
    outer.parentNode.removeChild(outer);
    return widthNoScroll - widthWithScroll;
}
exports.getScrollbarWidth = getScrollbarWidth;
exports.restorePopupVisibility = function ($container) {
    if ($container === void 0) { $container = document.getElementById(exports.popupContainerId); }
    var $topPopup = $container.querySelector('div[role="document"][aria-hidden="true"]:last-child');
    if ($topPopup) {
        $topPopup.removeAttribute('aria-hidden');
    }
};
//# sourceMappingURL=utils.js.map