import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var _jsxFileName = 'src/toast/Notification.jsx';
/**
 * notification.jsx
 *
 * @Author: Aubrey
 * @Date: 17/8/29 14:35
 */

import React from 'react';
import PropTypes from 'prop-types';
// import classNames from 'classnames';
import ReactDOM from 'react-dom';
import Notice from './Notice';

var Notification = function (_React$Component) {
    _inherits(Notification, _React$Component);

    function Notification(props) {
        _classCallCheck(this, Notification);

        var _this2 = _possibleConstructorReturn(this, (Notification.__proto__ || Object.getPrototypeOf(Notification)).call(this, props));

        _this2.state = {
            notices: []
        };
        return _this2;
    }

    _createClass(Notification, [{
        key: 'add',
        value: function add(notice) {
            var notices = this.state.notices;

            var key = notice.key ? notice.key : notice.key = getUuid();
            var temp = notices.filter(function (item) {
                return item.key === key;
            }).length;
            if (!temp) {
                notices.push(notice);
                this.setState({
                    notices: notices
                });
            }
        }
    }, {
        key: 'remove',
        value: function remove(key) {
            this.setState(function (previousState) {
                return {
                    notices: previousState.notices.filter(function (notice) {
                        return notice.key !== key;
                    })
                };
            });
        }
    }, {
        key: 'getNoticeDOM',
        value: function getNoticeDOM() {
            var _this3 = this;

            var _this = this;
            var notices = this.state.notices;

            var result = [];
            notices.map(function (notice) {
                var closeCallback = function closeCallback() {
                    _this.remove(notice.key);
                    notice.onClose && notice.onClose();
                };
                result.push(React.createElement(Notice, Object.assign({ key: notice.key }, notice, { onClose: closeCallback, __source: {
                        fileName: _jsxFileName,
                        lineNumber: 53
                    },
                    __self: _this3
                })));
                return true;
            });
            return result;
        }
    }, {
        key: 'render',
        value: function render() {
            var noticesDOM = this.getNoticeDOM();
            return React.createElement(
                'div',
                { className: 'ne-notification-box', __source: {
                        fileName: _jsxFileName,
                        lineNumber: 60
                    },
                    __self: this
                },
                noticesDOM
            );
        }
    }]);

    return Notification;
}(React.Component);

Notification.propTypes = {
    duration: PropTypes.number,
    content: PropTypes.node,
    onClose: PropTypes.func
};


var noticeNumber = 0;
var getUuid = function getUuid() {
    return 'notification-' + new Date().getTime() + '-' + noticeNumber++;
};

Notification.reWrite = function (properties) {
    var _ref = properties || {},
        props = _objectWithoutProperties(_ref, []);

    var div = void 0;
    div = document.createElement('div');
    document.body.appendChild(div);
    var notification = ReactDOM.render(React.createElement(Notification, Object.assign({}, props, {
        __source: {
            fileName: _jsxFileName,
            lineNumber: 74
        },
        __self: this
    })), div);

    return {
        notice: function notice(noticeProps) {
            notification.add(noticeProps);
        },
        removeNotice: function removeNotice(key) {
            notification.remove(key);
        },
        destroy: function destroy() {
            ReactDOM.unmountComponentAtNode(div);
            document.body.removeChild(div);
        },

        component: notification
    };
};
export default Notification;