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 _templateObject2() {
  var data = _taggedTemplateLiteralLoose(["content"]);

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

  return data;
}

function _templateObject() {
  var data = _taggedTemplateLiteralLoose(["title-with-icon"]);

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

  return data;
}

import React, { useCallback, useState } from 'react';
import PropTypes from 'prop-types';
import { useClassNames, useTimeout, MESSAGE_STATUS_ICONS } from '../utils';
import CloseButton from '../CloseButton';
var defaultProps = {
  as: 'div',
  classPrefix: 'notification',
  duration: 4500
};
var Notification = /*#__PURE__*/React.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 = _objectWithoutPropertiesLoose(props, ["as", "classPrefix", "closable", "duration", "className", "type", "header", "children", "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; // Click to trigger to close the message


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

    return /*#__PURE__*/React.createElement("div", {
      className: prefix('title')
    }, type ? /*#__PURE__*/React.createElement("div", {
      className: prefix(_templateObject())
    }, MESSAGE_STATUS_ICONS[type], header) : /*#__PURE__*/React.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.createElement(Component, _extends({
    role: "alert"
  }, rest, {
    ref: ref,
    className: classes
  }), /*#__PURE__*/React.createElement("div", {
    className: prefix(_templateObject2())
  }, renderHeader(), /*#__PURE__*/React.createElement("div", {
    className: prefix('description')
  }, typeof children === 'function' ? children() : children)), closable && /*#__PURE__*/React.createElement(CloseButton, {
    onClick: handleClose
  }));
});
Notification.displayName = 'Notification';
Notification.defaultProps = defaultProps;
Notification.propTypes = {
  as: PropTypes.elementType,
  duration: PropTypes.number,
  header: PropTypes.node,
  closable: PropTypes.bool,
  classPrefix: PropTypes.string,
  className: PropTypes.string,
  type: PropTypes.oneOf(['info', 'success', 'warning', 'error']),
  onClose: PropTypes.func
};
export default Notification;