"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "CountUp", {
    enumerable: true,
    get: function() {
        return CountUp;
    }
});
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _to_consumable_array = require("@swc/helpers/_/_to_consumable_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _components = require("@tarojs/components");
var _taro = require("@tarojs/taro");
var _typings = require("../../utils/typings");
var _mergeprops = require("../../utils/merge-props");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    length: 0,
    value: '',
    delay: 300,
    duration: 1,
    thousands: false
});
var CountUp = function CountUp(props) {
    var _mergeProps = (0, _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 = (0, _object_without_properties._)(_mergeProps, [
        "length",
        "value",
        "delay",
        "duration",
        "className",
        "thousands",
        "style"
    ]);
    var classPrefix = 'nut-countup';
    var countupRef = (0, _react.useRef)(null);
    var timerRef = (0, _react.useRef)();
    var numbers = Array.from({
        length: 10
    }, function(v, i) {
        return i;
    });
    var getShowNumber = (0, _react.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 = (0, _sliced_to_array._)((0, _react.useState)([]), 2), numberArr = _useState[0], setNumberArr = _useState[1];
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)([]), 2), transformArr = _useState1[0], setTransformArr = _useState1[1];
    var isLoaded = (0, _react.useRef)(false);
    var setNumberTransform = (0, _react.useCallback)(function() {
        if (countupRef.current && numberArr.length) {
            var _countupRef_current;
            var query = (0, _taro.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((0, _to_consumable_array._)(transformArrCache));
            }).exec();
        }
    }, [
        duration,
        numberArr
    ]);
    (0, _react.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
    ]);
    (0, _react.useEffect)(function() {
        setNumberArr(getShowNumber());
    }, [
        value,
        getShowNumber
    ]);
    return /*#__PURE__*/ _react.default.createElement(_components.View, (0, _object_spread._)({
        className: "".concat(classPrefix, " ").concat(className),
        ref: countupRef
    }, rest), /*#__PURE__*/ _react.default.createElement(_components.View, {
        className: "".concat(classPrefix, "-list")
    }, numberArr.map(function(item, idx) {
        return /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "".concat(classPrefix, "-listitem ").concat(!Number.isNaN(Number(item)) ? "".concat(classPrefix, "-listitem-number") : ''),
            key: idx
        }, !Number.isNaN(Number(item)) ? /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "".concat(classPrefix, "-number"),
            style: transformArr === null || transformArr === void 0 ? void 0 : transformArr[idx]
        }, (0, _to_consumable_array._)(numbers).concat((0, _to_consumable_array._)(numbers)).map(function(number, subidx) {
            return /*#__PURE__*/ _react.default.createElement(_components.Text, {
                className: "".concat(classPrefix, "-number-text"),
                key: subidx
            }, number);
        })) : /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "".concat(classPrefix, "-separator")
        }, item));
    })));
};
CountUp.displayName = 'NutCountUp';
