var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
import React, { forwardRef, useContext, useEffect, useImperativeHandle, useMemo, useState } from "react";
import classnames from "classnames";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
import RollingTextItem from "./RollingTextItem";
import { padZero } from "../utils";
import { raf } from "../utils/raf";
const CIRCLE_NUM = 2;
const RollingText = /* @__PURE__ */ forwardRef((_a, ref) => {
  var _b = _a, {
    duration,
    direction,
    height,
    textList,
    startNum,
    targetNum,
    autoStart
  } = _b, props = __objRest(_b, [
    "duration",
    "direction",
    "height",
    "textList",
    "startNum",
    "targetNum",
    "autoStart"
  ]);
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("rolling-text", prefixCls);
  const [rolling, setRolling] = useState(autoStart);
  const isCustomType = useMemo(() => Array.isArray(textList) && textList.length, [textList]);
  const itemLength = useMemo(() => {
    if (isCustomType)
      return textList[0].length;
    return `${Math.max(startNum, targetNum)}`.length;
  }, [startNum, targetNum]);
  const getTextArrByIdx = (idx) => {
    const result = [];
    for (let i = 0; i < textList.length; i++) {
      result.push(textList[i][idx]);
    }
    return result;
  };
  const targetNumArr = useMemo(() => {
    if (isCustomType)
      return new Array(itemLength).fill("");
    return padZero(targetNum, itemLength).split("");
  }, [isCustomType, itemLength, targetNum]);
  const startNumArr = useMemo(() => padZero(startNum, itemLength).split(""), [startNum, itemLength]);
  const getFigureArr = (i) => {
    const start2 = +startNumArr[i];
    const target = +targetNumArr[i];
    const result = [];
    for (let index = start2; index <= 9; index++) {
      result.push(index);
    }
    for (let index2 = 0; index2 <= CIRCLE_NUM; index2++) {
      for (let j = 0; j <= 9; j++) {
        result.push(j);
      }
    }
    for (let index3 = 0; index3 <= target; index3++) {
      result.push(index3);
    }
    return result;
  };
  const getDelay = (i, len) => {
    if (props.stopOrder === "ltr")
      return 0.2 * i;
    return 0.2 * (len - 1 - i);
  };
  const start = () => {
    setRolling(true);
  };
  const reset = () => {
    setRolling(false);
    if (autoStart) {
      raf(() => start());
    }
  };
  useEffect(() => {
    if (autoStart) {
      start();
    }
  }, [autoStart]);
  useImperativeHandle(ref, () => ({
    start,
    reset
  }));
  return /* @__PURE__ */ React.createElement("div", {
    className: classnames(bem(""), props.className)
  }, targetNumArr.map((_, i) => /* @__PURE__ */ React.createElement(RollingTextItem, {
    key: String(i),
    figureArr: isCustomType ? getTextArrByIdx(i) : getFigureArr(i),
    duration,
    direction,
    isStart: rolling,
    height,
    delay: getDelay(i, itemLength)
  })));
});
RollingText.defaultProps = {
  height: 40,
  startNum: 0,
  textList: [],
  duration: 2,
  autoStart: true,
  direction: "down",
  stopOrder: "ltr"
};
var stdin_default = RollingText;
export {
  stdin_default as default
};
