function normalizeTag(raw) {
    return typeof raw === 'string' && raw.trim().length > 0 ? raw.trim() : null;
}
function parseCodexVersionFromTag(tag) {
    const value = typeof tag === 'string' ? tag.trim() : '';
    const match = /^rust-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 preferredCodexAssetNames() {
    if (process.platform === 'darwin' && process.arch === 'arm64') {
        return ['codex-aarch64-apple-darwin.tar.gz'];
    }
    if (process.platform === 'darwin' && process.arch === 'x64') {
        return ['codex-x86_64-apple-darwin.tar.gz'];
    }
    if (process.platform === 'linux' && process.arch === 'arm64') {
        return ['codex-aarch64-unknown-linux-musl.tar.gz', 'codex-aarch64-unknown-linux-gnu.tar.gz'];
    }
    if (process.platform === 'linux' && process.arch === 'x64') {
        return ['codex-x86_64-unknown-linux-musl.tar.gz', 'codex-x86_64-unknown-linux-gnu.tar.gz'];
    }
    if (process.platform === 'win32' && process.arch === 'arm64') {
        return ['codex-aarch64-pc-windows-msvc.exe.zip'];
    }
    if (process.platform === 'win32' && process.arch === 'x64') {
        return ['codex-x86_64-pc-windows-msvc.exe.zip'];
    }
    throw new Error(`Unsupported codex platform: ${process.platform}/${process.arch}`);
}
export function resolveCodexReleaseAsset(release) {
    const parsed = (release && typeof release === 'object' ? release : {});
    const tag = normalizeTag(parsed.tag_name);
    const version = parseCodexVersionFromTag(tag);
    const assets = normalizeAssets(parsed.assets);
    const selected = preferredCodexAssetNames()
        .map((name) => assets.find((asset) => asset.name === name))
        .find(Boolean);
    if (!selected) {
        throw new Error(`No codex release asset found for ${process.platform}/${process.arch}`);
    }
    if (!selected.digest) {
        throw new Error(`Codex release asset ${selected.name} is missing a required digest`);
    }
    return {
        name: selected.name,
        url: selected.browser_download_url,
        digest: selected.digest,
        tag,
        version,
    };
}
//# sourceMappingURL=codexRelease.js.map