import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
import {
  JsonView,
  PanelContainer,
  Tree
} from "./chunk-SXXO7MP3.mjs";

// src/panels/client/DiagnosticsPanel/DiagnosticsPanel.tsx
import React, { useMemo, useState } from "react";
import { useClient } from "@dxos/react-client";
import { useAsyncEffect } from "@dxos/react-hooks";
import { Icon, Input, Toolbar, useFileDownload } from "@dxos/react-ui";
var DiagnosticsPanel = () => {
  const client = useClient();
  const [data, setData] = useState({});
  const handleRefresh = async () => {
    try {
      setData({
        status: "Pending..."
      });
      const data2 = await client.diagnostics({
        humanize: false,
        truncate: true
      });
      setData(data2);
    } catch (err) {
      setData({
        status: err.message
      });
    }
  };
  const [recording, setRecording] = useState(false);
  useAsyncEffect(async () => {
    const { recording: recording2 = false } = await client.services.services.LoggingService.controlMetrics({});
    setRecording(recording2);
  }, [
    client
  ]);
  const handleSetRecording = async (record) => {
    const { recording: recording2 = false } = await client.services.services.LoggingService.controlMetrics({
      record
    });
    setRecording(recording2);
  };
  const handleResetMetrics = async () => {
    const { recording: recording2 = false } = await client.services.services.LoggingService.controlMetrics({
      reset: true
    });
    setRecording(recording2);
    await handleRefresh();
  };
  const fileDownload = useFileDownload();
  const handleDownload = async () => {
    fileDownload(new Blob([
      JSON.stringify(data, void 0, 2)
    ], {
      type: "text/plain"
    }), `${(/* @__PURE__ */ new Date()).toISOString().replace(/\W/g, "-")}.json`);
  };
  const info = useMemo(() => {
    if (window.chrome) {
      return [
        "chrome://inspect/#workers"
      ];
    }
  }, []);
  const handleCopy = async (text) => {
    await navigator.clipboard.writeText(text);
  };
  return /* @__PURE__ */ React.createElement(PanelContainer, {
    toolbar: /* @__PURE__ */ React.createElement(Toolbar.Root, null, /* @__PURE__ */ React.createElement(Input.Root, null, /* @__PURE__ */ React.createElement(Input.Checkbox, {
      checked: recording,
      onCheckedChange: (recording2) => handleSetRecording(!!recording2)
    }), /* @__PURE__ */ React.createElement(Input.Label, null, "Record metrics")), /* @__PURE__ */ React.createElement("div", {
      className: "grow"
    }), /* @__PURE__ */ React.createElement(Toolbar.Button, {
      onClick: handleRefresh
    }, "Run Diagnostics"), /* @__PURE__ */ React.createElement(Toolbar.IconButton, {
      icon: "ph--download--regular",
      label: "Download diagnostics",
      onClick: handleDownload
    }), /* @__PURE__ */ React.createElement(Toolbar.Button, {
      onClick: handleResetMetrics
    }, "Reset metrics")),
    footer: info && /* @__PURE__ */ React.createElement("div", {
      className: "flex p-2 items-center text-sm font-mono gap-2"
    }, info.map((text, i) => /* @__PURE__ */ React.createElement("div", {
      key: i,
      className: "inline-flex items-center gap-1 cursor-pointer",
      onClick: () => handleCopy(text)
    }, /* @__PURE__ */ React.createElement(Icon, {
      icon: "ph--clipboard-text--regular"
    }), text)))
  }, /* @__PURE__ */ React.createElement(JsonView, {
    data
  }) || /* @__PURE__ */ React.createElement(Tree, {
    data
  }));
};

// src/panels/client/DiagnosticsPanel/index.ts
var DiagnosticsPanel_default = DiagnosticsPanel;
export {
  DiagnosticsPanel_default as default
};
//# sourceMappingURL=DiagnosticsPanel-BHODAH5P.mjs.map
