import { execFile } from "node:child_process";
import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
const packageName = "@jx-grxf/patchpilot";
const githubLatestReleaseUrl = "https://api.github.com/repos/jx-grxf/PatchPilot/releases/latest";
export async function checkForPatchPilotUpdate(currentVersion, signal) {
    const npmVersion = await fetchLatestNpmVersion(signal).catch(() => null);
    if (npmVersion) {
        return toUpdateResult(currentVersion, npmVersion, "npm");
    }
    const githubVersion = await fetchLatestGithubReleaseVersion(signal).catch(() => null);
    if (githubVersion) {
        return toUpdateResult(currentVersion, githubVersion, "github");
    }
    return {
        available: false,
        currentVersion,
        latestVersion: null,
        source: "none"
    };
}
export async function installPatchPilotUpdate(version) {
    const command = updateCommand();
    await execFileAsync("npm", ["update", "-g", packageName], {
        timeout: 180_000,
        maxBuffer: 2_000_000,
        windowsHide: true
    });
    return {
        version,
        command
    };
}
export function updateCommand() {
    return `npm update -g ${packageName}`;
}
function toUpdateResult(currentVersion, latestVersion, source) {
    if (compareSemver(latestVersion, currentVersion) <= 0) {
        return {
            available: false,
            currentVersion,
            latestVersion,
            source
        };
    }
    return {
        available: true,
        currentVersion,
        latestVersion,
        source,
        command: updateCommand()
    };
}
async function fetchLatestNpmVersion(signal) {
    const response = await fetch(`https://registry.npmjs.org/${encodeURIComponent(packageName)}/latest`, {
        headers: {
            Accept: "application/json"
        },
        signal
    });
    if (!response.ok) {
        return null;
    }
    const payload = (await response.json());
    return typeof payload.version === "string" ? payload.version : null;
}
async function fetchLatestGithubReleaseVersion(signal) {
    const response = await fetch(githubLatestReleaseUrl, {
        headers: {
            Accept: "application/vnd.github+json",
            "User-Agent": "PatchPilot"
        },
        signal
    });
    if (!response.ok) {
        return null;
    }
    const payload = (await response.json());
    return normalizeVersion(typeof payload.tag_name === "string" ? payload.tag_name : typeof payload.name === "string" ? payload.name : "");
}
function compareSemver(left, right) {
    const leftParts = parseVersion(left);
    const rightParts = parseVersion(right);
    for (let index = 0; index < 3; index += 1) {
        const diff = leftParts[index] - rightParts[index];
        if (diff !== 0) {
            return diff;
        }
    }
    return 0;
}
function parseVersion(value) {
    const [major = "0", minor = "0", patch = "0"] = normalizeVersion(value).split(".");
    return [readVersionPart(major), readVersionPart(minor), readVersionPart(patch)];
}
function normalizeVersion(value) {
    return value.trim().replace(/^v/i, "");
}
function readVersionPart(value) {
    const parsed = Number.parseInt(value.replace(/[^0-9].*$/, ""), 10);
    return Number.isFinite(parsed) ? parsed : 0;
}
//# sourceMappingURL=updateCheck.js.map