"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRemoteGenerationUrl = getRemoteGenerationUrl;
exports.neverGenerateRemote = neverGenerateRemote;
exports.getRemoteHealthUrl = getRemoteHealthUrl;
exports.shouldGenerateRemote = shouldGenerateRemote;
exports.getRemoteGenerationUrlForUnaligned = getRemoteGenerationUrlForUnaligned;
exports.buildRemoteUrl = buildRemoteUrl;
const cliState_1 = __importDefault(require("../cliState"));
const envars_1 = require("../envars");
const cloud_1 = require("../globalConfig/cloud");
function getRemoteGenerationUrl() {
    // Check env var first
    const envUrl = (0, envars_1.getEnvString)('TRUSYS_REMOTE_GENERATION_URL');
    if (envUrl) {
        return envUrl;
    }
    // If logged into cloud use that url + /task
    const cloudConfig = new cloud_1.CloudConfig();
    if (cloudConfig.isEnabled()) {
        return cloudConfig.getApiHost() + '/api/v1/task';
    }
    // otherwise use the default
    return 'https://api.promptfoo.app/api/v1/task';
}
function neverGenerateRemote() {
    return (0, envars_1.getEnvBool)('TRUSYS_DISABLE_REDTEAM_REMOTE_GENERATION');
}
/**
 * Gets the URL for checking remote API health based on configuration.
 * @returns The health check URL, or null if remote generation is disabled.
 */
function getRemoteHealthUrl() {
    if (neverGenerateRemote()) {
        return null;
    }
    const envUrl = (0, envars_1.getEnvString)('TRUSYS_REMOTE_GENERATION_URL');
    if (envUrl) {
        try {
            const url = new URL(envUrl);
            url.pathname = '/health';
            return url.toString();
        }
        catch {
            return 'https://api.promptfoo.app/health';
            // return 'https://api.trusys.ai/health';
        }
    }
    const cloudConfig = new cloud_1.CloudConfig();
    if (cloudConfig.isEnabled()) {
        return `${cloudConfig.getApiHost()}/health`;
    }
    return 'https://api.promptfoo.app/health';
    // return 'https://api.trusys.ai/health';
}
function shouldGenerateRemote() {
    if (neverGenerateRemote())
        return false;
    // Default to local. Use remote only when explicitly requested via --remote.
    return cliState_1.default.remote ?? false;
}
function getRemoteGenerationUrlForUnaligned() {
    // Check env var first
    const envUrl = (0, envars_1.getEnvString)('TRUSYS_UNALIGNED_INFERENCE_ENDPOINT');
    if (envUrl) {
        return envUrl;
    }
    // If logged into cloud use that url + /task
    const cloudConfig = new cloud_1.CloudConfig();
    if (cloudConfig.isEnabled()) {
        return cloudConfig.getApiHost() + '/api/v1/task/harmful';
    }
    // otherwise use the default
    return 'https://api.promptfoo.app/api/v1/task/harmful';
}
/**
 * Builds a remote URL with a substituted pathname, honoring env vars / cloud config.
 */
function buildRemoteUrl(pathname, fallback) {
    if (neverGenerateRemote()) {
        return null;
    }
    const envUrl = (0, envars_1.getEnvString)('TRUSYS_REMOTE_GENERATION_URL');
    if (envUrl) {
        try {
            const url = new URL(envUrl);
            url.pathname = pathname;
            return url.toString();
        }
        catch {
            return fallback;
        }
    }
    const cloudConfig = new cloud_1.CloudConfig();
    if (cloudConfig.isEnabled()) {
        return `${cloudConfig.getApiHost()}${pathname}`;
    }
    return fallback;
}
//# sourceMappingURL=remoteGeneration.js.map