"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Skeleton", {
    enumerable: true,
    get: function() {
        return Skeleton;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
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 _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _components = require("@tarojs/components");
var _typings = require("../../utils/typings");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    rows: 1,
    animated: true,
    visible: false,
    size: 'normal',
    shape: 'round',
    duration: 0.6
});
var Skeleton = function Skeleton(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 = (0, _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 = (0, _object_without_properties._)(_ref, [
        "className",
        "width",
        "height",
        "shape",
        "animated",
        "rows",
        "visible",
        "size",
        "duration",
        "children"
    ]);
    var classPrefix = 'nut-skeleton';
    var classes = (0, _classnames.default)(classPrefix, className);
    var repeatCount = function repeatCount(num) {
        return Array.from({
            length: num
        }, function(v, i) {
            return i;
        });
    };
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(false), 2), animate = _useState[0], setAnimate = _useState[1];
    var playAnimation = function playAnimation() {
        setAnimate(false);
        setTimeout(function() {
            setAnimate(true);
        }, 10);
    };
    (0, _react.useEffect)(function() {
        if (!animated) return;
        playAnimation();
        // 每隔 3 秒播放一次动画
        var intervalId = setInterval(playAnimation, 1000 + duration * 1000) // xs 动画 + 1s 间隔
        ;
        // 清理定时器
        return function() {
            return clearInterval(intervalId);
        };
    }, []);
    return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, visible ? children : /*#__PURE__*/ _react.default.createElement(_components.View, (0, _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.default.createElement(_components.View, {
            className: "".concat(contentClass),
            key: index,
            style: (0, _object_spread._)({
                width: width,
                height: height
            }, shapeStyle())
        }, animated && /*#__PURE__*/ _react.default.createElement(_components.View, {
            className: "".concat(classPrefix, "-animated ").concat(animate ? "".concat(classPrefix, "-animation") : ''),
            style: durationStyle()
        }));
    })));
};
Skeleton.displayName = 'NutSkeleton';
