'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactIntl = require('react-intl');

var _MiniCloseButton = require('../navigation/MiniCloseButton');

var _MiniCloseButton2 = _interopRequireDefault(_MiniCloseButton);

var _Notification = require('../../../styles/cspace-ui/Notification.css');

var _Notification2 = _interopRequireDefault(_Notification);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* global window */

var propTypes = {
  id: _propTypes2.default.string.isRequired,
  items: _propTypes2.default.arrayOf(_propTypes2.default.object),
  date: _propTypes2.default.instanceOf(Date),
  status: _propTypes2.default.string,
  autoClose: _propTypes2.default.bool,
  autoCloseTime: _propTypes2.default.number,
  showCloseButton: _propTypes2.default.bool,
  children: _propTypes2.default.node,
  close: _propTypes2.default.func
};

var defaultProps = {
  autoCloseTime: 5000, // ms
  showCloseButton: true
};

var Notification = function (_Component) {
  _inherits(Notification, _Component);

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

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

    _this.handleCloseButtonClick = _this.handleCloseButtonClick.bind(_this);
    _this.handleCloseButtonFocus = _this.handleCloseButtonFocus.bind(_this);
    _this.handleMouseDown = _this.handleMouseDown.bind(_this);
    return _this;
  }

  _createClass(Notification, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var autoClose = this.props.autoClose;


      if (autoClose) {
        this.startAutoCloseTimer();
      }
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      var prevAutoClose = prevProps.autoClose;
      var autoClose = this.props.autoClose;


      if (autoClose && !prevAutoClose) {
        this.startAutoCloseTimer();
      }
    }
  }, {
    key: 'cancelAutoCloseTimer',
    value: function cancelAutoCloseTimer() {
      if (this.autoCloseTimer) {
        window.clearTimeout(this.autoCloseTimer);
      }

      this.autoCloseTimer = null;
    }
  }, {
    key: 'close',
    value: function close() {
      var _props = this.props,
          id = _props.id,
          close = _props.close;


      if (close) {
        close(id);
      }
    }
  }, {
    key: 'startAutoCloseTimer',
    value: function startAutoCloseTimer() {
      var _this2 = this;

      var autoCloseTime = this.props.autoCloseTime;


      this.cancelAutoCloseTimer();

      this.autoCloseTimer = window.setTimeout(function () {
        _this2.close();
      }, autoCloseTime);
    }
  }, {
    key: 'handleCloseButtonClick',
    value: function handleCloseButtonClick() {
      this.close();
    }
  }, {
    key: 'handleCloseButtonFocus',
    value: function handleCloseButtonFocus() {
      this.cancelAutoCloseTimer();
    }
  }, {
    key: 'handleMouseDown',
    value: function handleMouseDown() {
      this.cancelAutoCloseTimer();
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          items = _props2.items,
          date = _props2.date,
          status = _props2.status,
          showCloseButton = _props2.showCloseButton,
          children = _props2.children;


      var className = status ? _Notification2.default[status] : _Notification2.default.common;

      var timestamp = null;

      if (date) {
        timestamp = _react2.default.createElement(_reactIntl.FormattedTime, {
          value: date,
          hour: 'numeric',
          minute: 'numeric',
          second: 'numeric'
        });
      }

      var closeButton = void 0;

      if (showCloseButton) {
        closeButton = _react2.default.createElement(_MiniCloseButton2.default, {
          onClick: this.handleCloseButtonClick,
          onFocus: this.handleCloseButtonFocus
        });
      }

      var content = void 0;

      if (children) {
        content = children;
      } else if (items) {
        var listItems = items.map(function (item, index) {
          return _react2.default.createElement(
            'li',
            { key: index },
            _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, item.message, { values: item.values }))
          );
        });

        content = _react2.default.createElement(
          'ul',
          null,
          listItems
        );
      }

      return _react2.default.createElement(
        'div',
        { className: className, onMouseDown: this.handleMouseDown },
        closeButton,
        _react2.default.createElement(
          'div',
          null,
          _react2.default.createElement(
            'header',
            null,
            timestamp
          ),
          _react2.default.createElement(
            'div',
            null,
            content
          )
        )
      );
    }
  }]);

  return Notification;
}(_react.Component);

exports.default = Notification;


Notification.propTypes = propTypes;
Notification.defaultProps = defaultProps;