function _array_like_to_array(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 _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    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 _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(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(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React, { useEffect, useMemo, useRef, useState } from "react";
import { DEFAULT_TEXT_SWAP_DURATION_MS, TEXT_SWAP_EASING } from "../Components/TextSwap/constants";
import { textSwapEnterAnimationForwards } from "../Components/TextSwap/textSwapMotion";
var extractText = function extractText1(children) {
    var _children_props;
    if (typeof children === 'string') return children;
    if (typeof children === 'number') return String(children);
    if (Array.isArray(children)) return children.map(extractText).join('');
    if (/*#__PURE__*/ React.isValidElement(children) && ((_children_props = children.props) === null || _children_props === void 0 ? void 0 : _children_props.children)) {
        return extractText(children.props.children);
    }
    return '';
};
/** 流式同一段内：前后文案仅「前缀关系」变化（增长或尾部截断修正） */ var isStreamingCompatible = function isStreamingCompatible(prev, next) {
    return prev.startsWith(next) || next.startsWith(prev);
};
function prefersReducedMotion() {
    if (typeof window === 'undefined' || !window.matchMedia) {
        return false;
    }
    try {
        return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    } catch (unused) {
        return false;
    }
}
/** 流式文字淡入，前缀追加只触发一次入场，非前缀替换时重播 */ var AnimationText = /*#__PURE__*/ React.memo(function(param) {
    var children = param.children, animationConfig = param.animationConfig;
    var _ref = animationConfig || {}, _ref_fadeDuration = _ref.fadeDuration, fadeDuration = _ref_fadeDuration === void 0 ? DEFAULT_TEXT_SWAP_DURATION_MS : _ref_fadeDuration, _ref_easing = _ref.easing, easing = _ref_easing === void 0 ? TEXT_SWAP_EASING : _ref_easing;
    var _useState = _sliced_to_array(useState(false), 2), animComplete = _useState[0], setAnimComplete = _useState[1];
    var _useState1 = _sliced_to_array(useState(0), 2), animSession = _useState1[0], setAnimSession = _useState1[1];
    var prevTextRef = useRef('');
    var text = extractText(children);
    useEffect(function() {
        if (text === prevTextRef.current) return;
        var prev = prevTextRef.current;
        if (!prev) {
            if (!text) return;
            prevTextRef.current = text;
            setAnimComplete(false);
            return;
        }
        if (isStreamingCompatible(prev, text)) {
            prevTextRef.current = text;
            return;
        }
        setAnimComplete(false);
        setAnimSession(function(s) {
            return s + 1;
        });
        prevTextRef.current = text;
    }, [
        text
    ]);
    var handleAnimationEnd = function handleAnimationEnd() {
        return setAnimComplete(true);
    };
    var animationStyle = useMemo(function() {
        return {
            display: 'inline-block',
            animation: textSwapEnterAnimationForwards(fadeDuration, easing),
            willChange: 'opacity, transform, filter',
            color: 'inherit'
        };
    }, [
        fadeDuration,
        easing
    ]);
    var doneChunkStyle = useMemo(function() {
        return {
            display: 'contents'
        };
    }, []);
    if (prefersReducedMotion()) {
        return /*#__PURE__*/ React.createElement("span", {
            style: doneChunkStyle
        }, children);
    }
    return animComplete ? /*#__PURE__*/ React.createElement("span", {
        style: doneChunkStyle
    }, children) : /*#__PURE__*/ React.createElement("span", {
        key: animSession,
        style: animationStyle,
        onAnimationEnd: handleAnimationEnd
    }, children);
});
AnimationText.displayName = 'AnimationText';
export default AnimationText;
