"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogsText = void 0;
const react_1 = __importStar(require("react"));
const components_1 = require("../../components");
const inlines_1 = require("inlines");
const Log_1 = require("./Log");
const react_virtual_1 = require("@tanstack/react-virtual");
const LogsText = ({ data, style }) => {
    const [isMouseOver, setIsMouseOver] = (0, react_1.useState)(false);
    const parentRef = (0, react_1.useRef)(null);
    const size = data.length;
    const virtualizer = (0, react_virtual_1.useVirtualizer)({
        count: size,
        getScrollElement: () => parentRef.current,
        estimateSize: () => 20,
    });
    const items = virtualizer.getVirtualItems();
    const [autoScroll, setAutoScroll] = (0, react_1.useState)(true);
    const virtualTotalSize = virtualizer.getTotalSize();
    (0, react_1.useEffect)(() => {
        if (autoScroll && !isMouseOver) {
            setTimeout(() => {
                const element = parentRef.current;
                const elementScrollHeight = parentRef.current.scrollHeight;
                element.scrollTop = elementScrollHeight;
            }, 700);
        }
    }, [virtualTotalSize, parentRef.current]);
    return (react_1.default.createElement(components_1.ScrollArea, { onScroll: (e) => {
            if (isMouseOver) {
                if (e.target.scrollTop >=
                    parentRef.current.scrollHeight - parentRef.current.clientHeight - 10) {
                    setAutoScroll(true);
                }
                else if (e.target.scrollTop <
                    parentRef.current.scrollHeight - parentRef.current.clientHeight - 10) {
                    setAutoScroll(false);
                }
            }
        }, ref: parentRef, style: {
            height: `400px`,
            overflowY: 'auto',
            overflowX: 'clip',
            scrollBehavior: 'smooth',
            width: '100%',
            // ...style,
        }, onMouseOver: () => setIsMouseOver(true), onMouseLeave: () => setIsMouseOver(false) },
        react_1.default.createElement(inlines_1.styled.div, { style: {
                height: virtualizer.getTotalSize(),
                width: '100%',
                position: 'relative',
            } },
            react_1.default.createElement(inlines_1.styled.div, { style: {
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    width: '100%',
                    transform: `translateY(${items[0]?.start ?? 0}px)`,
                } }, items.map((virtualItem) => (react_1.default.createElement(inlines_1.styled.div, { key: virtualItem.key, "data-index": virtualItem.index, ref: virtualizer.measureElement },
                react_1.default.createElement(Log_1.Log, { data: data, index: virtualItem.index, label: data[virtualItem.index].label, ts: data[virtualItem.index].ts, log: data[virtualItem.index].log, type: data[virtualItem.index].type }))))))));
};
exports.LogsText = LogsText;
//# sourceMappingURL=LogsText.js.map