"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 _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); } /* global window */
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();
    }
  }
  handleCloseButtonClick() {
    this.close();
  }
  handleCloseButtonFocus() {
    this.cancelAutoCloseTimer();
  }
  handleMouseDown() {
    this.cancelAutoCloseTimer();
  }
  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);
  }
  render() {
    const {
      items,
      date,
      status,
      showCloseButton,
      children
    } = this.props;
    const className = status ? _Notification.default[status] : _Notification.default.common;
    let timestamp = null;
    if (date) {
      timestamp = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedTime, {
        value: date,
        hour: "numeric",
        minute: "numeric",
        second: "numeric"
      });
    }
    let closeButton;
    if (showCloseButton) {
      closeButton = /*#__PURE__*/_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) =>
      /*#__PURE__*/
      // eslint-disable-next-line react/no-array-index-key
      _react.default.createElement("li", {
        key: index
      }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, item.message, {
        values: item.values
      }))));
      content = /*#__PURE__*/_react.default.createElement("ul", null, listItems);
    }
    return (
      /*#__PURE__*/
      // FIXME: Move mouse down handler into a button.
      // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
      _react.default.createElement("div", {
        className: className,
        role: "status",
        onMouseDown: this.handleMouseDown
      }, closeButton, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("header", null, timestamp), /*#__PURE__*/_react.default.createElement("div", null, content)))
    );
  }
}
exports.default = Notification;
Notification.propTypes = propTypes;
Notification.defaultProps = defaultProps;