var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
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;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_icon = __toESM(require("../icon"));
var import_get_rect = require("../hooks/get-rect");
var import_use_event_listener = __toESM(require("../hooks/use-event-listener"));
var import_utils = require("../utils");
var import_raf = require("../utils/raf");
var import_hooks = require("../hooks");
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_PopupContext = __toESM(require("../popup/PopupContext"));
const NoticeBar = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    text,
    color,
    background,
    wrapable,
    scrollable,
    speed,
    delay = 1
  } = props;
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const popupContext = (0, import_react.useContext)(import_PopupContext.default);
  const [bem] = createNamespace("notice-bar", prefixCls);
  const [state, setState] = (0, import_hooks.useSetState)({
    show: true,
    offset: 0,
    duration: 0
  });
  const wrapRef = (0, import_react.useRef)();
  const contentRef = (0, import_react.useRef)();
  const wrapWidth = (0, import_react.useRef)(0);
  const contentWidth = (0, import_react.useRef)(0);
  const startTimer = (0, import_react.useRef)(null);
  const renderLeftIcon = () => {
    if (typeof props.leftIcon !== "string" && /* @__PURE__ */ import_react.default.isValidElement(props.leftIcon)) {
      return props.leftIcon;
    }
    if (props.leftIcon) {
      return /* @__PURE__ */ import_react.default.createElement(import_icon.default, {
        className: (0, import_classnames.default)(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__ */ import_react.default.createElement(import_icon.default, {
        name,
        className: (0, import_classnames.default)(bem("right-icon")),
        onClick: onClickRightIcon
      });
    }
    return null;
  };
  const onTransitionEnd = () => {
    setState({
      offset: wrapWidth.current,
      duration: 0
    });
    (0, import_raf.raf)(() => {
      (0, import_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__ */ import_react.default.createElement("div", {
      className: (0, import_classnames.default)(bem("wrap")),
      ref: wrapRef
    }, /* @__PURE__ */ import_react.default.createElement("div", {
      className: (0, import_classnames.default)(bem("content"), {
        "rc-ellipsis": ellipsis
      }),
      ref: contentRef,
      style,
      onTransitionEnd
    }, props.children || text));
  };
  const reset = () => {
    const ms = (0, import_utils.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 = (0, import_get_rect.getRect)(wrapRef.current).width;
      const contentRefWidth = (0, import_get_rect.getRect)(contentRef.current).width;
      if (scrollable || contentRefWidth > wrapRefWidth) {
        (0, import_raf.doubleRaf)(() => {
          wrapWidth.current = wrapRefWidth;
          contentWidth.current = contentRefWidth;
          setState({
            offset: -contentWidth.current,
            duration: contentWidth.current / speed
          });
        });
      }
    }, ms);
  };
  (0, import_use_event_listener.default)("pageshow", reset);
  (0, import_react.useEffect)(() => {
    reset();
  }, [text, scrollable]);
  (0, import_hooks.useUpdateEffect)(() => {
    reset();
  }, [popupContext.visible]);
  (0, import_react.useImperativeHandle)(ref, () => ({
    reset
  }));
  return state.show && /* @__PURE__ */ import_react.default.createElement("div", {
    role: "alert",
    className: (0, import_classnames.default)(bem({
      wrapable
    }), props.className),
    style: __spreadValues({
      color,
      background
    }, props.style),
    onClick: props.onClick
  }, renderLeftIcon(), renderMarquee(), renderRightIcon());
});
NoticeBar.defaultProps = {
  delay: 1,
  speed: 60,
  onClick: import_utils.noop,
  onClose: import_utils.noop
};
var stdin_default = NoticeBar;
