import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import Message from './message';
import raf from 'raf';
var className = '.ty-message-container';
var offset;
var unmountDom = function (containerDiv, top, height, onClose) {
    unmountComponentAtNode(containerDiv);
    document.body.removeChild(containerDiv);
    raf(function () {
        var containers = document.querySelectorAll(className);
        var len = containers.length;
        for (var i = 0; i < len; i++) {
            var element = containers[i];
            var elementTop = parseInt(element.style.top || '0', 10);
            elementTop > top && (element.style.top = elementTop - height - offset + "px");
        }
    });
    onClose && onClose();
};
var createComponent = function (type, content, duration, onClose, options) {
    if (duration === void 0) { duration = 3000; }
    if (options === void 0) { options = {}; }
    var containers = document.querySelectorAll(className);
    var lastContainer = containers.length > 0 ? containers[containers.length - 1] : null;
    offset = options.offset || 16;
    var top = lastContainer
        ? parseInt(lastContainer.style.top || '0', 10) + lastContainer.offsetHeight + offset
        : options.top || 15;
    var div = document.createElement('div');
    div.className = 'ty-message-container';
    document.body.appendChild(div);
    div.style.top = top + "px";
    var props = {
        type: type,
        content: content,
        duration: duration,
        icon: options.icon,
        extra: options.extra,
        className: options.className,
        willUnmount: function (height) {
            var updatedTop = parseInt(div.style.top || '0', 10);
            unmountDom(div, updatedTop, height, onClose);
        },
    };
    var component = React.createElement(Message, props);
    render(component, div);
};
var messageContainer = function (content, duration, onClose, options) {
    createComponent(undefined, content, duration, onClose, options);
};
['success', 'error', 'warning', 'info', 'loading'].forEach(function (type) {
    messageContainer[type] = function (content, duration, onClose, options) {
        createComponent(type, content, duration, onClose, options);
    };
});
messageContainer.warn = messageContainer.warning;
export default messageContainer;
//# sourceMappingURL=index.js.map