import { readFileSync } from "fs";
import { secureMkdirSync, secureWriteFileSync } from "./secureWrite.js";
import { join } from "path";
import axios from "axios";
import { logger } from "./logger.js";
import { KASTELL_DIR } from "./paths.js";
export const UPDATE_CHECK_FILE = join(KASTELL_DIR, ".update-check");
const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24 hours
const REQUEST_TIMEOUT_MS = 3000; // 3 seconds
/**
 * Compare two semver-like version strings.
 * Returns true if latest > current.
 */
export function isNewerVersion(current, latest) {
    // Strip leading 'v' if present
    const cleanCurrent = current.replace(/^v/, "");
    const cleanLatest = latest.replace(/^v/, "");
    const currentParts = cleanCurrent.split(".").map(Number);
    const latestParts = cleanLatest.split(".").map(Number);
    // Compare major, minor, patch
    for (let i = 0; i < Math.max(currentParts.length, latestParts.length); i++) {
        const currentPart = currentParts[i] || 0;
        const latestPart = latestParts[i] || 0;
        if (latestPart > currentPart) {
            return true;
        }
        if (latestPart < currentPart) {
            return false;
        }
    }
    return false;
}
function readCache() {
    try {
        const content = readFileSync(UPDATE_CHECK_FILE, "utf-8");
        const cache = JSON.parse(content);
        if (typeof cache.lastCheck !== "number" ||
            typeof cache.latestVersion !== "string" ||
            !/^\d+\.\d+\.\d+/.test(cache.latestVersion)) {
            return null;
        }
        return cache;
    }
    catch {
        return null;
    }
}
function writeCache(cache) {
    try {
        secureMkdirSync(KASTELL_DIR, { recursive: true });
        secureWriteFileSync(UPDATE_CHECK_FILE, JSON.stringify(cache));
    }
    catch {
        // Silently ignore write errors
    }
}
async function fetchLatestVersion() {
    try {
        const response = await axios.get("https://registry.npmjs.org/kastell/latest", {
            timeout: REQUEST_TIMEOUT_MS,
        });
        const version = response.data?.version;
        if (typeof version === "string" && /^\d+\.\d+\.\d+/.test(version)) {
            return version;
        }
        return null;
    }
    catch {
        return null;
    }
}
/**
 * Check for updates and display a message if a newer version is available.
 * This function never throws - all errors are silently caught.
 */
export async function checkForUpdate(currentVersion) {
    try {
        const cache = readCache();
        const now = Date.now();
        // Use cached version if it's fresh (less than 24 hours old)
        if (cache && now - cache.lastCheck < CHECK_INTERVAL_MS) {
            if (isNewerVersion(currentVersion, cache.latestVersion)) {
                logger.info(`Update available: ${currentVersion} → ${cache.latestVersion} — Run: npm i -g kastell`);
            }
            return;
        }
        // Fetch latest version from npm registry
        const latestVersion = await fetchLatestVersion();
        if (!latestVersion) {
            return;
        }
        // Save to cache
        writeCache({ lastCheck: now, latestVersion });
        // Display update message if newer version available
        if (isNewerVersion(currentVersion, latestVersion)) {
            logger.info(`Update available: ${currentVersion} → ${latestVersion} — Run: npm i -g kastell`);
        }
    }
    catch {
        // Silently ignore all errors - update check is non-blocking
    }
}
//# sourceMappingURL=updateCheck.js.map