import {
  tabTerminalConnectionId,
  useSandboxTerminalConnection
} from "../chunk-65P3HJY3.js";

// src/web-react/workspace-terminal-panel.tsx
import { lazy, Suspense } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var TerminalView = lazy(
  () => import("@tangle-network/sandbox-ui/terminal").then((m) => ({ default: m.TerminalView }))
);
var TONE_DOT = {
  idle: "bg-muted-foreground/50",
  connecting: "animate-pulse bg-warning",
  connected: "bg-success",
  error: "bg-destructive"
};
function defaultStatusDisplay(conn) {
  if (conn.error) return { tone: "error", label: "Disconnected" };
  if (conn.runtimeUrl && conn.token) return { tone: "connected", label: "Connected" };
  if (conn.loading) return { tone: "connecting", label: conn.status === "provisioning" ? "Provisioning\u2026" : "Connecting\u2026" };
  return { tone: "idle", label: "Idle" };
}
function WorkspaceTerminalPanel({
  connection,
  connectionId,
  title = "Terminal",
  subtitle,
  isActive,
  onRetry,
  statusDisplay,
  headerExtra,
  className
}) {
  const status = (statusDisplay ?? defaultStatusDisplay)(connection);
  const apiUrl = connection.runtimeUrl ?? connection.sidecarUrl;
  const ready = Boolean(apiUrl && connection.token);
  return /* @__PURE__ */ jsxs("div", { className: `flex h-full min-h-0 flex-col overflow-hidden rounded-xl border border-border bg-card ${className ?? ""}`, children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-3 border-b border-border px-4 py-2.5", children: [
      /* @__PURE__ */ jsxs("div", { className: "min-w-0", children: [
        /* @__PURE__ */ jsx("p", { className: "truncate text-sm font-medium text-foreground", children: title }),
        subtitle && /* @__PURE__ */ jsx("p", { className: "truncate text-xs text-muted-foreground", children: subtitle })
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
        /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1.5 text-xs text-muted-foreground", children: [
          /* @__PURE__ */ jsx("span", { className: `h-1.5 w-1.5 rounded-full ${TONE_DOT[status.tone]}`, "aria-hidden": true }),
          status.label
        ] }),
        headerExtra
      ] })
    ] }),
    /* @__PURE__ */ jsx("div", { className: "relative min-h-0 flex-1", children: ready ? /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx(TerminalMessage, { children: "Loading terminal\u2026" }), children: /* @__PURE__ */ jsx(
      TerminalView,
      {
        apiUrl,
        token: connection.token,
        connectionId,
        title,
        subtitle,
        isActive
      }
    ) }) : /* @__PURE__ */ jsx(TerminalMessage, { children: connection.error ? /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx("p", { className: "text-sm text-destructive", children: connection.error }),
      onRetry && /* @__PURE__ */ jsx(
        "button",
        {
          type: "button",
          onClick: onRetry,
          className: "mt-3 inline-flex items-center justify-center rounded-lg border border-border px-3 py-1.5 text-sm font-medium text-foreground transition hover:bg-muted",
          children: "Reconnect"
        }
      )
    ] }) : connection.loading ? /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: connection.status === "provisioning" ? "Provisioning sandbox\u2026" : "Connecting\u2026" }) : /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: "Terminal not connected." }),
      onRetry && /* @__PURE__ */ jsx(
        "button",
        {
          type: "button",
          onClick: onRetry,
          className: "mt-3 inline-flex items-center justify-center rounded-lg border border-border px-3 py-1.5 text-sm font-medium text-foreground transition hover:bg-muted",
          children: "Connect"
        }
      )
    ] }) }) })
  ] });
}
function TerminalMessage({ children }) {
  return /* @__PURE__ */ jsx("div", { className: "absolute inset-0 flex flex-col items-center justify-center p-6 text-center", children });
}
export {
  WorkspaceTerminalPanel,
  tabTerminalConnectionId,
  useSandboxTerminalConnection
};
//# sourceMappingURL=terminal.js.map