// @bun
import {
  init_RemoteCallout,
  shouldShowRemoteCallout
} from "./chunk-zeebdzqf.js";
import"./chunk-8yk09jf7.js";
import {
  checkEnvLessBridgeMinVersion,
  init_envLessBridgeConfig
} from "./chunk-ekty2fdy.js";
import {
  $toString,
  init_server
} from "./chunk-xrw80zgd.js";
import {
  init_AppState,
  init_overlayContext,
  useAppState,
  useRegisterOverlay,
  useSetAppState
} from "./chunk-421yegj3.js";
import"./chunk-aaxz8a5g.js";
import"./chunk-2kcgxnvp.js";
import"./chunk-14p6wvsq.js";
import"./chunk-6s0q1s5r.js";
import"./chunk-0cpg8qh8.js";
import"./chunk-9phn5khx.js";
import"./chunk-vk9xcryd.js";
import"./chunk-9a9g5hbj.js";
import"./chunk-ptxteaeh.js";
import"./chunk-7fcmqvbz.js";
import"./chunk-rgyzsbs3.js";
import"./chunk-q3b4n194.js";
import"./chunk-zaqfb3tz.js";
import {
  checkBridgeMinVersion,
  getBridgeDisabledReason,
  init_bridgeEnabled,
  isEnvLessBridgeEnabled
} from "./chunk-6hzx0kqr.js";
import {
  BRIDGE_LOGIN_INSTRUCTION,
  REMOTE_CONTROL_DISCONNECTED_MSG,
  init_types
} from "./chunk-71sdcaq6.js";
import"./chunk-9hn8e6h1.js";
import"./chunk-dxa3y9vh.js";
import"./chunk-gjvhjywj.js";
import"./chunk-1w20m7gw.js";
import"./chunk-3gam18xn.js";
import"./chunk-rpmntgyh.js";
import"./chunk-24kv69g3.js";
import"./chunk-tzcrwnrf.js";
import"./chunk-k7xszak2.js";
import"./chunk-ztmb7geg.js";
import"./chunk-tbdkekz3.js";
import {
  init_useKeybinding
} from "./chunk-65zbgg1n.js";
import"./chunk-4spgkgr3.js";
import"./chunk-60fkafk2.js";
import"./chunk-h5zz0vxy.js";
import"./chunk-6hygvrhn.js";
import"./chunk-7tfdhkpy.js";
import"./chunk-8gv2pjby.js";
import"./chunk-pn23dh3d.js";
import {
  getBridgeAccessToken,
  init_bridgeConfig
} from "./chunk-rpdh02rm.js";
import"./chunk-j1wktfe2.js";
import"./chunk-fjtzfz1t.js";
import"./chunk-6hr7742d.js";
import"./chunk-xyg1sk2w.js";
import"./chunk-935nrvdb.js";
import"./chunk-8zz4z1q3.js";
import"./chunk-hem4f4gz.js";
import"./chunk-hqxp6b72.js";
import"./chunk-ccw8pse0.js";
import"./chunk-wsnxtggw.js";
import"./chunk-k8nwt8yq.js";
import"./chunk-vxbjzggp.js";
import"./chunk-d6h68rz1.js";
import"./chunk-8yvsnrkr.js";
import"./chunk-5mmhfbxb.js";
import"./chunk-s2t5anwv.js";
import"./chunk-ga1jkyqy.js";
import"./chunk-9hbhydvp.js";
import"./chunk-ck9r8hpf.js";
import"./chunk-k7hexw3v.js";
import"./chunk-trqrj23e.js";
import"./chunk-za3k6h2h.js";
import"./chunk-8kf8h7xf.js";
import"./chunk-bgan4cpf.js";
import"./chunk-jmv7k0jn.js";
import"./chunk-hjxqmtg5.js";
import"./chunk-bzyzrq0k.js";
import"./chunk-vwenx8ke.js";
import"./chunk-q2h79ncs.js";
import"./chunk-v4ypszbb.js";
import"./chunk-qfsn720k.js";
import"./chunk-7qx3xhv3.js";
import"./chunk-j9475b46.js";
import"./chunk-fdwabr8p.js";
import"./chunk-e4dsy4g1.js";
import"./chunk-326zehp8.js";
import"./chunk-kc67kt75.js";
import"./chunk-40t1d75v.js";
import"./chunk-dbts5q5p.js";
import"./chunk-e3abfxpy.js";
import"./chunk-q44zc68f.js";
import"./chunk-fejeqe61.js";
import"./chunk-q1vrhh0q.js";
import"./chunk-hn4w9pkj.js";
import {
  Dialog,
  ListItem,
  ThemedBox_default,
  ThemedText,
  init_src,
  useKeybindings
} from "./chunk-2g0ytd9d.js";
import {
  require_jsx_dev_runtime,
  require_react
} from "./chunk-de0qnnf7.js";
import {
  init_analytics,
  logEvent
} from "./chunk-j1mep9ck.js";
import"./chunk-6x35ffpx.js";
import"./chunk-1zbwhcbt.js";
import"./chunk-92bjkrf2.js";
import"./chunk-10k5jfc5.js";
import"./chunk-n9g24mwe.js";
import"./chunk-e3j7m7k2.js";
import"./chunk-hzmzwte2.js";
import"./chunk-pdvg91cg.js";
import"./chunk-c696122m.js";
import"./chunk-xbj5keyy.js";
import"./chunk-c1yc761e.js";
import"./chunk-c5g9shkw.js";
import"./chunk-aeysytks.js";
import {
  init_debug,
  logForDebugging
} from "./chunk-s3hafnk3.js";
import"./chunk-kb3758f7.js";
import"./chunk-9h9k5vz3.js";
import"./chunk-xmpgmeb8.js";
import"./chunk-2kz09j2s.js";
import"./chunk-c0k7b0jw.js";
import"./chunk-pecy49yr.js";
import"./chunk-azbab59e.js";
import"./chunk-3nk9q8dr.js";
import {
  __esm,
  __require,
  __toESM
} from "./chunk-hhsxm2yr.js";

// src/commands/bridge/bridge.tsx
function BridgeToggle({ onDone, name }) {
  const setAppState = useSetAppState();
  const replBridgeConnected = useAppState((s) => s.replBridgeConnected);
  const replBridgeEnabled = useAppState((s) => s.replBridgeEnabled);
  const replBridgeOutboundOnly = useAppState((s) => s.replBridgeOutboundOnly);
  const [showDisconnectDialog, setShowDisconnectDialog] = import_react.useState(false);
  import_react.useEffect(() => {
    if ((replBridgeConnected || replBridgeEnabled) && !replBridgeOutboundOnly) {
      setShowDisconnectDialog(true);
      return;
    }
    let cancelled = false;
    (async () => {
      const error = await checkBridgePrerequisites();
      if (cancelled)
        return;
      if (error) {
        logEvent("tengu_bridge_command", {
          action: "preflight_failed"
        });
        onDone(error, { display: "system" });
        return;
      }
      if (shouldShowRemoteCallout()) {
        setAppState((prev) => {
          if (prev.showRemoteCallout)
            return prev;
          return {
            ...prev,
            showRemoteCallout: true,
            replBridgeInitialName: name
          };
        });
        onDone("", { display: "system" });
        return;
      }
      logEvent("tengu_bridge_command", {
        action: "connect"
      });
      setAppState((prev) => {
        if (prev.replBridgeEnabled && !prev.replBridgeOutboundOnly)
          return prev;
        return {
          ...prev,
          replBridgeEnabled: true,
          replBridgeExplicit: true,
          replBridgeOutboundOnly: false,
          replBridgeInitialName: name
        };
      });
      onDone("Remote Control connecting\u2026", {
        display: "system"
      });
    })();
    return () => {
      cancelled = true;
    };
  }, []);
  if (showDisconnectDialog) {
    return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(BridgeDisconnectDialog, {
      onDone
    }, undefined, false, undefined, this);
  }
  return null;
}
function BridgeDisconnectDialog({ onDone }) {
  useRegisterOverlay("bridge-disconnect-dialog");
  const setAppState = useSetAppState();
  const sessionUrl = useAppState((s) => s.replBridgeSessionUrl);
  const connectUrl = useAppState((s) => s.replBridgeConnectUrl);
  const sessionActive = useAppState((s) => s.replBridgeSessionActive);
  const [focusIndex, setFocusIndex] = import_react.useState(2);
  const [showQR, setShowQR] = import_react.useState(false);
  const [qrText, setQrText] = import_react.useState("");
  const displayUrl = sessionActive ? sessionUrl : connectUrl;
  import_react.useEffect(() => {
    if (!showQR || !displayUrl) {
      setQrText("");
      return;
    }
    $toString(displayUrl, {
      type: "utf8",
      errorCorrectionLevel: "L",
      small: true
    }).then(setQrText).catch(() => setQrText(""));
  }, [showQR, displayUrl]);
  function handleDisconnect() {
    setAppState((prev) => {
      if (!prev.replBridgeEnabled)
        return prev;
      return {
        ...prev,
        replBridgeEnabled: false,
        replBridgeExplicit: false,
        replBridgeOutboundOnly: false
      };
    });
    logEvent("tengu_bridge_command", {
      action: "disconnect"
    });
    onDone(REMOTE_CONTROL_DISCONNECTED_MSG, { display: "system" });
  }
  function handleShowQR() {
    setShowQR((prev) => !prev);
  }
  function handleContinue() {
    onDone(undefined, { display: "skip" });
  }
  const ITEM_COUNT = 3;
  useKeybindings({
    "select:next": () => setFocusIndex((i) => (i + 1) % ITEM_COUNT),
    "select:previous": () => setFocusIndex((i) => (i - 1 + ITEM_COUNT) % ITEM_COUNT),
    "select:accept": () => {
      if (focusIndex === 0) {
        handleDisconnect();
      } else if (focusIndex === 1) {
        handleShowQR();
      } else {
        handleContinue();
      }
    }
  }, { context: "Select" });
  const qrLines = qrText ? qrText.split(`
`).filter((l) => l.length > 0) : [];
  return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(Dialog, {
    title: "Remote Control",
    onCancel: handleContinue,
    hideInputGuide: true,
    children: /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedBox_default, {
      flexDirection: "column",
      gap: 1,
      children: [
        /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedText, {
          children: [
            "This session is available via Remote Control",
            displayUrl ? ` at ${displayUrl}` : "",
            "."
          ]
        }, undefined, true, undefined, this),
        showQR && qrLines.length > 0 && /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedBox_default, {
          flexDirection: "column",
          children: qrLines.map((line, i) => /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedText, {
            children: line
          }, i, false, undefined, this))
        }, undefined, false, undefined, this),
        /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedBox_default, {
          flexDirection: "column",
          children: [
            /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ListItem, {
              isFocused: focusIndex === 0,
              children: /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedText, {
                children: "Disconnect this session"
              }, undefined, false, undefined, this)
            }, undefined, false, undefined, this),
            /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ListItem, {
              isFocused: focusIndex === 1,
              children: /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedText, {
                children: showQR ? "Hide QR code" : "Show QR code"
              }, undefined, false, undefined, this)
            }, undefined, false, undefined, this),
            /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ListItem, {
              isFocused: focusIndex === 2,
              children: /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedText, {
                children: "Continue"
              }, undefined, false, undefined, this)
            }, undefined, false, undefined, this)
          ]
        }, undefined, true, undefined, this),
        /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedText, {
          dimColor: true,
          children: "Enter to select \xB7 Esc to continue"
        }, undefined, false, undefined, this)
      ]
    }, undefined, true, undefined, this)
  }, undefined, false, undefined, this);
}
async function checkBridgePrerequisites() {
  const { waitForPolicyLimitsToLoad, isPolicyAllowed } = await import("./chunk-szcnh73g.js");
  await waitForPolicyLimitsToLoad();
  if (!isPolicyAllowed("allow_remote_control")) {
    return "Remote Control is disabled by your organization's policy.";
  }
  const disabledReason = await getBridgeDisabledReason();
  if (disabledReason) {
    return disabledReason;
  }
  let useV2 = isEnvLessBridgeEnabled();
  if (useV2) {
    const { isAssistantMode } = await import("./chunk-89yrrbm4.js");
    if (isAssistantMode()) {
      useV2 = false;
    }
  }
  const versionError = useV2 ? await checkEnvLessBridgeMinVersion() : checkBridgeMinVersion();
  if (versionError) {
    return versionError;
  }
  if (!getBridgeAccessToken()) {
    return BRIDGE_LOGIN_INSTRUCTION;
  }
  logForDebugging("[bridge] Prerequisites passed, enabling bridge");
  return null;
}
async function call(onDone, _context, args) {
  const name = args.trim() || undefined;
  return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(BridgeToggle, {
    onDone,
    name
  }, undefined, false, undefined, this);
}
var import_react, jsx_dev_runtime;
var init_bridge = __esm(() => {
  init_server();
  init_bridgeConfig();
  init_bridgeEnabled();
  init_envLessBridgeConfig();
  init_types();
  init_src();
  init_RemoteCallout();
  init_overlayContext();
  init_src();
  init_useKeybinding();
  init_analytics();
  init_AppState();
  init_debug();
  import_react = __toESM(require_react(), 1);
  jsx_dev_runtime = __toESM(require_jsx_dev_runtime(), 1);
});
init_bridge();

export {
  call
};
