"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 _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

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

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

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

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

var _message = _interopRequireDefault(require("../message"));

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

var _hooks = _interopRequireDefault(require("./hooks"));

var NoticeBar = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var children = props.children,
      speed = props.speed,
      delay = props.delay,
      onClose = props.onClose,
      className = props.className,
      style = props.style,
      restProps = (0, _objectWithoutProperties2.default)(props, ["children", "speed", "delay", "onClose", "className", "style"]);

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

  var _React$useContext = _react.default.useContext(_configProvider.ConfigContext),
      prefixCls = _React$useContext.prefixCls;

  var bem = (0, _bem.createBEM)('notice-bar', {
    prefixCls: prefixCls
  });
  var NOTICEBAR_KEYFRAME_NAME = "".concat(prefixCls, "-notice-bar-scrolling");

  var noticeBarRef = ref || /*#__PURE__*/_react.default.createRef();

  var contentRef = (0, _react.useRef)(null);
  var animationDuration = (0, _hooks.default)({
    noticeBarRef: noticeBarRef,
    contentRef: contentRef,
    delay: delay,
    speed: speed,
    keyframeName: NOTICEBAR_KEYFRAME_NAME
  });
  if (!visible) return null;

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

  var cls = bem([className]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: cls,
    style: style,
    ref: noticeBarRef
  }, /*#__PURE__*/_react.default.createElement(_message.default, (0, _extends2.default)({}, restProps, {
    onClose: handleClose
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: bem('body')
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('content'),
    ref: contentRef,
    style: animationDuration > 0 ? {
      WebkitAnimation: "".concat(NOTICEBAR_KEYFRAME_NAME, " ").concat(animationDuration, "ms linear infinite"),
      animation: "".concat(NOTICEBAR_KEYFRAME_NAME, " ").concat(animationDuration, "ms linear infinite")
    } : undefined
  }, children))));
});
NoticeBar.displayName = 'NoticeBar';
NoticeBar.defaultProps = {
  theme: 'warning',
  icon: /*#__PURE__*/_react.default.createElement(_icons.Volume, null),
  hasArrow: false,
  closable: false,
  speed: 50,
  delay: 2000
};
var _default = NoticeBar;
exports.default = _default;