Modules/businessdev.ALbuild.Apps/Private/Resolve-BcTranslationMemorySource.ps1

function Resolve-BcTranslationMemorySource {
    <#
    .SYNOPSIS
        Resolves the prioritised translation-memory reference descriptors for one language.
    .DESCRIPTION
        Internal helper for Invoke-BcTranslationSync. Turns the albuild.json translation.memory.sources
        (or the default self+bc-base) into concrete { Files; Origin; Priority } descriptors for
        Import-BcTranslationMemory. BC-base XLFs are located in the warm BC artifact cache (never
        downloaded); a source may set an explicit 'path' to override. Missing sources are skipped.
    #>

    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory)] [string] $WorkspaceRoot,
        [Parameter(Mandatory)] [string] $Language,
        [Parameter(Mandatory)] [string] $TargetFile,
        [object] $Config
    )

    $prop = {
        param($obj, [string] $name, $default)
        if ($null -eq $obj) { return $default }
        $p = $obj.PSObject.Properties | Where-Object { $_.Name -ieq $name } | Select-Object -First 1
        if ($p -and $null -ne $p.Value) { return $p.Value }
        return $default
    }

    $mem = & $prop $Config 'memory' $null
    $sources = @(& $prop $mem 'sources' @())
    if (-not $sources -or $sources.Count -eq 0) {
        # Default: the app's own translations (highest) + the Microsoft BC base.
        $sources = @(
            [PSCustomObject]@{ kind = 'self'; priority = 100 },
            [PSCustomObject]@{ kind = 'bc-base'; language = $Language; priority = 10 }
        )
    }

    $tf = Split-Path -Parent $TargetFile
    $descriptors = [System.Collections.Generic.List[object]]::new()

    foreach ($s in $sources) {
        $kind = [string](& $prop $s 'kind' 'xlf')
        $priority = [int](& $prop $s 'priority' 0)
        switch -Regex ($kind) {
            'self' {
                if ($priority -eq 0) { $priority = 100 }
                $files = @(Get-ChildItem -LiteralPath $tf -Filter "*.$Language.xlf" -File -ErrorAction SilentlyContinue | ForEach-Object { $_.FullName })
                if ($files.Count -gt 0) { $descriptors.Add([PSCustomObject]@{ Files = $files; Origin = 'self'; Priority = $priority }) }
            }
            'xlf' {
                if ($priority -eq 0) { $priority = 20 }
                $spec = [string](& $prop $s 'path' '')
                if ($spec) {
                    if (-not [System.IO.Path]::IsPathRooted($spec)) { $spec = Join-Path $WorkspaceRoot $spec }
                    $name = [string](& $prop $s 'name' '')
                    $origin = if ($name) { "app:$name" } else { 'app' }
                    $descriptors.Add([PSCustomObject]@{ Files = @($spec); Origin = $origin; Priority = $priority })
                }
            }
            'bc-base' {
                if ($priority -eq 0) { $priority = 10 }
                $lang = [string](& $prop $s 'language' $Language)
                $override = [string](& $prop $s 'path' '')
                $files = @()
                if ($override) {
                    if (-not [System.IO.Path]::IsPathRooted($override)) { $override = Join-Path $WorkspaceRoot $override }
                    $files = @($override)
                }
                else {
                    # Best-effort: find the Microsoft Base/System Application <lang> XLFs already present in
                    # the warm BC artifact cache. Never triggers a download; skipped (with a note) if absent.
                    $cacheRoots = @()
                    foreach ($cacheName in @('BcArtifactCacheFolder', 'ArtifactCacheFolder')) {
                        try { $cacheRoots += (Get-ALbuildConfig -Name $cacheName) }
                        catch { Write-ALbuildLog -Level Verbose "Cache '$cacheName' unavailable: $($_.Exception.Message)" }
                    }
                    foreach ($root in ($cacheRoots | Where-Object { $_ -and (Test-Path -LiteralPath $_) })) {
                        foreach ($pat in @("Microsoft_Base Application.$lang.xlf", "Microsoft_System Application.$lang.xlf", "Microsoft_Base Application*.$lang.xlf")) {
                            $found = @(Get-ChildItem -LiteralPath $root -Filter $pat -File -Recurse -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName)
                            if ($found.Count -gt 0) { $files += $found }
                        }
                    }
                    $files = @($files | Select-Object -Unique)
                    if ($files.Count -eq 0) {
                        Write-ALbuildLog -Level Warning "Translation memory: BC base '$lang' XLF not found in the artifact cache; skipping bc-base (warm the cache or set translation.memory.sources[bc-base].path)."
                    }
                }
                if ($files.Count -gt 0) { $descriptors.Add([PSCustomObject]@{ Files = $files; Origin = 'bc-base'; Priority = $priority }) }
            }
        }
    }

    return @($descriptors)
}