// Viewer config — env-driven settings for the long-message render router.
//
// Kept minimal & env-only for v1: the operator sets two variables (a public
// base URL + an enable flag), and the defaults handle everything else. No
// JSON-config schema dance, no on-disk state — change env, restart, done.
import { DEFAULT_THRESHOLDS } from './render-router.js';
import { getCurrentTunnelUrl } from './tunnel.js';
function parsePositiveInt(raw, fallback) {
    if (!raw)
        return fallback;
    const n = parseInt(raw, 10);
    return Number.isFinite(n) && n > 0 ? n : fallback;
}
/**
 * v1.2.43 — community-hosted default paste server.
 *
 * Operators who don't have a domain or working tunnel still get usable
 * viewer links out of the box by routing through this public service.
 * Users can:
 *   - Override by setting their own IMHUB_VIEWER_REMOTE_PASTE_URL
 *   - Opt back into a local-only path by setting IMHUB_VIEWER_PUBLIC_BASE_URL
 *   - Disable the default entirely with IMHUB_VIEWER_NO_DEFAULT_REMOTE=1
 *
 * IMPORTANT — privacy: with the default on, any markdown long enough
 * to exit the inline path is uploaded to viewer.iclaw.host over HTTPS.
 * agim's CLI wizard surfaces this trade-off on first run; deployment
 * docs (`docs/viewer-modes.md`) cover it too.
 */
export const DEFAULT_REMOTE_PASTE_URL = 'https://viewer.iclaw.host/paste';
export function getViewerConfig() {
    const enabledRaw = (process.env.IMHUB_VIEWER_ENABLED || '').toLowerCase();
    const enabled = enabledRaw === '1' || enabledRaw === 'true' || enabledRaw === 'yes';
    const publicBaseUrl = (process.env.IMHUB_VIEWER_PUBLIC_BASE_URL || '').replace(/\/$/, '');
    const tunnelModeRaw = (process.env.IMHUB_VIEWER_TUNNEL_MODE || 'off').toLowerCase();
    const tunnelMode = tunnelModeRaw === 'quick' ? 'quick' : 'off';
    const remotePasteUrl = (process.env.IMHUB_VIEWER_REMOTE_PASTE_URL || '').replace(/\/$/, '');
    const remotePasteToken = process.env.IMHUB_VIEWER_REMOTE_PASTE_TOKEN || '';
    return {
        enabled,
        publicBaseUrl,
        tunnelMode,
        remotePasteUrl,
        remotePasteToken,
        thresholds: {
            chars: parsePositiveInt(process.env.IMHUB_VIEWER_CHARS, DEFAULT_THRESHOLDS.chars),
            lines: parsePositiveInt(process.env.IMHUB_VIEWER_LINES, DEFAULT_THRESHOLDS.lines),
            codeLines: parsePositiveInt(process.env.IMHUB_VIEWER_CODE_LINES, DEFAULT_THRESHOLDS.codeLines),
        },
    };
}
/**
 * v1.2.45 — pick the active viewer mode.
 *
 * Operator-visible knobs (CLI + Web admin):
 *   - Public base URL  → run agim behind your own domain (local mode)
 *   - Tunnel mode      → cloudflared quick tunnel (tunnel mode)
 *   - Disable default  → opt out of the community paste server fallback
 *
 * Hidden env override (advanced, not surfaced in UI):
 *   - IMHUB_VIEWER_REMOTE_PASTE_URL — point to a different paste
 *     server (e.g. your own self-hosted clone of viewer-paste). Only
 *     kicks in when neither public base nor tunnel is configured.
 *
 * Order:
 *   1. Public base URL                          → local
 *   2. Tunnel mode = quick                      → tunnel
 *   3. user-set IMHUB_VIEWER_REMOTE_PASTE_URL   → remote (custom server)
 *   4. community default viewer.iclaw.host       → remote (default-on)
 *   5. nothing                                   → none (inline degrade)
 *
 * Set IMHUB_VIEWER_NO_DEFAULT_REMOTE=1 to skip steps 3-4 entirely.
 */
export function resolveViewerMode(cfg = getViewerConfig()) {
    if (!cfg.enabled)
        return 'none';
    if (cfg.publicBaseUrl)
        return 'local';
    if (cfg.tunnelMode === 'quick')
        return 'tunnel';
    if (isDefaultRemoteDisabled())
        return 'none';
    return 'remote';
}
/**
 * Resolve the effective remote paste URL the sink should POST to.
 * Honours user-set value first, then the community default unless
 * the operator opted out via IMHUB_VIEWER_NO_DEFAULT_REMOTE=1.
 * Returns null when no usable URL is available.
 */
export function getEffectiveRemotePasteUrl(cfg = getViewerConfig()) {
    if (cfg.remotePasteUrl)
        return cfg.remotePasteUrl;
    if (!isDefaultRemoteDisabled())
        return DEFAULT_REMOTE_PASTE_URL;
    return null;
}
function isDefaultRemoteDisabled() {
    const v = (process.env.IMHUB_VIEWER_NO_DEFAULT_REMOTE || '').toLowerCase();
    return v === '1' || v === 'true' || v === 'yes';
}
/**
 * Resolve the effective public base URL. Order of preference:
 *   1. Explicit IMHUB_VIEWER_PUBLIC_BASE_URL (operator-managed reverse proxy)
 *   2. Auto-tunnel URL if tunnel_mode=quick and cloudflared has acquired a URL
 *   3. Caller-supplied fallback
 *
 * Returns empty string when nothing usable is available.
 */
export function getEffectivePublicBaseUrl(fallback) {
    const cfg = getViewerConfig();
    if (cfg.publicBaseUrl)
        return cfg.publicBaseUrl;
    if (cfg.tunnelMode === 'quick') {
        const tu = getCurrentTunnelUrl();
        if (tu)
            return tu;
    }
    return (fallback || '').replace(/\/$/, '');
}
/**
 * Build the URL placed in the IM reply for a saved paste. Returns null if
 * no public base URL is configured AND no fallback was provided. Caller
 * should treat null as "viewer can't produce a usable link — degrade".
 */
export function buildPasteUrl(id, fallbackBaseUrl) {
    const base = getEffectivePublicBaseUrl(fallbackBaseUrl);
    if (!base)
        return null;
    return `${base.replace(/\/$/, '')}/v/${id}`;
}
//# sourceMappingURL=viewer-config.js.map