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/SchemaPanel/SchemaPanel.tsx
import * as Option from "effect/Option";
import * as SchemaAST from "effect/SchemaAST";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { DXN, Entity, Format, Type } from "@dxos/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 ||= !!Type.getDXN(item)?.toString().match(matcher);
    match ||= !!SchemaAST.getTitleAnnotation(item.ast).pipe(Option.getOrUndefined)?.match(matcher);
    match ||= !!SchemaAST.getDescriptionAnnotation(item.ast).pipe(Option.getOrUndefined)?.match(matcher);
    return match;
  };
};
var useSchemaQuery = (space) => {
  const [schema, setSchema] = useState([]);
  useEffect(() => {
    if (!space) {
      return;
    }
    return space.db.schemaRegistry.query().subscribe((query) => {
      setSchema([
        ...space.internal.db.graph.schemaRegistry.schemas,
        ...query.results
      ]);
    }, {
      fire: true
    });
  }, [
    space
  ]);
  return schema;
};
var SchemaPanel = (props) => {
  const state = useDevtoolsState();
  const space = props.space ?? state.space;
  const schema = useSchemaQuery(space);
  const [filter, setFilter] = useState("");
  const [selected, setSelected] = useState();
  const onNavigate = (dxn) => {
    const selectedSchema = schema.find((item) => Type.getDXN(item) && DXN.equals(Type.getDXN(item), dxn));
    if (selectedSchema) {
      setSelected(() => selectedSchema);
      return;
    }
    const typeDXN = dxn.asTypeDXN();
    if (typeDXN && typeDXN.version === void 0) {
      const latestSchema = schema.find((item) => Type.getDXN(item)?.toString().startsWith(dxn.toString()));
      if (latestSchema) {
        setSelected(() => latestSchema);
      }
    }
  };
  const itemSelect = (item) => {
    setSelected(() => item);
  };
  const dataProperties = useMemo(() => [
    {
      name: "typename",
      format: Format.TypeFormat.String
    },
    {
      name: "version",
      format: Format.TypeFormat.String,
      size: 100
    },
    {
      name: "kind",
      format: Format.TypeFormat.SingleSelect,
      size: 100,
      config: {
        options: [
          {
            id: "object",
            title: "OBJECT",
            color: "indigo"
          },
          {
            id: "relation",
            title: "RELATION",
            color: "green"
          }
        ]
      }
    }
  ], []);
  const dataRows = useMemo(() => {
    return schema.filter(textFilter(filter)).map((item) => ({
      id: Type.getDXN(item),
      typename: Type.getTypename(item) ?? "",
      version: Type.getVersion(item) ?? "",
      kind: Entity.getKind(item),
      _original: item
    })).toSorted((a, b) => (a.id?.toString() ?? "").localeCompare(b.id?.toString() ?? ""));
  }, [
    schema,
    filter
  ]);
  const handleObjectRowClicked = useCallback((row) => {
    if (!row) {
      setSelected(void 0);
      return;
    }
    itemSelect(row._original);
  }, []);
  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: ", dataRows.length))), /* @__PURE__ */ React.createElement("div", {
    className: "min-bs-0 bs-full !border-separator border-is border-bs"
  }, /* @__PURE__ */ React.createElement("div", {
    className: mx("p-1 min-bs-0 bs-full overflow-auto")
  }, selected ? /* @__PURE__ */ React.createElement(ObjectViewer, {
    object: Type.toJsonSchema(selected),
    id: Type.getDXN(selected)?.toString(),
    onNavigate
  }) : /* @__PURE__ */ React.createElement(Placeholder, {
    label: "Data"
  })))));
};

// src/panels/echo/SchemaPanel/index.ts
var SchemaPanel_default = SchemaPanel;
export {
  SchemaPanel_default as default
};
//# sourceMappingURL=SchemaPanel-O62IO5T2.mjs.map
