"use strict";

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

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

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

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

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

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

var _bem = require("@zarm-design/bem");

var _icons = require("@zarm-design/icons");

var _configProvider = require("../config-provider");

var Message = /*#__PURE__*/(0, _react.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 = (0, _objectWithoutProperties2.default)(props, ["className", "theme", "icon", "hasArrow", "closable", "children", "onClick", "onClose"]);

  var _useState = (0, _react.useState)(true),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      visible = _useState2[0],
      setVisible = _useState2[1];

  var _useContext = (0, _react.useContext)(_configProvider.ConfigContext),
      prefixCls = _useContext.prefixCls;

  if (!visible) return null;
  var bem = (0, _bem.createBEM)('message', {
    prefixCls: prefixCls
  });
  var cls = bem([(_ref = {}, (0, _defineProperty2.default)(_ref, "".concat(theme), !!theme), (0, _defineProperty2.default)(_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.default.createElement("div", (0, _extends2.default)({
    ref: ref,
    className: cls,
    onClick: handleClick
  }, restProps), icon && /*#__PURE__*/_react.default.createElement("div", {
    className: bem('header')
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('icon')
  }, icon)), /*#__PURE__*/_react.default.createElement("div", {
    className: bem('body')
  }, children), (closable || hasArrow) && /*#__PURE__*/_react.default.createElement("div", {
    className: bem('footer')
  }, hasArrow && /*#__PURE__*/_react.default.createElement(_icons.ArrowRight, null), closable && /*#__PURE__*/_react.default.createElement(_icons.Close, {
    onClick: handleClose
  })));
});
Message.displayName = 'Message';
Message.defaultProps = {
  theme: 'primary',
  hasArrow: false,
  closable: false
};
var _default = Message;
exports.default = _default;