//
// Copyright 2024 DXOS.org
//
import React, { useMemo, useState } from 'react';
import { Format } from '@dxos/echo/internal';
import { useClient } from '@dxos/react-client';
import { Toolbar } from '@dxos/react-ui';
import { MasterDetailTable, PanelContainer, Searchbar } from '../../../components';
import { DataSpaceSelector } from '../../../containers';
import { useDevtoolsState } from '../../../hooks';
const hasMatchRecursive = (object, pattern) => {
    if (!object) {
        return false;
    }
    if (typeof object !== 'object') {
        return Boolean(String(object).match(pattern));
    }
    if (Array.isArray(object)) {
        return object.some((element) => hasMatchRecursive(element, pattern));
    }
    return Object.values(object).some((element) => hasMatchRecursive(element, pattern));
};
const textFilter = (text) => {
    if (!text?.length) {
        return () => true;
    }
    const pattern = new RegExp(text, 'i');
    return (item) => {
        if (getStoredObjectType(item)?.toLowerCase()?.endsWith('canvas')) {
            return false;
        }
        return hasMatchRecursive(getStoredObject(item.accessor()), pattern);
    };
};
const getStoredObject = (doc) => {
    const [[id, object]] = Object.entries(doc?.objects ?? {});
    return { id, object };
};
const getStoredObjectType = (data) => {
    return isSpaceRoot(data.accessor()) ? '' : getStoredObject(data.accessor())?.object?.system?.type?.['/'];
};
export const AutomergePanel = (props) => {
    const state = useDevtoolsState();
    const space = props.space ?? state.space;
    const client = useClient();
    const handles = (space &&
        client.spaces
            .get()
            .find((s) => s.id === space.id)
            ?.internal.db.coreDatabase?.getLoadedDocumentHandles()) ??
        [];
    const [filter, setFilter] = useState('');
    const properties = useMemo(() => [
        { name: 'documentId', format: Format.TypeFormat.DID, size: 320 },
        { name: 'content', format: Format.TypeFormat.String, size: 320 },
        { name: 'type', format: Format.TypeFormat.String, size: 320 },
    ], []);
    const data = useMemo(() => {
        return handles
            .map((handle) => {
            const doc = handle.doc();
            return {
                id: handle.documentId,
                documentId: handle.documentId,
                content: isSpaceRoot(doc) ? 'space root doc' : getStoredObject(doc)?.id,
                type: getStoredObjectType({ documentId: handle.documentId, accessor: () => doc, id: handle.documentId }),
                accessor: () => doc,
            };
        })
            .filter((item) => item.id != null)
            .filter(textFilter(filter));
    }, [handles, filter]);
    return (React.createElement(PanelContainer, { toolbar: React.createElement(Toolbar.Root, null,
            !props.space && React.createElement(DataSpaceSelector, null),
            React.createElement(Searchbar, { onChange: setFilter })) },
        React.createElement(MasterDetailTable, { properties: properties, data: data, detailsTransform: ({ accessor }) => accessor() })));
};
const isSpaceRoot = (accessor) => {
    return Object.keys(accessor?.links ?? {}).length > 0;
};
//# sourceMappingURL=AutomergePanel.js.map