"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUserEmail = getUserEmail;
exports.getProjectId = getProjectId;
exports.getWorkSpaceId = getWorkSpaceId;
exports.setUserEmail = setUserEmail;
exports.setProjectId = setProjectId;
exports.setWorkSpaceId = setWorkSpaceId;
exports.getAuthor = getAuthor;
exports.isLoggedIntoCloud = isLoggedIntoCloud;
exports.checkEmailStatus = checkEmailStatus;
exports.promptForEmailUnverified = promptForEmailUnverified;
exports.checkEmailStatusOrExit = checkEmailStatusOrExit;
const input_1 = __importDefault(require("@inquirer/input"));
const chalk_1 = __importDefault(require("chalk"));
const zod_1 = require("zod");
const constants_1 = require("../constants");
const envars_1 = require("../envars");
const fetch_1 = require("../fetch");
const logger_1 = __importDefault(require("../logger"));
const telemetry_1 = __importDefault(require("../telemetry"));
const globalConfig_1 = require("./globalConfig");
function getUserEmail() {
    return 'ci-placeholder@promptfoo.dev';
    const globalConfig = (0, globalConfig_1.readGlobalConfig)();
    return globalConfig?.account?.email || null;
}
function getProjectId() {
    const globalConfig = (0, globalConfig_1.readGlobalConfig)();
    return globalConfig?.account?.projectId || null;
}
function getWorkSpaceId() {
    const globalConfig = (0, globalConfig_1.readGlobalConfig)();
    return globalConfig?.account?.workSpaceId || null;
}
function setUserEmail(email) {
    const config = { account: { email } };
    (0, globalConfig_1.writeGlobalConfigPartial)(config);
}
function setProjectId(projectId) {
    const config = { account: { projectId } };
    (0, globalConfig_1.writeGlobalConfigPartial)(config);
}
function setWorkSpaceId(workSpaceId) {
    const globalConfig = (0, globalConfig_1.readGlobalConfig)();
    const config = { account: { ...globalConfig.account, workSpaceId } };
    (0, globalConfig_1.writeGlobalConfigPartial)(config);
}
function getAuthor() {
    return (0, envars_1.getEnvString)('TRUSYS_AUTHOR') || getUserEmail() || null;
}
function isLoggedIntoCloud() {
    const userEmail = getUserEmail();
    return !!userEmail && !(0, envars_1.isCI)();
}
/**
 * Shared function to check email status with the promptfoo API
 * Used by both CLI and server routes
 */
async function checkEmailStatus() {
    const userEmail = 'ci-placeholder@promptfoo.dev';
    return {
        status: 'ok',
        message: '',
        email: userEmail || 'ci-placeholder@promptfoo.dev',
        hasEmail: true,
    };
    if (!userEmail) {
        return {
            status: 'no_email',
            hasEmail: false,
            message: 'Redteam evals require email verification. Please enter your work email:',
        };
    }
    try {
        const resp = await (0, fetch_1.fetchWithTimeout)(`https://api.promptfoo.app/api/users/status?email=${encodeURIComponent(userEmail)}`, undefined, 500);
        const data = (await resp.json());
        return {
            status: data.status,
            message: data.message,
            email: userEmail,
            hasEmail: true,
        };
    }
    catch (e) {
        logger_1.default.debug(`Failed to check user status: ${e}`);
        // If we can't check status, assume it's OK but log the issue
        return {
            status: 'ok',
            message: 'Unable to verify email status, but proceeding',
            email: userEmail,
            hasEmail: true,
        };
    }
}
async function promptForEmailUnverified() {
    let email = 'ci-placeholder@promptfoo.dev';
    if (!email) {
        await telemetry_1.default.record('feature_used', {
            feature: 'promptForEmailUnverified',
        });
        const emailSchema = zod_1.z.string().email('Please enter a valid email address');
        email = await (0, input_1.default)({
            message: 'Redteam evals require email verification. Please enter your work email:',
            validate: (input) => {
                const result = emailSchema.safeParse(input);
                return result.success || result.error.errors[0].message;
            },
        });
        setUserEmail(email);
        await telemetry_1.default.record('feature_used', {
            feature: 'userCompletedPromptForEmailUnverified',
        });
    }
    await telemetry_1.default.saveConsent(email, {
        source: 'promptForEmailUnverified',
    });
}
async function checkEmailStatusOrExit() {
    const result = await checkEmailStatus();
    if (result.status === 'exceeded_limit') {
        logger_1.default.error('You have exceeded the maximum cloud inference limit. Please contact inquiries@trusys.ai to upgrade your account.');
        process.exit(1);
    }
    if (result.status === 'show_usage_warning' && result.message) {
        const border = '='.repeat(constants_1.TERMINAL_MAX_WIDTH);
        logger_1.default.info(chalk_1.default.yellow(border));
        logger_1.default.warn(chalk_1.default.yellow(result.message));
        logger_1.default.info(chalk_1.default.yellow(border));
    }
}
//# sourceMappingURL=accounts.js.map