import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
import React, { useEffect, useCallback, useRef, useState } from "react";
import { View, Text } from "@tarojs/components";
import { createSelectorQuery } from "@tarojs/taro";
import { ComponentDefaults } from "../../utils/typings";
import { mergeProps } from "../../utils/merge-props";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    length: 0,
    value: '',
    delay: 300,
    duration: 1,
    thousands: false
});
export var CountUp = function(props) {
    var _mergeProps = mergeProps(defaultProps, props), length = _mergeProps.length, value = _mergeProps.value, delay = _mergeProps.delay, duration = _mergeProps.duration, className = _mergeProps.className, thousands = _mergeProps.thousands, style = _mergeProps.style, rest = _object_without_properties(_mergeProps, [
        "length",
        "value",
        "delay",
        "duration",
        "className",
        "thousands",
        "style"
    ]);
    var classPrefix = 'nut-countup';
    var countupRef = useRef(null);
    var timerRef = useRef();
    var numbers = Array.from({
        length: 10
    }, function(v, i) {
        return i;
    });
    var getShowNumber = useCallback(function() {
        var splitArr = value.split('.');
        var intNumber = length && splitArr[0].length < length ? (Array(length).join('0') + splitArr[0]).slice(-length) : splitArr[0];
        var currNumber = "".concat(thousands ? intNumber.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') : intNumber).concat(splitArr[1] ? '.' : '').concat(splitArr[1] || '');
        return currNumber.split('');
    }, [
        length,
        thousands,
        value
    ]);
    var _useState = _sliced_to_array(useState([]), 2), numberArr = _useState[0], setNumberArr = _useState[1];
    var _useState1 = _sliced_to_array(useState([]), 2), transformArr = _useState1[0], setTransformArr = _useState1[1];
    var isLoaded = useRef(false);
    var setNumberTransform = useCallback(function() {
        if (countupRef.current && numberArr.length) {
            var _countupRef_current;
            var query = createSelectorQuery();
            var scope = (_countupRef_current = countupRef.current) === null || _countupRef_current === void 0 ? void 0 : _countupRef_current._scope;
            var scopedQuery = scope ? query.in(scope) : query;
            scopedQuery.selectAll('.nut-countup-listitem').node(function(numberItems) {
                var transformArrCache = [];
                Object.keys(numberItems).forEach(function(key) {
                    var elem = numberItems[Number(key)];
                    var idx = Number(numberArr[Number(key)]);
                    var enabled = elem && typeof idx === 'number' && !Number.isNaN(idx);
                    if (enabled) {
                        // 计算规则：父元素和实际列表高度的百分比，分割成20等份
                        var transform = idx || idx === 0 ? "translate(0, -".concat((idx === 0 ? 10 : idx) * 5, "%)") : '';
                        transformArrCache.push({
                            transitionDuration: "".concat(duration, "s"),
                            transform: transform
                        });
                    } else {
                        transformArrCache.push({});
                    }
                });
                setTransformArr(_to_consumable_array(transformArrCache));
            }).exec();
        }
    }, [
        duration,
        numberArr
    ]);
    useEffect(function() {
        if (numberArr.length) {
            if (!isLoaded.current) {
                isLoaded.current = true;
                // @ts-ignore
                timerRef.current = setTimeout(function() {
                    setNumberTransform();
                }, delay);
            } else {
                setNumberTransform();
            }
        }
        return function() {
            clearTimeout(timerRef.current);
            isLoaded.current = false;
        };
    }, [
        numberArr,
        delay,
        setNumberTransform
    ]);
    useEffect(function() {
        setNumberArr(getShowNumber());
    }, [
        value,
        getShowNumber
    ]);
    return /*#__PURE__*/ React.createElement(View, _object_spread({
        className: "".concat(classPrefix, " ").concat(className),
        ref: countupRef
    }, rest), /*#__PURE__*/ React.createElement(View, {
        className: "".concat(classPrefix, "-list")
    }, numberArr.map(function(item, idx) {
        return /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-listitem ").concat(!Number.isNaN(Number(item)) ? "".concat(classPrefix, "-listitem-number") : ''),
            key: idx
        }, !Number.isNaN(Number(item)) ? /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-number"),
            style: transformArr === null || transformArr === void 0 ? void 0 : transformArr[idx]
        }, _to_consumable_array(numbers).concat(_to_consumable_array(numbers)).map(function(number, subidx) {
            return /*#__PURE__*/ React.createElement(Text, {
                className: "".concat(classPrefix, "-number-text"),
                key: subidx
            }, number);
        })) : /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-separator")
        }, item));
    })));
};
CountUp.displayName = 'NutCountUp';
