private/Language.ps1

# FR-023: explicit language choice, independent of the OS's own $PSUICulture, persisted
# locally. Uses PowerShell's native Import-LocalizedData/.psd1 mechanism (research.md) -
# no external i18n dependency.

$script:OctaSupportedCultures = @('es-ES', 'en-US')

function Get-OctaSettingsPath {
    $folder = Join-Path $env:LOCALAPPDATA 'Octa'
    if (-not (Test-Path $folder)) {
        New-Item -ItemType Directory -Path $folder -Force | Out-Null
    }
    return Join-Path $folder 'settings.json'
}

function Get-OctaLanguage {
    $path = Get-OctaSettingsPath
    if (Test-Path $path) {
        $settings = Get-Content -Path $path -Raw | ConvertFrom-Json
        if ($settings.Culture -in $script:OctaSupportedCultures) {
            return $settings.Culture
        }
    }
    return $null
}

function Set-OctaLanguage {
    param([Parameter(Mandatory)][ValidateSet('es-ES', 'en-US')][string]$Culture)
    $path = Get-OctaSettingsPath
    [pscustomobject]@{ Culture = $Culture } | ConvertTo-Json | Set-Content -Path $path -Encoding utf8
}

function Show-OctaLanguageMenu {
    <#
        First-launch (or explicit change) language submenu. Reuses Show-OctaMenu (Menu.ps1) -
        this used to be its own hand-rolled Write-Host prompt, which never got the padding fix
        applied to every other screen and left stale characters from the previous (wider) screen
        showing through on real hardware (e.g. "Espanolentas" - "Espanol" overwriting only the
        first 12 columns of a leftover "Herramientas" row).
    #>

    $items = @(
        [pscustomobject]@{ Key = '1'; Label = 'Español'; Description = 'Usar la interfaz en español' },
        [pscustomobject]@{ Key = '2'; Label = 'English'; Description = 'Use the interface in English' }
    )
    $chosen = Show-OctaMenu -Items $items -Header 'Idioma / Language' `
        -NavHint 'Flechas para navegar - Enter para elegir / Up-Down to navigate - Enter to select' `
        -Tagline 'Windows 11 Debloat -- Local. Transparente. Open Source. / Local. Transparent. Open Source.'
    if ($chosen -eq '1') { return 'es-ES' }
    return 'en-US'
}

function Get-OctaStrings {
    <#
        Resolves the active language (persisted choice, prompting on first run) and imports
        its string table via the native Import-LocalizedData -UICulture override - never the
        OS's own $PSUICulture by default.

        In a non-interactive context (no console attached, redirected input - e.g. CI, a
        scheduled task, a remote exec without a pty) [Console]::ReadKey throws, so the
        language prompt is skipped in favor of a silent 'en-US' default for THIS call only -
        the choice is deliberately not persisted, so the next genuinely interactive run still
        prompts normally instead of being silently locked into a default the user never chose.
    #>

    [CmdletBinding()]
    param()

    $culture = Get-OctaLanguage
    if (-not $culture) {
        if (Test-OctaInteractiveConsole) {
            $culture = Show-OctaLanguageMenu
            Set-OctaLanguage -Culture $culture
        }
        else {
            $culture = 'en-US'
        }
    }

    return Import-LocalizedData -BaseDirectory $PSScriptRoot\.. -FileName 'Octa.psd1' -UICulture $culture
}