
using namespace System.Management.Automation
using namespace System.Management.Automation.Language

Register-ArgumentCompleter -Native -CommandName 'spellout' -ScriptBlock {
    param($wordToComplete, $commandAst, $cursorPosition)

    $commandElements = $commandAst.CommandElements
    $command = @(
        'spellout'
        for ($i = 1; $i -lt $commandElements.Count; $i++) {
            $element = $commandElements[$i]
            if ($element -isnot [StringConstantExpressionAst] -or
                $element.StringConstantType -ne [StringConstantType]::BareWord -or
                $element.Value.StartsWith('-') -or
                $element.Value -eq $wordToComplete) {
                break
        }
        $element.Value
    }) -join ';'

    $completions = @(switch ($command) {
        'spellout' {
            [CompletionResult]::new('-c', '-c', [CompletionResultType]::ParameterName, 'Specify the phonetic code for encoding/decoding the input text. Default is NATO. Use `--list` option to see all available codes.')
            [CompletionResult]::new('--code', '--code', [CompletionResultType]::ParameterName, 'Specify the phonetic code for encoding/decoding the input text. Default is NATO. Use `--list` option to see all available codes.')
            [CompletionResult]::new('--input', '--input', [CompletionResultType]::ParameterName, 'Specify the the path to a custom phonetic code file.')
            [CompletionResult]::new('-l', '-l', [CompletionResultType]::ParameterName, 'Prints the available phonetic codes.')
            [CompletionResult]::new('--list', '--list', [CompletionResultType]::ParameterName, 'Prints the available phonetic codes.')
            [CompletionResult]::new('-p', '-p', [CompletionResultType]::ParameterName, 'Prints the phonetic codes for the given type.')
            [CompletionResult]::new('--print', '--print', [CompletionResultType]::ParameterName, 'Prints the phonetic codes for the given type.')
            [CompletionResult]::new('--only-code', '--only-code', [CompletionResultType]::ParameterName, 'Prints the only phonetic code for the given words.')
            [CompletionResult]::new('-d', '-d', [CompletionResultType]::ParameterName, 'Decodes given phonetic codes into string.')
            [CompletionResult]::new('--decode', '--decode', [CompletionResultType]::ParameterName, 'Decodes given phonetic codes into string.')
            [CompletionResult]::new('--gencomp', '--gencomp', [CompletionResultType]::ParameterName, 'Generates completion files.')
            [CompletionResult]::new('-h', '-h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', '--help', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('-V', '-V ', [CompletionResultType]::ParameterName, 'Print version')
            [CompletionResult]::new('--version', '--version', [CompletionResultType]::ParameterName, 'Print version')
            break
        }
    })

    $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
        Sort-Object -Property ListItemText
}
