import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import React, { forwardRef, useState, useContext } from 'react';
import { createBEM } from '@zarm-design/bem';
import { ArrowRight as ArrowRightIcon, Close as CloseIcon } from '@zarm-design/icons';
import { ConfigContext } from '../config-provider';
var Message = /*#__PURE__*/forwardRef(function (props, ref) {
  var _ref;

  var className = props.className,
      theme = props.theme,
      icon = props.icon,
      hasArrow = props.hasArrow,
      closable = props.closable,
      children = props.children,
      onClick = props.onClick,
      onClose = props.onClose,
      restProps = _objectWithoutProperties(props, ["className", "theme", "icon", "hasArrow", "closable", "children", "onClick", "onClose"]);

  var _useState = useState(true),
      _useState2 = _slicedToArray(_useState, 2),
      visible = _useState2[0],
      setVisible = _useState2[1];

  var _useContext = useContext(ConfigContext),
      prefixCls = _useContext.prefixCls;

  if (!visible) return null;
  var bem = createBEM('message', {
    prefixCls: prefixCls
  });
  var cls = bem([(_ref = {}, _defineProperty(_ref, "".concat(theme), !!theme), _defineProperty(_ref, "link", hasArrow), _ref), className]);

  function handleClick(e) {
    if (!hasArrow) {
      e.preventDefault();
      return;
    }

    onClick === null || onClick === void 0 ? void 0 : onClick(e);
  }

  function handleClose(e) {
    setVisible(false);
    onClose === null || onClose === void 0 ? void 0 : onClose(e);
  }

  return /*#__PURE__*/React.createElement("div", _extends({
    ref: ref,
    className: cls,
    onClick: handleClick
  }, restProps), icon && /*#__PURE__*/React.createElement("div", {
    className: bem('header')
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('icon')
  }, icon)), /*#__PURE__*/React.createElement("div", {
    className: bem('body')
  }, children), (closable || hasArrow) && /*#__PURE__*/React.createElement("div", {
    className: bem('footer')
  }, hasArrow && /*#__PURE__*/React.createElement(ArrowRightIcon, null), closable && /*#__PURE__*/React.createElement(CloseIcon, {
    onClick: handleClose
  })));
});
Message.displayName = 'Message';
Message.defaultProps = {
  theme: 'primary',
  hasArrow: false,
  closable: false
};
export default Message;