import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
import _extends from 'babel-runtime/helpers/extends';
import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
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/notice/Notice.jsx',
    _this5 = this;

/**
 * notification.jsx
 *
 * @Author: jruif
 * @Date: 2017/8/16 下午6:00
 */

import React from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import Overlay from '../overlay';
import keyboardKey from '../util/keyboardKey';
import NoticeItem from './NoticeItem';
import deepCopy from 'deep-extend';
import { isString } from '../util/isType';
import omit from '../util/omit';
import pick from '../util/pick';

var transitionAnimate = {
    top: 'notice-slide-down',
    topLeft: 'notice-slide-right',
    topRight: 'notice-slide-left',
    bottom: 'notice-slide-up',
    bottomLeft: 'notice-slide-right',
    bottomRight: 'notice-slide-left'
};

var transitionTime = 500;

/**
 * Notice
 * */

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

    function Notice(props, context) {
        _classCallCheck(this, Notice);

        var _this = _possibleConstructorReturn(this, (Notice.__proto__ || Object.getPrototypeOf(Notice)).call(this, props, context));

        _initialiseProps.call(_this);

        _this.state = {
            list: [], // 存储子元素
            isClearNode: false
        };
        _this.noticeIndex = 100;
        return _this;
    }

    _createClass(Notice, [{
        key: 'componentDidMount',
        value: function componentDidMount() {
            var _this2 = this;

            var items = this.props.items;

            items.forEach(function (item) {
                _this2.show(item);
            });
        }
    }, {
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(nextProps) {
            var _this3 = this;

            var items = nextProps.items;

            items.forEach(function (item) {
                _this3.show(item);
            });
        }
    }, {
        key: 'componentWillUnmount',
        value: function componentWillUnmount() {
            this.setState({
                isClearNode: true
            });
        }

        /**
         * 显示
         * @param itemProps {object} noticeItem 的属性
         * */


        /**
         * 销毁某个noticeItem
         * @param itemID {string} noticeItem 的属性
         * @param itemProps {object} noticeItem 的其他属性
         * */


        /**
         * 销毁某个noticeItem
         * @param itemID {string} noticeItem 的属性
         * @param overtime {bool} 是否设置为超时
         * */

    }, {
        key: 'render',
        value: function render() {
            var _this4 = this;

            var _props = this.props,
                prefixCls = _props.prefixCls,
                autoFocus = _props.autoFocus,
                position = _props.position;

            var transitionName = transitionAnimate[position];
            var listDirection = position.split(/(?=[A-Z]|$)/)[0];
            var classes = classNames(prefixCls, _defineProperty({}, prefixCls + '-' + position, position), ['notice-' + listDirection]);

            return !this.state.isClearNode ? React.createElement(
                Overlay,
                {
                    className: classes,
                    autoFocus: autoFocus,
                    hasMask: false,
                    enforceFocus: false,
                    isOpen: this.state.list.length > 0,
                    onClose: this.handleClose,
                    transitionDuration: transitionTime,
                    transitionName: transitionName,
                    onMouseEnter: this.handleMouseEnter,
                    onMouseLeave: this.handleMouseLeave, __source: {
                        fileName: _jsxFileName,
                        lineNumber: 217
                    },
                    __self: this
                },
                this.state.list.map(function (_ref) {
                    var itemID = _ref.itemID,
                        style = _ref.style,
                        other = _objectWithoutProperties(_ref, ['itemID', 'style']);

                    return React.createElement(NoticeItem, Object.assign({}, other, {
                        key: itemID,
                        itemID: itemID,
                        onClose: _this4.handleDestroyItem(itemID),
                        style: deepCopy({ zIndex: Notice.zIndex++ }, style),
                        __source: {
                            fileName: _jsxFileName,
                            lineNumber: 229
                        },
                        __self: _this4
                    }));
                })
            ) : null;
        }
    }]);

    return Notice;
}(React.Component);

Notice.displayName = 'NeatUI.Notice';
Notice.zIndex = 10000;
Notice.propTypes = {
    prefixCls: PropTypes.string,
    /**
     * 打开时是否获取焦点
     * */
    autoFocus: PropTypes.bool,
    /**
     * 显示的位置
     * */
    position: PropTypes.oneOf(['top', 'topLeft', 'topRight', 'bottom', 'bottomLeft', 'bottomRight']),
    /**
     * @ignore
     * */
    items: PropTypes.array,
    /**
     * @ignore
     * */
    isClearNode: PropTypes.bool
};
Notice.defaultProps = {
    prefixCls: 'ne-notice',
    position: 'top',
    autoFocus: false,
    isClearNode: false,
    items: []
};

Notice.create = function (props) {
    // const container = document.body;
    var containerElement = document.createElement('div');
    // 不在DOM中创建节点
    // container.appendChild(containerElement);
    return ReactDOM.render(React.createElement(Notice, Object.assign({}, props, {
        __source: {
            fileName: _jsxFileName,
            lineNumber: 71
        },
        __self: _this5
    })), containerElement);
};

Notice.show = function (props) {
    var current = Notice.create(_extends({}, pick(props, ['prefixCls', 'position', 'autoFocus']), { isClearNode: true }));
    setTimeout(function () {
        current.show(omit(props, ['prefixCls', 'position', 'autoFocus']));
    }, 0);
};

var _initialiseProps = function _initialiseProps() {
    var _this6 = this;

    this.createKey = function () {
        return 'notice-item-' + _this6.noticeIndex++;
    };

    this.show = function (itemProps) {
        var itemID = _this6.createKey();
        //const { position } = this.props;
        if (isString(itemProps)) {
            itemProps = {
                content: itemProps
            };
        }
        _this6.setState(function (preState) {
            return {
                list: [_extends({}, itemProps, { itemID: itemID })].concat(_toConsumableArray(preState.list))
            };
        });
        // 方便定位具体notice item调用
        return itemID;
    };

    this.update = function (itemID, itemProps) {
        if (isString(itemProps)) {
            itemProps = {
                content: itemProps
            };
        }
        _this6.setState(function (preState) {
            return {
                list: preState.list.map(function (item) {
                    return item.itemID === itemID ? deepCopy({
                        itemID: _this6.createKey()
                    }, itemProps) : item;
                })
            };
        });
    };

    this.destroy = function (itemID) {
        var overtime = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
        var isClearNode = _this6.props.isClearNode;

        _this6.setState(function (_ref2) {
            var list = _ref2.list;
            return {
                list: list.filter(function (item) {
                    var matchesKey = item.itemID === itemID;
                    if (matchesKey) {
                        item.onClose && item.onClose(overtime);
                    }
                    return !matchesKey;
                })
            };
        }, function () {
            if (isClearNode && _this6.state.list.length === 0) {
                setTimeout(function () {
                    _this6.setState({
                        isClearNode: isClearNode
                    });
                }, transitionTime * 1.3); // 延迟到动画执行的1.3倍后再移除节点
            }
        });
    };

    this.handleDestroyItem = function (itemID) {
        return function (overtime) {
            _this6.destroy(itemID, overtime);
        };
    };

    this.handleClose = function (e) {
        // 按ESC键关闭所有
        if (keyboardKey.getCode(e) !== keyboardKey.name.Escape) {
            _this6.clear();
        }
    };

    this.clear = function () {
        _this6.state.list.map(function (item) {
            return item.onClose(false);
        });
        _this6.setState({ list: [] });
    };
};

export default Notice;