#compdef xcbeautify

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

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

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

_xcbeautify() {
    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 ___renderer=('terminal' 'github-actions' 'teamcity' 'azure-devops-pipelines')
    local -ar ___report=('junit')
    local -ar arg_specs=(
        '(-q --quiet)'{-q,--quiet}'[Only print tasks that have warnings or errors.]'
        '(--quieter -qq)'{--quieter,-qq}'[Only print tasks that have errors.]'
        '--preserve-unbeautified[Preserves unbeautified output lines.]'
        '--is-ci[Print test result too under quiet/quieter flag.]'
        '--disable-colored-output[Disable the colored output]'
        '--disable-logging[Suppress the xcbeautify information table when xcbeautify starts. It includes the active xcbeautify version.]'
        '--renderer[Specify a renderer to format raw xcodebuild output.]:renderer:{__xcbeautify_complete "${___renderer[@]}"}'
        '*--report[Generate the specified reports.]:report:{__xcbeautify_complete "${___report[@]}"}'
        '--report-path[The path to use when generating reports]:report-path:'
        '--junit-report-filename[The name of JUnit report file name]:junit-report-filename:'
        '--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 && ret=0
        ;;
    arg)
        case "${words[1]}" in
        help)
            "_xcbeautify_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

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

    return "${ret}"
}

if [[ "${funcstack[1]}" = _xcbeautify ]]; then
    _xcbeautify "${@}"
else
    compdef _xcbeautify xcbeautify
fi
