import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
import {
  DataSpaceSelector,
  MasterDetailTable,
  PanelContainer,
  Searchbar,
  useDevtoolsState
} from "./chunk-DWYIZZUB.mjs";

// src/panels/echo/AutomergePanel/AutomergePanel.tsx
import React, { useMemo, useState } from "react";
import { Format } from "@dxos/echo/internal";
import { useClient } from "@dxos/react-client";
import { Toolbar } from "@dxos/react-ui";
var 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));
};
var 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);
  };
};
var getStoredObject = (doc) => {
  const [[id, object]] = Object.entries(doc?.objects ?? {});
  return {
    id,
    object
  };
};
var getStoredObjectType = (data) => {
  return isSpaceRoot(data.accessor()) ? "" : getStoredObject(data.accessor())?.object?.system?.type?.["/"];
};
var 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 /* @__PURE__ */ React.createElement(PanelContainer, {
    toolbar: /* @__PURE__ */ React.createElement(Toolbar.Root, null, !props.space && /* @__PURE__ */ React.createElement(DataSpaceSelector, null), /* @__PURE__ */ React.createElement(Searchbar, {
      onChange: setFilter
    }))
  }, /* @__PURE__ */ React.createElement(MasterDetailTable, {
    properties,
    data,
    detailsTransform: ({ accessor }) => accessor()
  }));
};
var isSpaceRoot = (accessor) => {
  return Object.keys(accessor?.links ?? {}).length > 0;
};

// src/panels/echo/AutomergePanel/index.ts
var AutomergePanel_default = AutomergePanel;
export {
  AutomergePanel_default as default
};
//# sourceMappingURL=AutomergePanel-P33RZHSN.mjs.map
