var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
import React, { useRef, useEffect, useImperativeHandle, forwardRef, useContext } from "react";
import classnames from "classnames";
import Icon from "../icon";
import { getRect } from "../hooks/get-rect";
import useEventListener from "../hooks/use-event-listener";
import { isDef, noop } from "../utils";
import { raf, doubleRaf } from "../utils/raf";
import { useSetState, useUpdateEffect } from "../hooks";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
import PopupContext from "../popup/PopupContext";
const NoticeBar = /* @__PURE__ */ forwardRef((props, ref) => {
  const {
    text,
    color,
    background,
    wrapable,
    scrollable,
    speed,
    delay = 1
  } = props;
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const popupContext = useContext(PopupContext);
  const [bem] = createNamespace("notice-bar", prefixCls);
  const [state, setState] = useSetState({
    show: true,
    offset: 0,
    duration: 0
  });
  const wrapRef = useRef();
  const contentRef = useRef();
  const wrapWidth = useRef(0);
  const contentWidth = useRef(0);
  const startTimer = useRef(null);
  const renderLeftIcon = () => {
    if (typeof props.leftIcon !== "string" && /* @__PURE__ */ React.isValidElement(props.leftIcon)) {
      return props.leftIcon;
    }
    if (props.leftIcon) {
      return /* @__PURE__ */ React.createElement(Icon, {
        className: classnames(bem("left-icon")),
        name: props.leftIcon
      });
    }
    return null;
  };
  const getRightIconName = () => {
    if (props.mode === "closeable") {
      return "cross";
    }
    if (props.mode === "link") {
      return "arrow";
    }
    return "";
  };
  const onClickRightIcon = (event) => {
    if (props.mode === "closeable") {
      setState({
        show: false
      });
      if (props.onClose) {
        props.onClose(event);
      }
    }
  };
  const renderRightIcon = () => {
    if (props.rightIcon) {
      return props.rightIcon;
    }
    const name = getRightIconName();
    if (name) {
      return /* @__PURE__ */ React.createElement(Icon, {
        name,
        className: classnames(bem("right-icon")),
        onClick: onClickRightIcon
      });
    }
    return null;
  };
  const onTransitionEnd = () => {
    setState({
      offset: wrapWidth.current,
      duration: 0
    });
    raf(() => {
      doubleRaf(() => {
        setState({
          offset: -contentWidth.current,
          duration: (contentWidth.current + wrapWidth.current) / speed
        });
        if (props.onReplay) {
          props.onReplay();
        }
      });
    });
  };
  const renderMarquee = () => {
    const ellipsis = scrollable === false && !props.wrapable;
    const style = {
      transform: state.offset ? `translateX(${state.offset}px)` : "",
      transitionDuration: `${state.duration}s`
    };
    return /* @__PURE__ */ React.createElement("div", {
      className: classnames(bem("wrap")),
      ref: wrapRef
    }, /* @__PURE__ */ React.createElement("div", {
      className: classnames(bem("content"), {
        "rc-ellipsis": ellipsis
      }),
      ref: contentRef,
      style,
      onTransitionEnd
    }, props.children || text));
  };
  const reset = () => {
    const ms = isDef(delay) ? +delay * 1e3 : 0;
    wrapWidth.current = 0;
    contentWidth.current = 0;
    setState({
      offset: 0,
      duration: 0
    });
    clearTimeout(startTimer.current);
    startTimer.current = setTimeout(() => {
      if (!wrapRef.current || !contentRef.current || scrollable === false) {
        return;
      }
      const wrapRefWidth = getRect(wrapRef.current).width;
      const contentRefWidth = getRect(contentRef.current).width;
      if (scrollable || contentRefWidth > wrapRefWidth) {
        doubleRaf(() => {
          wrapWidth.current = wrapRefWidth;
          contentWidth.current = contentRefWidth;
          setState({
            offset: -contentWidth.current,
            duration: contentWidth.current / speed
          });
        });
      }
    }, ms);
  };
  useEventListener("pageshow", reset);
  useEffect(() => {
    reset();
  }, [text, scrollable]);
  useUpdateEffect(() => {
    reset();
  }, [popupContext.visible]);
  useImperativeHandle(ref, () => ({
    reset
  }));
  return state.show && /* @__PURE__ */ React.createElement("div", {
    role: "alert",
    className: classnames(bem({
      wrapable
    }), props.className),
    style: __spreadValues({
      color,
      background
    }, props.style),
    onClick: props.onClick
  }, renderLeftIcon(), renderMarquee(), renderRightIcon());
});
NoticeBar.defaultProps = {
  delay: 1,
  speed: 60,
  onClick: noop,
  onClose: noop
};
var stdin_default = NoticeBar;
export {
  stdin_default as default
};
