'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; }; /* global window */

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 }; }

const 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
};

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

class Notification extends _react.Component {
  constructor() {
    super();

    this.handleCloseButtonClick = this.handleCloseButtonClick.bind(this);
    this.handleCloseButtonFocus = this.handleCloseButtonFocus.bind(this);
    this.handleMouseDown = this.handleMouseDown.bind(this);
  }

  componentDidMount() {
    const autoClose = this.props.autoClose;


    if (autoClose) {
      this.startAutoCloseTimer();
    }
  }

  componentDidUpdate(prevProps) {
    const prevAutoClose = prevProps.autoClose;
    const autoClose = this.props.autoClose;


    if (autoClose && !prevAutoClose) {
      this.startAutoCloseTimer();
    }
  }

  cancelAutoCloseTimer() {
    if (this.autoCloseTimer) {
      window.clearTimeout(this.autoCloseTimer);
    }

    this.autoCloseTimer = null;
  }

  close() {
    var _props = this.props;
    const id = _props.id,
          close = _props.close;


    if (close) {
      close(id);
    }
  }

  startAutoCloseTimer() {
    const autoCloseTime = this.props.autoCloseTime;


    this.cancelAutoCloseTimer();

    this.autoCloseTimer = window.setTimeout(() => {
      this.close();
    }, autoCloseTime);
  }

  handleCloseButtonClick() {
    this.close();
  }

  handleCloseButtonFocus() {
    this.cancelAutoCloseTimer();
  }

  handleMouseDown() {
    this.cancelAutoCloseTimer();
  }

  render() {
    var _props2 = this.props;
    const items = _props2.items,
          date = _props2.date,
          status = _props2.status,
          showCloseButton = _props2.showCloseButton,
          children = _props2.children;


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

    let timestamp = null;

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

    let closeButton;

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

    let content;

    if (children) {
      content = children;
    } else if (items) {
      const listItems = items.map((item, index) => _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
        )
      )
    );
  }
}

exports.default = Notification;
Notification.propTypes = propTypes;
Notification.defaultProps = defaultProps;