#compdef swift-outdated

__swift-outdated_complete() {
    local -ar non_empty_completions=("${@:#(|:*)}")
    local -ar empty_completions=("${(M)@:#(|:*)}")
    _describe -V '' non_empty_completions -- empty_completions -P $'\'\''
}

__swift-outdated_custom_complete() {
    local -a completions
    completions=("${(@f)"$("${command_name}" "${@}" "${command_line[@]}")"}")
    if [[ "${#completions[@]}" -gt 1 ]]; then
        __swift-outdated_complete "${completions[@]:0:-1}"
    fi
}

__swift-outdated_cursor_index_in_current_word() {
    if [[ -z "${QIPREFIX}${IPREFIX}${PREFIX}" ]]; then
        printf 0
    else
        printf %s "${#${(z)LBUFFER}[-1]}"
    fi
}

_swift-outdated() {
    emulate -RL zsh -G
    setopt extendedglob nullglob numericglobsort
    unsetopt aliases banghist

    local -xr SAP_SHELL=zsh
    local -x SAP_SHELL_VERSION
    SAP_SHELL_VERSION="$(builtin emulate zsh -c 'printf %s "${ZSH_VERSION}"')"
    local -r SAP_SHELL_VERSION

    local context state state_descr line
    local -A opt_args

    local -r command_name="${words[1]}"
    local -ar command_line=("${words[@]}")
    local -ir current_word_index="$((CURRENT - 1))"

    local -i ret=1
    local -ar ___update=('patch' 'minor' 'major')
    local -ar arg_specs=(
        '(--format -f)'{--format,-f}'[The output format (markdown, json, xcode).]:format:'
        '(--ignore-prerelease -i)'{--ignore-prerelease,-i}'[Ignore pre-release versions.]'
        '(-m --only-major)'{-m,--only-major}'[Output only packages with major version updates]'
        '-v[Verbose output.]'
        ':path:_files -/'
        '(-u --include-up-to-date)'{-u,--include-up-to-date}'[Include up to date packages]'
        '--update[Update outdated packages (patch, minor, major).]:update:{__swift-outdated_complete "${___update[@]}"}'
        '*--package[Only update specific packages (repeatable).]:package:'
        '--dry-run[Show what would be updated without making changes.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
        '(-): :->command'
        '(-)*:: :->arg'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0
    case "${state}" in
    command)
        local -ar subcommands=(
            'help:Show subcommand help information.'
        )
        _describe -V subcommand subcommands
        ;;
    arg)
        case "${words[1]}" in
        help)
            "_swift-outdated_${words[1]}"
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_swift-outdated_help() {
    local -i ret=1
    local -ar arg_specs=(
        '*:subcommands:'
        '--version[Show the version.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_swift-outdated
