import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
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 _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useEffect, useRef, useImperativeHandle } from "react";
import classNames from "classnames";
import { View } from "@tarojs/components";
import { ComponentDefaults } from "../../utils/typings";
import { getRectInMultiPlatform } from "../../utils/taro/get-rect";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    list: [],
    interval: 500,
    loop: true,
    duration: 3000,
    rows: 3,
    gapY: 10
});
var classPrefix = "nut-barrage";
var InternalBarrage = function(props, ref) {
    var _ref = _object_spread({}, defaultProps, props), className = _ref.className, interval = _ref.interval, loop = _ref.loop, list = _ref.list, duration = _ref.duration, rows = _ref.rows, gapY = _ref.gapY, restProps = _object_without_properties(_ref, [
        "className",
        "interval",
        "loop",
        "list",
        "duration",
        "rows",
        "gapY"
    ]);
    var barrageBody = useRef(null);
    var barrageContainer = useRef(null);
    var barrageCWidth = useRef(0);
    var timer = useRef(0);
    var domTimer = useRef(0);
    var index = useRef(0);
    var times = useRef([]);
    var historyIndex = useRef(-1);
    var classes = classNames(classPrefix, className);
    useImperativeHandle(ref, function() {
        return {
            add: function(word) {
                var _index = index.current % list.length;
                if (!loop && index.current === list.length) {
                    list.splice(list.length, 0, word);
                } else {
                    list.splice(_index, 0, word);
                }
            }
        };
    });
    var getNodeWidth = function(node) {
        var type = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 'width';
        return _async_to_generator(function() {
            var refe;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        if (!node) return [
                            3,
                            2
                        ];
                        return [
                            4,
                            getRectInMultiPlatform(node)
                        ];
                    case 1:
                        refe = _state.sent();
                        return [
                            2,
                            Math.ceil(type === 'height' ? refe.height : refe.width)
                        ];
                    case 2:
                        return [
                            2,
                            0
                        ];
                }
            });
        })();
    };
    var clearDomTimeout = function() {
        if (domTimer.current) {
            clearTimeout(domTimer.current);
            domTimer.current = 0;
        }
    };
    useEffect(function() {
        var init = function() {
            return _async_to_generator(function() {
                var _barrageBody_current;
                return _ts_generator(this, function(_state) {
                    switch(_state.label){
                        case 0:
                            if (!barrageBody.current) return [
                                3,
                                2
                            ];
                            return [
                                4,
                                getNodeWidth(barrageBody.current)
                            ];
                        case 1:
                            barrageCWidth.current = _state.sent();
                            (_barrageBody_current = barrageBody.current) === null || _barrageBody_current === void 0 ? void 0 : _barrageBody_current.style.setProperty('--move-distance', "-".concat(barrageCWidth.current, "px"));
                            index.current = 0;
                            run();
                            _state.label = 2;
                        case 2:
                            return [
                                2
                            ];
                    }
                });
            })();
        };
        domTimer.current = window.setTimeout(function() {
            init();
        }, 300);
        return function() {
            clearDomTimeout();
        };
    }, [
        list
    ]);
    var run = function() {
        clearTimeout(timer.current);
        var intervalCache = interval;
        var _index = (loop ? index.current % list.length : index.current) % rows;
        var result = times.current[_index] - rows * interval;
        if (result > 0) {
            intervalCache += result;
        }
        timer.current = window.setTimeout(function() {
            play();
        }, intervalCache);
    };
    var setStyle = function(el, currentIndex) {
        return _async_to_generator(function() {
            var refe, width, height, elScrollDuration, error;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        _state.trys.push([
                            0,
                            3,
                            ,
                            4
                        ]);
                        if (!el) return [
                            3,
                            2
                        ];
                        return [
                            4,
                            getRectInMultiPlatform(el)
                        ];
                    case 1:
                        refe = _state.sent();
                        width = refe.width;
                        height = refe.height;
                        el.classList.add('move');
                        elScrollDuration = Math.round(width / barrageCWidth.current * duration);
                        times.current[currentIndex] = elScrollDuration;
                        el.style.animationDuration = "".concat(duration + elScrollDuration, "ms");
                        el.style.top = "".concat(currentIndex * (height + gapY) + 20, "px");
                        el.style.width = "".concat(width, "px");
                        _state.label = 2;
                    case 2:
                        return [
                            3,
                            4
                        ];
                    case 3:
                        error = _state.sent();
                        console.log('异常自动流转到下一个', error);
                        barrageContainer.current.removeChild(el);
                        return [
                            3,
                            4
                        ];
                    case 4:
                        el.addEventListener('animationend', function() {
                            ;
                            barrageContainer.current.removeChild(el);
                        });
                        return [
                            2
                        ];
                }
            });
        })();
    };
    var play = function() {
        if (!loop && index.current >= list.length) {
            return;
        }
        var _index = loop ? index.current % list.length : index.current;
        var el = document.createElement("View");
        var currentIndex = _index % rows;
        if (currentIndex <= historyIndex.current || historyIndex.current === 3 && currentIndex !== 0 || Math.abs(currentIndex - historyIndex.current) !== 1) {
            currentIndex = historyIndex.current + 1 >= rows ? 0 : historyIndex.current + 1;
        }
        historyIndex.current = currentIndex;
        el.innerHTML = list[_index];
        barrageContainer.current.appendChild(el);
        el.classList.add('barrage-item');
        requestAnimationFrame(function() {
            return setStyle(el, currentIndex);
        });
        index.current++;
        run();
    };
    return /*#__PURE__*/ React.createElement(View, _object_spread({
        className: classes,
        ref: barrageBody
    }, restProps), /*#__PURE__*/ React.createElement(View, {
        ref: barrageContainer,
        className: "bContainer"
    }));
};
export var Barrage = /*#__PURE__*/ React.forwardRef(InternalBarrage);
Barrage.displayName = 'NutBarrage';
