'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _rcAnimate = require('rc-animate');

var _rcAnimate2 = _interopRequireDefault(_rcAnimate);

var _createChainedFunction = require('rc-util/lib/createChainedFunction');

var _createChainedFunction2 = _interopRequireDefault(_createChainedFunction);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _Notice = require('./Notice');

var _Notice2 = _interopRequireDefault(_Notice);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var seed = 0;
var now = Date.now();

function getUuid() {
  return 'rcNotification_' + now + '_' + seed++;
}

var Notification = function (_Component) {
  (0, _inherits3["default"])(Notification, _Component);

  function Notification() {
    var _temp, _this, _ret;

    (0, _classCallCheck3["default"])(this, Notification);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3["default"])(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.state = {
      notices: []
    }, _this.add = function (notice) {
      var key = notice.key = notice.key || getUuid();
      _this.setState(function (previousState) {
        var notices = previousState.notices;
        if (!notices.filter(function (v) {
          return v.key === key;
        }).length) {
          return {
            notices: notices.concat(notice)
          };
        }
      });
    }, _this.remove = function (key) {
      _this.setState(function (previousState) {
        return {
          notices: previousState.notices.filter(function (notice) {
            return notice.key !== key;
          })
        };
      });
    }, _temp), (0, _possibleConstructorReturn3["default"])(_this, _ret);
  }

  Notification.prototype.getTransitionName = function getTransitionName() {
    var props = this.props;
    var transitionName = props.transitionName;
    if (!transitionName && props.animation) {
      transitionName = props.prefixCls + '-' + props.animation;
    }
    return transitionName;
  };

  Notification.prototype.render = function render() {
    var _this2 = this,
        _className;

    var props = this.props;
    var noticeNodes = this.state.notices.map(function (notice) {
      var onClose = (0, _createChainedFunction2["default"])(_this2.remove.bind(_this2, notice.key), notice.onClose);
      return _react2["default"].createElement(
        _Notice2["default"],
        (0, _extends3["default"])({
          prefixCls: props.prefixCls
        }, notice, {
          onClose: onClose
        }),
        notice.content
      );
    });
    var className = (_className = {}, (0, _defineProperty3["default"])(_className, props.prefixCls, 1), (0, _defineProperty3["default"])(_className, props.className, !!props.className), _className);
    var animateProps = {};
    if (this.state.notices.length <= 1) {
      animateProps.component = '';
    }
    return _react2["default"].createElement(
      'div',
      { className: (0, _classnames2["default"])(className), style: props.style },
      _react2["default"].createElement(
        _rcAnimate2["default"],
        (0, _extends3["default"])({ transitionName: this.getTransitionName() }, animateProps),
        noticeNodes
      )
    );
  };

  return Notification;
}(_react.Component);

Notification.propTypes = {
  prefixCls: _propTypes2["default"].string,
  transitionName: _propTypes2["default"].string,
  animation: _propTypes2["default"].oneOfType([_propTypes2["default"].string, _propTypes2["default"].object]),
  style: _propTypes2["default"].object
};
Notification.defaultProps = {
  prefixCls: 'rc-notification',
  animation: 'fade',
  style: {
    top: 65,
    left: '50%'
  }
};


Notification.newInstance = function newNotificationInstance(properties) {
  var _ref = properties || {},
      getContainer = _ref.getContainer,
      props = (0, _objectWithoutProperties3["default"])(_ref, ['getContainer']);

  var div = void 0;
  if (getContainer) {
    div = getContainer();
  } else {
    div = document.createElement('div');
    document.body.appendChild(div);
  }
  var notification = _reactDom2["default"].render(_react2["default"].createElement(Notification, props), div);
  return {
    notice: function notice(noticeProps) {
      notification.add(noticeProps);
    },
    removeNotice: function removeNotice(key) {
      notification.remove(key);
    },

    component: notification,
    destroy: function destroy() {
      _reactDom2["default"].unmountComponentAtNode(div);
      document.body.removeChild(div);
    }
  };
};

exports["default"] = Notification;
module.exports = exports['default'];