import { getThreadwellUserAgent } from "./threadwell-user-agent.js";
const NPM_LATEST_VERSION_URL = "https://registry.npmjs.org/threadwell/latest";
const DEFAULT_VERSION_CHECK_TIMEOUT_MS = 10000;
function parsePackageVersion(version) {
    const match = version.trim().match(/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+.*)?$/);
    if (!match) {
        return undefined;
    }
    return {
        major: Number.parseInt(match[1], 10),
        minor: Number.parseInt(match[2], 10),
        patch: Number.parseInt(match[3], 10),
        prerelease: match[4],
    };
}
export function comparePackageVersions(leftVersion, rightVersion) {
    const left = parsePackageVersion(leftVersion);
    const right = parsePackageVersion(rightVersion);
    if (!left || !right) {
        return undefined;
    }
    if (left.major !== right.major)
        return left.major - right.major;
    if (left.minor !== right.minor)
        return left.minor - right.minor;
    if (left.patch !== right.patch)
        return left.patch - right.patch;
    if (left.prerelease === right.prerelease)
        return 0;
    if (!left.prerelease)
        return 1;
    if (!right.prerelease)
        return -1;
    return left.prerelease.localeCompare(right.prerelease);
}
export function isNewerPackageVersion(candidateVersion, currentVersion) {
    const comparison = comparePackageVersions(candidateVersion, currentVersion);
    if (comparison !== undefined) {
        return comparison > 0;
    }
    return candidateVersion.trim() !== currentVersion.trim();
}
export async function getLatestThreadwellRelease(currentVersion, options = {}) {
    if (process.env.THREADWELL_SKIP_VERSION_CHECK || process.env.THREADWELL_OFFLINE)
        return undefined;
    const response = await fetch(NPM_LATEST_VERSION_URL, {
        headers: {
            "User-Agent": getThreadwellUserAgent(currentVersion),
            accept: "application/json",
        },
        signal: AbortSignal.timeout(options.timeoutMs ?? DEFAULT_VERSION_CHECK_TIMEOUT_MS),
    });
    if (!response.ok)
        return undefined;
    const data = (await response.json());
    if (typeof data.version !== "string" || !data.version.trim()) {
        return undefined;
    }
    const packageName = typeof data.name === "string" && data.name.trim()
        ? data.name.trim()
        : typeof data.packageName === "string" && data.packageName.trim()
            ? data.packageName.trim()
            : undefined;
    return { version: data.version.trim(), packageName };
}
export async function getLatestThreadwellVersion(currentVersion, options = {}) {
    return (await getLatestThreadwellRelease(currentVersion, options))?.version;
}
export async function checkForNewThreadwellVersion(currentVersion, options = {}) {
    try {
        const latestVersion = await getLatestThreadwellVersion(currentVersion, options);
        if (latestVersion && isNewerPackageVersion(latestVersion, currentVersion)) {
            return latestVersion;
        }
        return undefined;
    }
    catch {
        return undefined;
    }
}
//# sourceMappingURL=version-check.js.map