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 React, { useEffect, useState } from "react";
import classNames from "classnames";
import { View } from "@tarojs/components";
import { ComponentDefaults } from "../../utils/typings";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    rows: 1,
    animated: true,
    visible: false,
    size: 'normal',
    shape: 'round',
    duration: 0.6
});
export var Skeleton = function(props) {
    var shapeStyle = function shapeStyle() {
        if (shape === 'circle') return {
            borderRadius: '50%'
        };
        if (shape === 'square') return {
            borderRadius: '0'
        };
        return {};
    };
    var durationStyle = function durationStyle() {
        if (typeof duration !== 'undefined') return {
            animationDuration: "".concat(duration, "s")
        };
        return {};
    };
    var _ref = _object_spread({}, defaultProps, props), className = _ref.className, width = _ref.width, height = _ref.height, shape = _ref.shape, animated = _ref.animated, rows = _ref.rows, visible = _ref.visible, size = _ref.size, duration = _ref.duration, children = _ref.children, rest = _object_without_properties(_ref, [
        "className",
        "width",
        "height",
        "shape",
        "animated",
        "rows",
        "visible",
        "size",
        "duration",
        "children"
    ]);
    var classPrefix = 'nut-skeleton';
    var classes = classNames(classPrefix, className);
    var repeatCount = function(num) {
        return Array.from({
            length: num
        }, function(v, i) {
            return i;
        });
    };
    var _useState = _sliced_to_array(useState(false), 2), animate = _useState[0], setAnimate = _useState[1];
    var playAnimation = function() {
        setAnimate(false);
        setTimeout(function() {
            setAnimate(true);
        }, 10);
    };
    useEffect(function() {
        if (!animated) return;
        playAnimation();
        // 每隔 3 秒播放一次动画
        var intervalId = setInterval(playAnimation, 1000 + duration * 1000) // xs 动画 + 1s 间隔
        ;
        // 清理定时器
        return function() {
            return clearInterval(intervalId);
        };
    }, []);
    return /*#__PURE__*/ React.createElement(React.Fragment, null, visible ? children : /*#__PURE__*/ React.createElement(View, _object_spread({
        className: classes
    }, rest), repeatCount(rows).map(function(item, index) {
        var contentClass = "".concat(classPrefix, "-content ").concat(classPrefix, "-content-").concat(size, " ").concat(classPrefix, "-content-").concat(size, "-").concat(index);
        return /*#__PURE__*/ React.createElement(View, {
            className: "".concat(contentClass),
            key: index,
            style: _object_spread({
                width: width,
                height: height
            }, shapeStyle())
        }, animated && /*#__PURE__*/ React.createElement(View, {
            className: "".concat(classPrefix, "-animated ").concat(animate ? "".concat(classPrefix, "-animation") : ''),
            style: durationStyle()
        }));
    })));
};
Skeleton.displayName = 'NutSkeleton';
