import {
  DataSpaceSelector,
  PanelContainer,
  SyncStateInfo,
  useDevtoolsDispatch,
  useDevtoolsState
} from "./chunk-VBEJ3GU4.mjs";

// src/panels/edge/TestingPanel/TestingPanel.tsx
import React from "react";
import { waitForCondition } from "@dxos/async";
import { DeviceType } from "@dxos/client/halo";
import { log } from "@dxos/log";
import { useClient } from "@dxos/react-client";
import { IconButton, Toolbar } from "@dxos/react-ui";
var __dxlog_file = "/__w/dxos/dxos/packages/devtools/devtools/src/panels/edge/TestingPanel/TestingPanel.tsx";
var TestingPanel = ({ onSpaceCreate, onScriptPluginOpen }) => {
  const client = useClient();
  const { space } = useDevtoolsState();
  const dispatch = useDevtoolsDispatch();
  const handleSpaceCreate = async () => {
    const agentDevice = client.halo.devices.get().find((device) => device.profile?.type === DeviceType.AGENT_MANAGED);
    const agentKey = agentDevice?.deviceKey.toHex();
    if (!agentKey) {
      log.warn("no agent key", void 0, {
        F: __dxlog_file,
        L: 33,
        S: void 0,
        C: (f, a) => f(...a)
      });
      return;
    }
    try {
      const response = await client.edge.createSpace({
        agentKey
      });
      log.info("space created", {
        response
      }, {
        F: __dxlog_file,
        L: 39,
        S: void 0,
        C: (f, a) => f(...a)
      });
      const space2 = await waitForCondition({
        condition: () => client.spaces.get(response.spaceId),
        timeout: 1e4,
        interval: 100
      });
      if (!space2) {
        log.warn("space not found", {
          spaceId: response.spaceId
        }, {
          F: __dxlog_file,
          L: 46,
          S: void 0,
          C: (f, a) => f(...a)
        });
        return;
      }
      await onSpaceCreate?.(space2);
      dispatch((state) => ({
        ...state,
        space: space2
      }));
    } catch (error) {
      log.warn("space creation failed", {
        error
      }, {
        F: __dxlog_file,
        L: 53,
        S: void 0,
        C: (f, a) => f(...a)
      });
    }
  };
  const handleScriptPluginOpen = async () => {
    if (!space) {
      log.warn("no space", void 0, {
        F: __dxlog_file,
        L: 59,
        S: void 0,
        C: (f, a) => f(...a)
      });
      return;
    }
    await onScriptPluginOpen?.(space);
  };
  return /* @__PURE__ */ React.createElement(PanelContainer, {
    classNames: "flex flex-col gap-4 p-4",
    toolbar: /* @__PURE__ */ React.createElement(Toolbar.Root, null, /* @__PURE__ */ React.createElement(DataSpaceSelector, null), /* @__PURE__ */ React.createElement(Toolbar.IconButton, {
      icon: "ph--plus--regular",
      label: "Create Space",
      onClick: handleSpaceCreate
    }))
  }, /* @__PURE__ */ React.createElement(IconButton, {
    icon: "ph--code--regular",
    label: "Open Script Plugin",
    onClick: handleScriptPluginOpen
  }), /* @__PURE__ */ React.createElement("div", {
    className: "border-bs border-separator"
  }, space && /* @__PURE__ */ React.createElement(SyncStateInfo, {
    space
  })));
};

// src/panels/edge/TestingPanel/index.ts
var TestingPanel_default = TestingPanel;
export {
  TestingPanel_default as default
};
//# sourceMappingURL=TestingPanel-Y4W46D6M.mjs.map
