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

// src/panels/echo/ObjectsPanel/ObjectsPanel.tsx
import React, { useCallback, useMemo, useState } from "react";
import { Filter, Format, Obj, Query, Type } from "@dxos/echo";
import { checkoutVersion, getEditHistory } from "@dxos/echo-db";
import { useQuery } from "@dxos/react-client/echo";
import { Toolbar } from "@dxos/react-ui";
import { DynamicTable } from "@dxos/react-ui-table";
import { mx } from "@dxos/ui-theme";
var textFilter = (text) => {
  if (!text) {
    return () => true;
  }
  const matcher = new RegExp(text, "i");
  return (item) => {
    let match = false;
    match ||= !!Obj.getTypename(item)?.match(matcher);
    match ||= !!String(item.title ?? "").match(matcher);
    return match;
  };
};
var mapHistoryRow = (item) => {
  return {
    hash: item.change.hash,
    actor: item.change.actor,
    time: item.change.time,
    message: item.change.message
  };
};
var ObjectsPanel = (props) => {
  const state = useDevtoolsState();
  const space = props.space ?? state.space;
  const items = useQuery(space?.db, Query.select(Filter.everything()).options({
    deleted: "include"
  }));
  const [filter, setFilter] = useState("");
  const [selected, setSelected] = useState();
  const [selectedVersion, setSelectedVersion] = useState(null);
  const [selectedVersionObject, setSelectedVersionObject] = useState(null);
  const onNavigate = (dxn) => {
    if (dxn.isLocalObjectId()) {
      const [, id] = dxn.parts;
      const object = items.find((item) => item.id === id);
      if (object) {
        setSelectedVersionObject(null);
        setSelected(object);
      }
    }
  };
  const objectSelect = (object) => {
    setSelectedVersionObject(null);
    setSelected(object);
  };
  const history = useMemo(() => {
    return selected ? getEditHistory(selected).map(mapHistoryRow) : [];
  }, [
    selected
  ]);
  const dataProperties = useMemo(() => [
    {
      name: "id",
      format: Format.TypeFormat.DID
    },
    {
      name: "type",
      format: Format.TypeFormat.String
    },
    {
      name: "version",
      format: Format.TypeFormat.String,
      size: 100
    },
    {
      name: "deleted",
      format: Format.TypeFormat.SingleSelect,
      size: 100,
      config: {
        options: [
          {
            id: "DELETED",
            title: "DELETED",
            color: "red"
          }
        ]
      }
    },
    {
      name: "schemaAvailable",
      format: Format.TypeFormat.SingleSelect,
      size: 180,
      config: {
        options: [
          {
            id: "YES",
            title: "YES",
            color: "green"
          },
          {
            id: "NO",
            title: "NO",
            color: "red"
          }
        ]
      }
    }
  ], []);
  const dataRows = useMemo(() => {
    return items.filter(textFilter(filter)).map((item) => ({
      id: item.id,
      type: Obj.getTypename(item),
      version: Obj.getSchema(item) ? Type.getVersion(Obj.getSchema(item)) : void 0,
      deleted: Obj.isDeleted(item) ? "DELETED" : " ",
      schemaAvailable: Obj.getSchema(item) ? "YES" : "NO",
      _original: item
    }));
  }, [
    items,
    filter
  ]);
  const handleObjectRowClicked = useCallback((row) => {
    if (!row) {
      setSelected(void 0);
      setSelectedVersion(null);
      setSelectedVersionObject(null);
      return;
    }
    objectSelect(row._original);
  }, []);
  const historyProperties = useMemo(() => [
    {
      name: "hash",
      format: Format.TypeFormat.JSON
    },
    {
      name: "actor",
      format: Format.TypeFormat.JSON,
      size: 380
    }
  ], []);
  const historyRows = useMemo(() => {
    return history.map((item) => ({
      id: item.hash,
      hash: item.hash.slice(0, 8),
      actor: item.actor
    }));
  }, [
    history,
    selectedVersion
  ]);
  const handleVersionClick = useCallback((version) => {
    setSelectedVersion(version);
    setSelectedVersionObject(checkoutVersion(selected, [
      version.hash
    ]));
  }, [
    selected
  ]);
  const handleHistoryRowClicked = useCallback((row) => {
    if (!row || !selected) {
      setSelectedVersion(null);
      setSelectedVersionObject(null);
      return;
    }
    const versionItem = history.find((item) => item.hash === row.id);
    if (versionItem) {
      handleVersionClick(versionItem);
    }
  }, [
    history,
    handleVersionClick,
    selected
  ]);
  const features = useMemo(() => ({
    selection: {
      enabled: true,
      mode: "single"
    }
  }), []);
  return /* @__PURE__ */ React.createElement(PanelContainer, {
    toolbar: /* @__PURE__ */ React.createElement(Toolbar.Root, null, !props.space && /* @__PURE__ */ React.createElement(DataSpaceSelector, null), /* @__PURE__ */ React.createElement(Searchbar, {
      placeholder: "Filter...",
      onChange: setFilter
    }))
  }, /* @__PURE__ */ React.createElement("div", {
    className: "bs-full grid grid-cols-[4fr_3fr] overflow-hidden"
  }, /* @__PURE__ */ React.createElement("div", {
    className: "flex flex-col is-full overflow-hidden"
  }, /* @__PURE__ */ React.createElement(DynamicTable, {
    properties: dataProperties,
    rows: dataRows,
    features,
    onRowClick: handleObjectRowClicked
  }), /* @__PURE__ */ React.createElement("div", {
    className: mx("bs-[--statusbar-size]", "flex shrink-0 justify-end items-center gap-2", "bg-baseSurface text-description")
  }, /* @__PURE__ */ React.createElement("div", {
    className: "text-sm pie-2"
  }, "Objects: ", items.length))), /* @__PURE__ */ React.createElement("div", {
    className: "min-bs-0 bs-full grid grid-rows-[1fr_16rem] !border-separator border-is border-bs"
  }, /* @__PURE__ */ React.createElement("div", {
    className: mx("p-1 min-bs-0 overflow-auto")
  }, selected ? /* @__PURE__ */ React.createElement(ObjectViewer, {
    object: selectedVersionObject ?? selected,
    id: Obj.getDXN(selected)?.toString(),
    onNavigate
  }) : /* @__PURE__ */ React.createElement(Placeholder, {
    label: "Data"
  })), /* @__PURE__ */ React.createElement("div", {
    className: mx(!selected && "p-1 border-bs !border-separator")
  }, selected ? /* @__PURE__ */ React.createElement(DynamicTable, {
    properties: historyProperties,
    rows: historyRows,
    onRowClick: handleHistoryRowClicked
  }) : /* @__PURE__ */ React.createElement(Placeholder, {
    label: "History"
  })))));
};

// src/panels/echo/ObjectsPanel/index.ts
var ObjectsPanel_default = ObjectsPanel;
export {
  ObjectsPanel_default as default
};
//# sourceMappingURL=ObjectsPanel-USPKXWZC.mjs.map
