import { resolveReleaseAssetBundle } from '@happier-dev/release-runtime/assets';
export function resolveCliBinaryAssetBundleFromReleaseAssets(params) {
    const os = String(params.os ?? '').trim();
    const arch = String(params.arch ?? '').trim();
    if (!os)
        throw new Error('os is required');
    if (!arch)
        throw new Error('arch is required');
    const preferVersion = String(params.preferVersion ?? '').trim() || inferRollingReleaseVersion({
        assets: params.assets,
        os,
        arch,
    });
    if (preferVersion) {
        return resolveReleaseAssetBundleForPreferredVersion({
            assets: params.assets,
            os,
            arch,
            version: preferVersion,
        });
    }
    return resolveReleaseAssetBundle({
        assets: params.assets,
        product: 'happier',
        os,
        arch,
        preferZipOnWindows: true,
    });
}
function inferRollingReleaseVersion(params) {
    const archiveExtensions = params.os.toLowerCase() === 'windows'
        ? ['.zip', '.tar.gz']
        : ['.tar.gz'];
    const archiveBasePrefix = `happier-v`;
    const archiveSuffix = `-${params.os}-${params.arch}`;
    const assets = Array.isArray(params.assets) ? params.assets : [];
    let selectedVersion = null;
    for (const asset of assets) {
        if (!asset || typeof asset !== 'object')
            continue;
        const name = typeof asset.name === 'string'
            ? asset.name.trim()
            : '';
        if (!name.startsWith(archiveBasePrefix))
            continue;
        for (const extension of archiveExtensions) {
            const expectedSuffix = `${archiveSuffix}${extension}`;
            if (!name.endsWith(expectedSuffix))
                continue;
            const version = name.slice(archiveBasePrefix.length, name.length - expectedSuffix.length);
            if (version) {
                selectedVersion = version;
            }
            break;
        }
    }
    return selectedVersion;
}
function resolveReleaseAssetBundleForPreferredVersion(params) {
    const desiredVersion = params.version;
    const desiredChecksumsName = `checksums-happier-v${desiredVersion}.txt`;
    const desiredChecksumsSigName = `${desiredChecksumsName}.minisig`;
    const desiredArchiveBase = `happier-v${desiredVersion}-${params.os}-${params.arch}`;
    const desiredArchiveNames = params.os.toLowerCase() === 'windows'
        ? [`${desiredArchiveBase}.zip`, `${desiredArchiveBase}.tar.gz`]
        : [`${desiredArchiveBase}.tar.gz`];
    const assets = Array.isArray(params.assets) ? params.assets : [];
    const filteredAssets = assets.filter((asset) => {
        if (!asset || typeof asset !== 'object')
            return false;
        const name = typeof asset.name === 'string'
            ? asset.name.trim()
            : '';
        return (name === desiredChecksumsName ||
            name === desiredChecksumsSigName ||
            desiredArchiveNames.includes(name));
    });
    const resolved = resolveReleaseAssetBundle({
        assets: filteredAssets,
        product: 'happier',
        os: params.os,
        arch: params.arch,
        preferZipOnWindows: true,
    });
    if (resolved.version !== desiredVersion) {
        throw new Error(`missing CLI archive for requested version ${desiredVersion}`);
    }
    return resolved;
}
//# sourceMappingURL=releaseAssetBundle.js.map