"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _MiniCloseButton = _interopRequireDefault(require("../navigation/MiniCloseButton"));

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

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _extends() { _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; }; return _extends.apply(this, arguments); }

const propTypes = {
  id: _propTypes.default.string.isRequired,
  items: _propTypes.default.arrayOf(_propTypes.default.object),
  date: _propTypes.default.instanceOf(Date),
  status: _propTypes.default.string,
  autoClose: _propTypes.default.bool,
  autoCloseTime: _propTypes.default.number,
  showCloseButton: _propTypes.default.bool,
  children: _propTypes.default.node,
  close: _propTypes.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;

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

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

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

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

    this.autoCloseTimer = null;
  }

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

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

  startAutoCloseTimer() {
    const {
      autoCloseTime
    } = this.props;
    this.cancelAutoCloseTimer();
    this.autoCloseTimer = window.setTimeout(() => {
      this.close();
    }, autoCloseTime);
  }

  handleCloseButtonClick() {
    this.close();
  }

  handleCloseButtonFocus() {
    this.cancelAutoCloseTimer();
  }

  handleMouseDown() {
    this.cancelAutoCloseTimer();
  }

  render() {
    const {
      items,
      date,
      status,
      showCloseButton,
      children
    } = this.props;
    const className = status ? _Notification.default[status] : _Notification.default.common;
    let timestamp = null;

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

    let closeButton;

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

    let content;

    if (children) {
      content = children;
    } else if (items) {
      const listItems = items.map((item, index) => _react.default.createElement("li", {
        key: index
      }, _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, item.message, {
        values: item.values
      }))));
      content = _react.default.createElement("ul", null, listItems);
    }

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

}

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