//
// Copyright 2025 DXOS.org
//
import React, { useMemo } from 'react';
export const LogPanel = ({ objects }) => {
    const rows = useMemo(() => {
        if (!objects?.length) {
            return [];
        }
        return objects.flatMap((event) => event.logs.map((log, idx) => ({
            id: `${event.id}-${idx}`,
            time: new Date(log.timestamp),
            level: log.level,
            message: log.message,
            context: safeStringify(log.context),
        })));
    }, [objects]);
    return (React.createElement("div", { className: 'flex grow min-bs-0 min-is-0 is-full bs-full overflow-auto' },
        React.createElement("table", { className: 'table-fixed min-w-full text-xs border-collapse' },
            React.createElement("thead", { className: 'sticky top-0 z-10' },
                React.createElement("tr", null,
                    React.createElement("th", { className: 'text-left px-2 py-1 w-40 border border-separator' }, "Started"),
                    React.createElement("th", { className: 'text-left px-2 py-1 w-24 border border-separator' }, "Level"),
                    React.createElement("th", { className: 'text-left px-2 py-1 w-80 border border-separator' }, "Message"),
                    React.createElement("th", { className: 'text-left px-2 py-1 border border-separator' }, "Context"))),
            React.createElement("tbody", null, rows.map((row) => {
                const level = String(row.level).toUpperCase();
                return (React.createElement("tr", { key: row.id, className: 'align-top' },
                    React.createElement("td", { className: 'px-2 py-1 whitespace-nowrap border border-separator' }, row.time.toLocaleString()),
                    React.createElement("td", { className: 'px-2 py-1 font-mono border border-separator' }, level),
                    React.createElement("td", { className: 'px-2 py-1 truncate max-w-[20rem] border border-separator' }, row.message),
                    React.createElement("td", { className: 'px-2 py-1 font-mono text-[10px] whitespace-pre-wrap break-words border border-separator' }, row.context)));
            })))));
};
const safeStringify = (value) => {
    try {
        if (value == null) {
            return '';
        }
        const seen = new WeakSet();
        return JSON.stringify(value, (key, val) => {
            if (typeof val === 'object' && val !== null) {
                if (seen.has(val)) {
                    return '[Circular]';
                }
                seen.add(val);
            }
            return val;
        }, 2);
    }
    catch {
        return '[Unserializable]';
    }
};
//# sourceMappingURL=LogPanel.js.map