//
// Copyright 2025 DXOS.org
//
import React, { useMemo } from 'react';
import { Format } from '@dxos/echo/internal';
import { useDevtools, useStream } from '@dxos/react-client/devtools';
import { DynamicTable } from '@dxos/react-ui-table';
import { useDevtoolsDispatch, useDevtoolsState } from '../../../hooks';
import { createTextBitbar } from '../../../util';
export const FeedTable = ({ onSelect }) => {
    const { space } = useDevtoolsState();
    const setContext = useDevtoolsDispatch();
    // TODO(burdon): Constant updates.
    // TODO(burdon): Out of order with main table.
    const feedKeys = [
        ...(space?.internal.data.pipeline?.controlFeeds ?? []),
        ...(space?.internal.data.pipeline?.dataFeeds ?? []),
    ];
    const devtoolsHost = useDevtools();
    const { feeds = [] } = useStream(() => devtoolsHost.subscribeToFeeds({ feedKeys }), {}, [feedKeys]);
    const maxLength = feeds.reduce((max, feed) => (feed?.length > max ? feed.length : max), 0);
    const properties = useMemo(() => [
        { name: 'feedKey', format: Format.TypeFormat.DID },
        { name: 'progress', format: Format.TypeFormat.String, size: 600 },
    ], []);
    const rows = useMemo(() => {
        return feeds.map((feed) => ({
            id: feed.feedKey.toString(),
            feedKey: feed.feedKey.toString(),
            progress: createTextBitbar(feed.downloaded, maxLength),
            _original: { ...feed, maxLength },
        }));
    }, [feeds, maxLength]);
    const handleRowClick = (row) => {
        if (row?._original !== undefined) {
            setContext((ctx) => ({ ...ctx, feedKey: row._original?.feedKey }));
            onSelect?.(row._original);
        }
    };
    const features = useMemo(() => ({ selection: { enabled: true, mode: 'single' } }), []);
    return React.createElement(DynamicTable, { properties: properties, rows: rows, features: features, onRowClick: handleRowClick });
};
//# sourceMappingURL=FeedTable.js.map