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;
};
import React from 'react';
import ReactDOM from 'react-dom';
import { Alert } from './popups/Alert';
import { Confirm } from './popups/Confirm';
import { PopupDispatcher } from './popups/PopupDispatcher';
import { NotificationDispatcher } from './NotificationDispatcher';
import { Notification } from './Notification';
import { isReactElement } from '../utils/functions';
export var popupContainerId = 'popup-container-' + Math.random();
var popupDispatcher;
var notificationDispatcher;
(function initializePopupContainer() {
    var popupContainer = document.createElement('div');
    popupContainer.id = 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');
    }
})();
export 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 = isReactElement(componentOrElement)
        ? componentOrElement
        : React.createElement(componentOrElement, rest, messageText);
    // TODO: (UIA-70822) temporary solution caused by React 16 async rendering
    setTimeout(function () {
        if (!popupDispatcher || !element.props.stackable) {
            popupDispatcher = ReactDOM.render(React.createElement(PopupDispatcher, { popupContext: popupContext }), document.getElementById(popupContainerId));
        }
        popupDispatcher.showPopup(element);
    }, 0);
}
export function closePopup() {
    if (popupDispatcher) {
        popupDispatcher.closePopup();
    }
}
export function showAlert(params) {
    if (typeof params !== 'object') {
        params = { messageText: params };
    }
    showPopup(Alert, params);
}
export function showConfirmation(params) {
    showPopup(Confirm, params);
}
/**
 * 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
 */
export function showNotification(componentOrElement, props) {
    var _a;
    if (props === void 0) { props = {}; }
    var element = null;
    if (isReactElement(componentOrElement)) {
        element = componentOrElement;
    }
    else if (arguments.length === 1 && typeof componentOrElement === 'object') {
        var _b = componentOrElement, messageText = _b.messageText, rest = __rest(_b, ["messageText"]);
        element = React.createElement(Notification, rest, messageText);
    }
    else if (typeof componentOrElement === 'string') {
        var type = typeof props === 'string' ? props : 'default';
        element = React.createElement(Notification, (_a = {}, _a[type] = true, _a), componentOrElement);
    }
    else {
        var messageText = props.messageText, rest = __rest(props, ["messageText"]);
        element = React.createElement(componentOrElement, rest, messageText);
    }
    if (!notificationDispatcher) {
        notificationDispatcher = ReactDOM.render(React.createElement(NotificationDispatcher), document.getElementById('notification-container'));
    }
    notificationDispatcher.showNotification(element);
}
export 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);
}
export 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)
    };
}
export 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)
    };
}
export 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)
    };
}
function extractCSSProperty(propertyValue) {
    return parseInt(propertyValue, 10) || 0;
}
export function scrollToTop() {
    document.body.scrollTop = document.documentElement.scrollTop = 0;
}
/**
 * Scrolls window to the elements node position
 * @param {React.Element} element || DOM node
 */
export function scrollToNode(element) {
    if (!element) {
        return;
    }
    var position;
    if (element.getBoundingClientRect) {
        position = element.getBoundingClientRect();
    }
    else {
        var node = ReactDOM.findDOMNode(element);
        position = node && node.getBoundingClientRect();
    }
    if (position) {
        window.scrollTo(position.x, position.y);
    }
}
export 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;
}
export var restorePopupVisibility = function ($container) {
    if ($container === void 0) { $container = document.getElementById(popupContainerId); }
    var $topPopup = $container.querySelector('div[role="document"][aria-hidden="true"]:last-child');
    if ($topPopup) {
        $topPopup.removeAttribute('aria-hidden');
    }
};
//# sourceMappingURL=utils.js.map