import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import raf from 'raf';
import Notification from './notification';
import { camelCaseToDash } from '../_utils/general';
var className = 'ty-notification-container';
var offset;
var unmountDom = function (queryName, containerDiv, position, height, direction) {
    unmountComponentAtNode(containerDiv);
    document.body.removeChild(containerDiv);
    raf(function () {
        var containers = document.querySelectorAll("." + queryName);
        var len = containers.length;
        for (var i = 0; i < len; i++) {
            var element = containers[i];
            var elementPosition = parseInt(element.style[direction] || '0', 10);
            elementPosition > position &&
                (element.style[direction] = elementPosition - height - offset + "px");
        }
    });
};
var createComponent = function (options, type) {
    var placement = options.placement || 'topRight';
    var queryName = className + "_" + camelCaseToDash(placement);
    var containers = document.querySelectorAll("." + queryName);
    var lastContainer = containers.length > 0 ? containers[containers.length - 1] : null;
    var div = document.createElement('div');
    div.className = className + " " + queryName;
    document.body.appendChild(div);
    offset = options.offset || 24;
    var direction = placement.includes('top') ? 'top' : 'bottom';
    var position = lastContainer
        ? parseInt(lastContainer.style[direction] || '0', 10) + lastContainer.offsetHeight + offset
        : options.margin || 16;
    div.style[direction] = position + "px";
    var props = {
        type: type,
        title: options.title,
        description: options.description,
        footer: options.footer,
        duration: options.duration,
        onClose: options.onClose,
        onClick: options.onClick,
        icon: options.icon,
        didMount: function () {
            placement.includes('Right') ? (div.style.right = "0px") : (div.style.left = "0px");
        },
        willUnmount: function (height) {
            var updatedPosition = parseInt(div.style[direction] || '0', 10);
            unmountDom(queryName, div, updatedPosition, height, direction);
        },
    };
    var element = React.createElement(Notification, props);
    render(element, div);
};
var open = function (options) {
    createComponent(options, undefined);
};
var notificationContainer = {};
['success', 'error', 'warning', 'info'].forEach(function (type) {
    notificationContainer[type] = function (options) {
        createComponent(options, type);
    };
});
notificationContainer.open = open;
notificationContainer.warn = notificationContainer.warning;
export default notificationContainer;
//# sourceMappingURL=index.js.map