function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useState, useEffect } from 'react';

// Hook的参数类型定义

/**
 * 一个自定义React Hook，用于实现打字机效果。
 * @param {TypewriterOptions} options - 配置选项
 * @returns {string} 当前应显示的文本
 */
export var useTypewriter = function useTypewriter(_ref) {
  var texts = _ref.texts,
    _ref$typingSpeed = _ref.typingSpeed,
    typingSpeed = _ref$typingSpeed === void 0 ? 150 : _ref$typingSpeed,
    _ref$pauseDelay = _ref.pauseDelay,
    pauseDelay = _ref$pauseDelay === void 0 ? 2000 : _ref$pauseDelay;
  // 当前显示第几个字符串
  var _useState = useState(0),
    _useState2 = _slicedToArray(_useState, 2),
    textIndex = _useState2[0],
    setTextIndex = _useState2[1];
  // 当前字符串显示到第几个字符
  var _useState3 = useState(0),
    _useState4 = _slicedToArray(_useState3, 2),
    charIndex = _useState4[0],
    setCharIndex = _useState4[1];
  useEffect(function () {
    // 获取当前要处理的完整字符串
    var currentText = texts[textIndex];

    // 如果当前字符索引小于字符串长度，说明还在打字过程中
    if (charIndex < currentText.length) {
      var typingTimeout = setTimeout(function () {
        setCharIndex(function (prev) {
          return prev + 1;
        });
      }, typingSpeed);

      // 清除定时器，防止内存泄漏
      return function () {
        return clearTimeout(typingTimeout);
      };
    }
    // 如果字已经打完
    else {
      var pauseTimeout = setTimeout(function () {
        // 切换到下一个字符串
        setTextIndex(function (prev) {
          return (prev + 1) % texts.length;
        });
        // 重置字符索引，从头开始
        setCharIndex(0);
      }, pauseDelay);

      // 清除定时器
      return function () {
        return clearTimeout(pauseTimeout);
      };
    }
  }, [charIndex, textIndex, texts, typingSpeed, pauseDelay]);

  // 根据当前状态截取并返回应该显示的字符串
  return texts[textIndex].substring(0, charIndex);
};