import { config } from '../config.js';
let proxyPort = 0;
let proxyReady = false;
// Port advertised in proxy-mode links, overriding the local proxy port.
// 0 = unset → advertise the local proxy port. Set from WEB_EXTERNAL_PORT +
// botIndex so a relay can front the terminal on a different port number.
let externalPort = 0;
/** Marks the proxy live on `port`. Called only after a successful bind. */
export function setTerminalProxyPort(port) {
    proxyPort = port;
    proxyReady = true;
}
/** Override the port shown in proxy-mode links (WEB_EXTERNAL_PORT + botIndex).
 *  0 reverts to advertising the local proxy port. */
export function setTerminalExternalPort(port) {
    externalPort = port;
}
/** Bound proxy port, or 0 when the proxy is not available. */
export function getTerminalProxyPort() {
    return proxyReady ? proxyPort : 0;
}
/** Port clients should use to reach the proxy: the configured external port
 *  (WEB_EXTERNAL_PORT + botIndex) when set, else the bound proxy port; 0 when
 *  the proxy isn't up. Single source of truth for the proxy-mode port that both
 *  buildTerminalUrl (card links) and the dashboard rows advertise, so they agree
 *  on the same external port instead of diverging. */
export function getTerminalAdvertisedPort() {
    return proxyReady ? externalPort || proxyPort : 0;
}
/** Test/edge helper: revert to the no-proxy (direct-port) state. */
export function resetTerminalProxy() {
    proxyPort = 0;
    proxyReady = false;
    externalPort = 0;
}
export function buildTerminalUrl(ds, opts = {}) {
    const base = proxyReady
        ? `http://${config.web.externalHost}:${getTerminalAdvertisedPort()}/s/${ds.session.sessionId}`
        : `http://${config.web.externalHost}:${ds.workerPort ?? ds.session.webPort}`;
    if (opts.write && ds.workerToken)
        return `${base}?token=${ds.workerToken}`;
    return base;
}
//# sourceMappingURL=terminal-url.js.map