"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 _templateObject5() {
  var data = (0, _taggedTemplateLiteralLoose2.default)(["body"]);

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

  return data;
}

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

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

  return data;
}

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

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

  return data;
}

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

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

  return data;
}

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

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

  return data;
}

var defaultProps = {
  as: 'div',
  classPrefix: 'message',
  type: 'info',
  duration: 2000
};

var Message = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var _withClassPrefix;

  var Component = props.as,
      className = props.className,
      classPrefix = props.classPrefix,
      children = props.children,
      closable = props.closable,
      duration = props.duration,
      full = props.full,
      header = props.header,
      type = props.type,
      showIcon = props.showIcon,
      onClose = props.onClose,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "className", "classPrefix", "children", "closable", "duration", "full", "header", "type", "showIcon", "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;

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

  var classes = merge(className, withClassPrefix(type, display, (_withClassPrefix = {
    full: full
  }, _withClassPrefix['has-title'] = header, _withClassPrefix['has-icon'] = showIcon, _withClassPrefix)));

  var handleClose = function handleClose(event) {
    setDisplay('hiding');
    onClose === null || onClose === void 0 ? void 0 : onClose(event);
    clear();
    setTimeout(function () {
      setDisplay('hide');
    }, 1000);
  };

  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
    role: "alert"
  }, rest, {
    ref: ref,
    className: classes
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: prefix(_templateObject())
  }, closable && /*#__PURE__*/_react.default.createElement(_CloseButton.default, {
    onClick: handleClose
  }), showIcon && /*#__PURE__*/_react.default.createElement("div", {
    className: prefix(_templateObject2())
  }, _utils.MESSAGE_STATUS_ICONS[type]), /*#__PURE__*/_react.default.createElement("div", {
    className: prefix(_templateObject3())
  }, header && /*#__PURE__*/_react.default.createElement("h5", {
    className: prefix(_templateObject4())
  }, header), children && /*#__PURE__*/_react.default.createElement("div", {
    className: prefix(_templateObject5())
  }, children))));
});

Message.displayName = 'Message';
Message.defaultProps = defaultProps;
Message.propTypes = {
  type: _propTypes.default.oneOf(_utils.STATUS),
  className: _propTypes.default.string,
  onClose: _propTypes.default.func,
  closable: _propTypes.default.bool,
  title: _propTypes.default.node,
  description: _propTypes.default.node,
  showIcon: _propTypes.default.bool,
  full: _propTypes.default.bool,
  classPrefix: _propTypes.default.string
};
var _default = Message;
exports.default = _default;