import { existsSync, mkdirSync } from "node:fs";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import chalk from "chalk";
import { gt, inc, lt } from "semver";
import { ZUPLO_VERSION_CHECK_FILE } from "./constants.js";
import { printWarningToConsole } from "./output.js";
import box from "./utils/box.js";
import { ZUPLO_XDG_STATE_HOME } from "./xdg/lib.js";
export async function warnNodeVersion(currentCliVersion, desiredNodeVersion) {
    const currentNodeVersion = process.versions.node;
    if (lt(currentNodeVersion, desiredNodeVersion)) {
        const nextMajorCliVersion = inc(currentCliVersion, "major");
        printWarningToConsole(box(`You are using Node.js ${currentNodeVersion}. The recommended version for the @zuplo/cli is ${nextMajorCliVersion} or greater.
        Some features might not work as expected. Please update your Node.js version as soon as possible.`));
    }
}
export async function warnIfOutdatedVersion(currentVersion) {
    if (!process.env.ZUPLO_OVERRIDE_CI_TO_TEST &&
        (process.env.CI || process.env.ZUPLO_DISABLE_UPDATE_CHECK)) {
        return false;
    }
    const versionCheckInfo = await getVersionCheckInfo();
    const shouldWarn = gt(versionCheckInfo.latestVersion, currentVersion);
    if (shouldWarn) {
        printWarningToConsole(box(`Update available! ${chalk.gray(`v${currentVersion}`)} ≫ ${chalk.green(`v${versionCheckInfo.latestVersion}`)}
Run ${chalk.cyan("npm install zuplo@latest")} to update.

${chalk.gray("Older versions are unsupported and may not work as expected.")}`));
    }
    return shouldWarn;
}
async function getLatestVersion() {
    const response = await fetch("https://raw.githubusercontent.com/zuplo/zuplo/main/packages/zuplo/package.json");
    const result = await response.json();
    return result.dependencies["@zuplo/cli"];
}
async function getVersionCheckInfo() {
    if (!existsSync(ZUPLO_XDG_STATE_HOME)) {
        mkdirSync(ZUPLO_XDG_STATE_HOME, { recursive: true });
    }
    const versionCheckPath = join(ZUPLO_XDG_STATE_HOME, ZUPLO_VERSION_CHECK_FILE);
    let versionCheckInfo;
    if (existsSync(versionCheckPath)) {
        try {
            versionCheckInfo = await readFile(versionCheckPath, "utf-8").then(JSON.parse);
        }
        catch {
        }
        if (versionCheckInfo) {
            if (typeof versionCheckInfo.lastCheck !== "number" ||
                typeof versionCheckInfo.latestVersion !== "string") {
                versionCheckInfo = undefined;
            }
        }
    }
    let shouldCheck = true;
    if (versionCheckInfo) {
        const now = Date.now();
        const lastCheck = versionCheckInfo.lastCheck;
        if (now - lastCheck < 1000 * 60 * 60 * 24) {
            shouldCheck = false;
        }
    }
    if (!versionCheckInfo || shouldCheck) {
        const latestVersion = await getLatestVersion();
        versionCheckInfo = {
            lastCheck: Date.now(),
            latestVersion,
        };
        await writeFile(versionCheckPath, JSON.stringify(versionCheckInfo), "utf-8");
    }
    return versionCheckInfo;
}
//# sourceMappingURL=outdated.js.map