export const PNPM_GITHUB_REPO = 'pnpm/pnpm';
function normalizeTag(raw) {
    return typeof raw === 'string' && raw.trim().length > 0 ? raw.trim() : null;
}
export function parsePnpmVersionFromTag(tag) {
    const value = typeof tag === 'string' ? tag.trim() : '';
    if (!value)
        return null;
    const match = /^v?(\d+\.\d+\.\d+(?:[-+][A-Za-z0-9.-]+)?)$/.exec(value);
    return match?.[1] ?? null;
}
function normalizeAssets(raw) {
    if (!Array.isArray(raw))
        return [];
    const assets = [];
    for (const entry of raw) {
        if (!entry || typeof entry !== 'object')
            continue;
        const name = typeof entry.name === 'string' ? entry.name.trim() : '';
        const url = typeof entry.browser_download_url === 'string'
            ? entry.browser_download_url.trim()
            : '';
        const digest = typeof entry.digest === 'string'
            ? entry.digest.trim()
            : null;
        if (!name || !url)
            continue;
        assets.push({ name, browser_download_url: url, digest });
    }
    return assets;
}
function getPreferredAssetName() {
    if (process.platform === 'darwin' && process.arch === 'arm64')
        return 'pnpm-macos-arm64';
    if (process.platform === 'darwin' && process.arch === 'x64')
        return 'pnpm-macos-x64';
    if (process.platform === 'linux' && process.arch === 'arm64')
        return 'pnpm-linuxstatic-arm64';
    if (process.platform === 'linux' && process.arch === 'x64')
        return 'pnpm-linuxstatic-x64';
    if (process.platform === 'win32' && process.arch === 'arm64')
        return 'pnpm-win-arm64.exe';
    if (process.platform === 'win32' && process.arch === 'x64')
        return 'pnpm-win-x64.exe';
    throw new Error(`Unsupported pnpm platform: ${process.platform}/${process.arch}`);
}
export function resolvePnpmReleaseAsset(release) {
    const parsed = (release && typeof release === 'object' ? release : {});
    const tag = normalizeTag(parsed.tag_name);
    const version = parsePnpmVersionFromTag(tag);
    const preferredName = getPreferredAssetName();
    const assets = normalizeAssets(parsed.assets);
    const selected = assets.find((asset) => asset.name === preferredName);
    if (!selected) {
        throw new Error(`No pnpm release asset found for ${preferredName}`);
    }
    if (!selected.digest) {
        throw new Error(`pnpm release asset ${selected.name} is missing a required digest`);
    }
    return {
        name: selected.name,
        url: selected.browser_download_url,
        digest: selected.digest,
        tag,
        version,
    };
}
//# sourceMappingURL=pnpmRelease.js.map