"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkForUpdates = exports.getLatestVersion = void 0;
const chalk_1 = __importDefault(require("chalk"));
const gt_1 = __importDefault(require("semver/functions/gt"));
const package_json_1 = __importDefault(require("../package.json"));
const fetch_1 = require("./fetch");
const logger_1 = __importDefault(require("./logger"));
const VERSION = package_json_1.default.version;
async function getLatestVersion() {
    const response = await (0, fetch_1.fetchWithTimeout)(`https://api.promptfoo.dev/api/latestVersion`, {}, 1000);
    if (!response.ok) {
        throw new Error(`Failed to fetch package information for promptfoo`);
    }
    const data = (await response.json());
    return data.latestVersion;
}
exports.getLatestVersion = getLatestVersion;
async function checkForUpdates() {
    if (process.env.PROMPTFOO_DISABLE_UPDATE) {
        return false;
    }
    let latestVersion;
    try {
        latestVersion = await getLatestVersion();
    }
    catch {
        return false;
    }
    if ((0, gt_1.default)(latestVersion, VERSION)) {
        const border = '='.repeat(process.stdout.columns - 10);
        logger_1.default.info(`\n${border}
${chalk_1.default.yellow('⚠️')} The current version of promptfoo ${chalk_1.default.yellow(VERSION)} is lower than the latest available version ${chalk_1.default.green(latestVersion)}.

Please run ${chalk_1.default.green('npx promptfoo@latest')} or ${chalk_1.default.green('npm install -g promptfoo@latest')} to update.
${border}\n`);
        return true;
    }
    return false;
}
exports.checkForUpdates = checkForUpdates;
//# sourceMappingURL=updates.js.map