// @bun
import {
  init_RemoteCallout,
  shouldShowRemoteCallout
} from "./chunk-8a2h28kk.js";
import"./chunk-6wgtpme1.js";
import {
  checkEnvLessBridgeMinVersion,
  init_envLessBridgeConfig
} from "./chunk-r59kdjpt.js";
import {
  $toString,
  init_server
} from "./chunk-xrw80zgd.js";
import {
  init_AppState,
  init_overlayContext,
  useAppState,
  useRegisterOverlay,
  useSetAppState
} from "./chunk-e91vvygt.js";
import"./chunk-k60b56gr.js";
import"./chunk-14p6wvsq.js";
import"./chunk-n66h7gtd.js";
import"./chunk-28jd8qjx.js";
import"./chunk-djt39ze3.js";
import"./chunk-9a9g5hbj.js";
import"./chunk-ptxteaeh.js";
import"./chunk-ftrj0crk.js";
import"./chunk-yrpthe2x.js";
import"./chunk-10fpzkhd.js";
import"./chunk-awz3dq2e.js";
import"./chunk-2tabctw9.js";
import"./chunk-z21x0z3h.js";
import"./chunk-9hn8e6h1.js";
import"./chunk-rgyzsbs3.js";
import"./chunk-qn6me9n1.js";
import"./chunk-9rwstxn3.js";
import {
  checkBridgeMinVersion,
  getBridgeDisabledReason,
  init_bridgeEnabled,
  isEnvLessBridgeEnabled
} from "./chunk-td4ter5p.js";
import {
  BRIDGE_LOGIN_INSTRUCTION,
  REMOTE_CONTROL_DISCONNECTED_MSG,
  init_types
} from "./chunk-71sdcaq6.js";
import"./chunk-cb6yemrf.js";
import"./chunk-tj8te4np.js";
import"./chunk-jjh8y2m7.js";
import"./chunk-dr2x4ze7.js";
import"./chunk-8ad8jmrf.js";
import"./chunk-24kv69g3.js";
import"./chunk-68n1gfc7.js";
import"./chunk-1w7c5hws.js";
import"./chunk-4gyjc4jm.js";
import"./chunk-4spgkgr3.js";
import"./chunk-e8dhxycp.js";
import"./chunk-hf0jvnz1.js";
import {
  init_useKeybinding
} from "./chunk-9pygs2bc.js";
import"./chunk-60fkafk2.js";
import"./chunk-gaazk5m4.js";
import"./chunk-vgmsapsy.js";
import"./chunk-093ej2sf.js";
import"./chunk-7tfdhkpy.js";
import"./chunk-nde5ym6a.js";
import"./chunk-zypcr179.js";
import"./chunk-etcs82b2.js";
import {
  getBridgeAccessToken,
  init_bridgeConfig
} from "./chunk-7wy0nfkf.js";
import"./chunk-1emjj9xz.js";
import"./chunk-m1hz377a.js";
import"./chunk-4nctdw69.js";
import"./chunk-vhghhw4q.js";
import"./chunk-935nrvdb.js";
import"./chunk-8zz4z1q3.js";
import"./chunk-hdwp8fte.js";
import"./chunk-hqxp6b72.js";
import"./chunk-3cwfww0q.js";
import"./chunk-xqc9bbe3.js";
import"./chunk-w5hnghah.js";
import"./chunk-qm7pdcft.js";
import"./chunk-x59e8cv0.js";
import"./chunk-s76nvx50.js";
import"./chunk-7jyr5758.js";
import"./chunk-zrpa39fh.js";
import"./chunk-nkg1b3qw.js";
import"./chunk-c1kx8bry.js";
import"./chunk-epvbnq43.js";
import"./chunk-swgt1px0.js";
import"./chunk-8kf8h7xf.js";
import"./chunk-swz6krys.js";
import"./chunk-7eywm3ed.js";
import"./chunk-bgan4cpf.js";
import"./chunk-jmv7k0jn.js";
import"./chunk-25w7rxtr.js";
import"./chunk-5xfj4jkd.js";
import"./chunk-c1t99xmt.js";
import"./chunk-vwenx8ke.js";
import"./chunk-8thwr7p8.js";
import"./chunk-v4ypszbb.js";
import"./chunk-bk6ck5c2.js";
import"./chunk-ym6j0wv1.js";
import"./chunk-hjmatcgt.js";
import"./chunk-6mdh70q0.js";
import"./chunk-ch92ycde.js";
import"./chunk-e4dsy4g1.js";
import"./chunk-326zehp8.js";
import"./chunk-kc67kt75.js";
import"./chunk-40t1d75v.js";
import"./chunk-rt34thxn.js";
import"./chunk-e3abfxpy.js";
import"./chunk-q44zc68f.js";
import"./chunk-fejeqe61.js";
import"./chunk-kgkdmr0x.js";
import"./chunk-hn4w9pkj.js";
import {
  Dialog,
  ListItem,
  ThemedBox_default,
  ThemedText,
  init_src,
  require_react,
  useKeybindings
} from "./chunk-cswpvb8x.js";
import {
  require_jsx_runtime
} from "./chunk-gxyx8b8w.js";
import {
  init_analytics,
  logEvent
} from "./chunk-j1mep9ck.js";
import"./chunk-6x35ffpx.js";
import"./chunk-1zbwhcbt.js";
import"./chunk-ddaxg4xh.js";
import"./chunk-vvh31np8.js";
import"./chunk-tywrrc1m.js";
import"./chunk-e3j7m7k2.js";
import"./chunk-4p0hxk2c.js";
import"./chunk-c4dyxsat.js";
import"./chunk-34rn09za.js";
import"./chunk-m28vg9w4.js";
import"./chunk-rk9fgqmg.js";
import"./chunk-nw4zt0y4.js";
import"./chunk-c1yc761e.js";
import"./chunk-c5g9shkw.js";
import {
  init_debug,
  logForDebugging
} from "./chunk-k9jzhhww.js";
import"./chunk-kb3758f7.js";
import"./chunk-xqs9r7pg.js";
import"./chunk-tj0dzck2.js";
import"./chunk-aeysytks.js";
import"./chunk-ns1htxgd.js";
import"./chunk-ztqzmfx1.js";
import"./chunk-6k1rsk85.js";
import"./chunk-nxzx0ey9.js";
import"./chunk-yes1my80.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_runtime.jsx(BridgeDisconnectDialog, {
      onDone
    });
  }
  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_runtime.jsx(Dialog, {
    title: "Remote Control",
    onCancel: handleContinue,
    hideInputGuide: true,
    children: /* @__PURE__ */ jsx_runtime.jsxs(ThemedBox_default, {
      flexDirection: "column",
      gap: 1,
      children: [
        /* @__PURE__ */ jsx_runtime.jsxs(ThemedText, {
          children: [
            "This session is available via Remote Control",
            displayUrl ? ` at ${displayUrl}` : "",
            "."
          ]
        }),
        showQR && qrLines.length > 0 && /* @__PURE__ */ jsx_runtime.jsx(ThemedBox_default, {
          flexDirection: "column",
          children: qrLines.map((line, i) => /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
            children: line
          }, i))
        }),
        /* @__PURE__ */ jsx_runtime.jsxs(ThemedBox_default, {
          flexDirection: "column",
          children: [
            /* @__PURE__ */ jsx_runtime.jsx(ListItem, {
              isFocused: focusIndex === 0,
              children: /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
                children: "Disconnect this session"
              })
            }),
            /* @__PURE__ */ jsx_runtime.jsx(ListItem, {
              isFocused: focusIndex === 1,
              children: /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
                children: showQR ? "Hide QR code" : "Show QR code"
              })
            }),
            /* @__PURE__ */ jsx_runtime.jsx(ListItem, {
              isFocused: focusIndex === 2,
              children: /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
                children: "Continue"
              })
            })
          ]
        }),
        /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
          dimColor: true,
          children: "Enter to select \xB7 Esc to continue"
        })
      ]
    })
  });
}
async function checkBridgePrerequisites() {
  const { waitForPolicyLimitsToLoad, isPolicyAllowed } = await import("./chunk-ar9ycnne.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-0r1rw2zf.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_runtime.jsx(BridgeToggle, {
    onDone,
    name
  });
}
var import_react, jsx_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_runtime = __toESM(require_jsx_runtime(), 1);
});
init_bridge();

export {
  call
};

//# debugId=2CB8AA870EBBA1CB64756E2164756E21
//# sourceMappingURL=chunk-tdzaf167.js.map
