import _extends from "@babel/runtime/helpers/esm/extends";
import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/esm/taggedTemplateLiteralLoose";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";

function _templateObject5() {
  var data = _taggedTemplateLiteralLoose(["body"]);

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

  return data;
}

function _templateObject4() {
  var data = _taggedTemplateLiteralLoose(["header"]);

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

  return data;
}

function _templateObject3() {
  var data = _taggedTemplateLiteralLoose(["content"]);

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

  return data;
}

function _templateObject2() {
  var data = _taggedTemplateLiteralLoose(["icon-wrapper"]);

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

  return data;
}

function _templateObject() {
  var data = _taggedTemplateLiteralLoose(["container"]);

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

  return data;
}

import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { useClassNames, useTimeout, MESSAGE_STATUS_ICONS, STATUS } from '../utils';
import CloseButton from '../CloseButton';
var defaultProps = {
  as: 'div',
  classPrefix: 'message',
  type: 'info',
  duration: 2000
};
var Message = /*#__PURE__*/React.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 = _objectWithoutPropertiesLoose(props, ["as", "className", "classPrefix", "children", "closable", "duration", "full", "header", "type", "showIcon", "onClose"]);

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

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


  var _useTimeout = 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.createElement(Component, _extends({
    role: "alert"
  }, rest, {
    ref: ref,
    className: classes
  }), /*#__PURE__*/React.createElement("div", {
    className: prefix(_templateObject())
  }, closable && /*#__PURE__*/React.createElement(CloseButton, {
    onClick: handleClose
  }), showIcon && /*#__PURE__*/React.createElement("div", {
    className: prefix(_templateObject2())
  }, MESSAGE_STATUS_ICONS[type]), /*#__PURE__*/React.createElement("div", {
    className: prefix(_templateObject3())
  }, header && /*#__PURE__*/React.createElement("h5", {
    className: prefix(_templateObject4())
  }, header), children && /*#__PURE__*/React.createElement("div", {
    className: prefix(_templateObject5())
  }, children))));
});
Message.displayName = 'Message';
Message.defaultProps = defaultProps;
Message.propTypes = {
  type: PropTypes.oneOf(STATUS),
  className: PropTypes.string,
  onClose: PropTypes.func,
  closable: PropTypes.bool,
  title: PropTypes.node,
  description: PropTypes.node,
  showIcon: PropTypes.bool,
  full: PropTypes.bool,
  classPrefix: PropTypes.string
};
export default Message;