Modules/businessdev.ALbuild.Containers/Private/Get-BcArtifactCandidate.ps1

function Get-BcArtifactCandidate {
    <#
    .SYNOPSIS
        Queries the BC artifact indexes and returns matching "version/country" candidates.
    .DESCRIPTION
        Internal helper. Downloads the per-country version index (and, unless suppressed, the
        platform index to confirm the platform build exists), filters by version prefix, and
        returns "{version}/{country}" strings sorted ascending by version.
    #>

    [CmdletBinding()]
    [OutputType([string[]])]
    param(
        [Parameter(Mandatory)]
        [PSCustomObject] $Storage,

        [string] $Country = '',

        [string] $VersionPrefix = '',

        [switch] $DoNotCheckPlatform
    )

    $download = {
        param([string] $url)
        $attempt = 0
        while ($true) {
            $attempt++
            try {
                return Invoke-RestMethod -Uri $url -Method Get -UseBasicParsing -TimeoutSec 100 -ErrorAction Stop
            }
            catch {
                if ($attempt -ge 3) { throw "Failed to query artifact index '$url': $($_.Exception.Message)" }
                Start-Sleep -Seconds (2 * $attempt)
            }
        }
    }

    if ([string]::IsNullOrWhiteSpace($Country)) {
        $countries = @((& $download "$($Storage.IndexesUrl)/countries.json") | Where-Object { $_ -ne 'platform' })
    }
    else {
        $countries = @($Country.ToLowerInvariant())
    }

    $platformVersions = $null
    if (-not $DoNotCheckPlatform) {
        $platformVersions = @((& $download "$($Storage.IndexesUrl)/platform.json") | ForEach-Object { $_.Version })
    }

    $candidates = New-Object System.Collections.Generic.List[string]
    foreach ($c in $countries) {
        $entries = & $download "$($Storage.IndexesUrl)/$c.json"
        foreach ($entry in $entries) {
            if ($VersionPrefix -and ($entry.Version -notlike "$VersionPrefix*")) { continue }
            if ($platformVersions -and ($platformVersions -notcontains $entry.Version)) { continue }
            $candidates.Add("$($entry.Version)/$c")
        }
    }

    return @($candidates | Sort-Object -Property @{ Expression = { [version](($_ -split '/', 2)[0]) } })
}