"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _taggedTemplateLiteralLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteralLoose"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

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

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

var _utils = require("../utils");

var _CloseButton = _interopRequireDefault(require("../CloseButton"));

function _templateObject2() {
  var data = (0, _taggedTemplateLiteralLoose2.default)(["content"]);

  _templateObject2 = function _templateObject2() {
    return data;
  };

  return data;
}

function _templateObject() {
  var data = (0, _taggedTemplateLiteralLoose2.default)(["title-with-icon"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

var defaultProps = {
  as: 'div',
  classPrefix: 'notification',
  duration: 4500
};

var Notification = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var Component = props.as,
      classPrefix = props.classPrefix,
      closable = props.closable,
      duration = props.duration,
      className = props.className,
      type = props.type,
      header = props.header,
      children = props.children,
      onClose = props.onClose,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "classPrefix", "closable", "duration", "className", "type", "header", "children", "onClose"]);

  var _useState = (0, _react.useState)('show'),
      display = _useState[0],
      setDisplay = _useState[1];

  var _useClassNames = (0, _utils.useClassNames)(classPrefix),
      withClassPrefix = _useClassNames.withClassPrefix,
      merge = _useClassNames.merge,
      prefix = _useClassNames.prefix; // Timed close message


  var _useTimeout = (0, _utils.useTimeout)(onClose, duration, duration > 0),
      clear = _useTimeout.clear; // Click to trigger to close the message


  var handleClose = (0, _react.useCallback)(function (event) {
    setDisplay('hiding');
    onClose === null || onClose === void 0 ? void 0 : onClose(event);
    clear();
    setTimeout(function () {
      setDisplay('hide');
    }, 1000);
  }, [onClose, clear]);
  var renderHeader = (0, _react.useCallback)(function () {
    if (!header) {
      return null;
    }

    return /*#__PURE__*/_react.default.createElement("div", {
      className: prefix('title')
    }, type ? /*#__PURE__*/_react.default.createElement("div", {
      className: prefix(_templateObject())
    }, _utils.MESSAGE_STATUS_ICONS[type], header) : /*#__PURE__*/_react.default.createElement("div", {
      className: prefix('title')
    }, header));
  }, [header, type, prefix]);

  if (display === 'hide') {
    return null;
  }

  var classes = merge(className, withClassPrefix(type, display, {
    closable: closable
  }));
  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
    role: "alert"
  }, rest, {
    ref: ref,
    className: classes
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: prefix(_templateObject2())
  }, renderHeader(), /*#__PURE__*/_react.default.createElement("div", {
    className: prefix('description')
  }, typeof children === 'function' ? children() : children)), closable && /*#__PURE__*/_react.default.createElement(_CloseButton.default, {
    onClick: handleClose
  }));
});

Notification.displayName = 'Notification';
Notification.defaultProps = defaultProps;
Notification.propTypes = {
  as: _propTypes.default.elementType,
  duration: _propTypes.default.number,
  header: _propTypes.default.node,
  closable: _propTypes.default.bool,
  classPrefix: _propTypes.default.string,
  className: _propTypes.default.string,
  type: _propTypes.default.oneOf(['info', 'success', 'warning', 'error']),
  onClose: _propTypes.default.func
};
var _default = Notification;
exports.default = _default;