#compdef bitcoin-cli
# zsh completion for bitcoin-cli(1)
#     DO NOT EDIT THIS FILE BY HAND -- THIS WILL FAIL THE FUNCTIONAL TEST tool_cli_completion
# This file is auto-generated by the functional test tool_cli_completion.
# If you want to modify this file, modify test/functional/tool_cli_completion.py and re-autogenerate
# this file via the --overwrite test flag.

# Copyright (c) 2012-2024 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

# call bitcoin-cli for RPC
_bitcoin_rpc() {
    # determine already specified args necessary for RPC
    local rpcargs=()
    local -a words_array
    words_array=(${(z)BUFFER})

    for i in $words_array; do
        case "$i" in
            -conf=*|-datadir=*|-regtest|-rpc*|-testnet|-testnet4)
                rpcargs+=("$i")
                ;;
        esac
    done

    $bitcoin_cli "${rpcargs[@]}" "$@"
}

_bitcoin-cli() {
    local context state line
    local bitcoin_cli="$words[1]"

    if (( CURRENT > 6 )); then
        case ${words[CURRENT-5]} in
            descriptorprocesspsbt)
                _values 'arg' 'false' 'true'
                return 0
                ;;
        esac
    fi

    if (( CURRENT > 5 )); then
        case ${words[CURRENT-4]} in
            createpsbt|createrawtransaction|descriptorprocesspsbt|setban)
                _values 'arg' 'false' 'true'
                return 0
                ;;
            signrawtransactionwithkey)
                _values 'arg' 'ALL' 'ALL|ANYONECANPAY' 'NONE' 'NONE|ANYONECANPAY' 'SINGLE' 'SINGLE|ANYONECANPAY'
                return 0
                ;;
        esac
    fi

    if (( CURRENT > 4 )); then
        case ${words[CURRENT-3]} in
            addnode|converttopsbt|getdescriptoractivity|gettxout|gettxoutsetinfo)
                _values 'arg' 'false' 'true'
                return 0
                ;;
        esac
    fi

    if (( CURRENT > 3 )); then
        case ${words[CURRENT-2]} in
            converttopsbt|decoderawtransaction|finalizepsbt|getblockheader|getmempoolancestors|getmempooldescendants|getrawmempool|listmempooltransactions)
                _values 'arg' 'false' 'true'
                return 0
                ;;
            addnode)
                _values 'arg' 'add' 'onetry' 'remove'
                return 0
                ;;
            setban)
                _values 'arg' 'add' 'remove'
                return 0
                ;;
            estimatesmartfee)
                _values 'arg' 'CONSERVATIVE' 'ECONOMICAL' 'UNSET'
                return 0
                ;;
        esac
    fi

    # Handle previous word completions
    case "${words[CURRENT-1]}" in
        dumptxoutset|importmempool|loadtxoutset)
            _files
            return 0
            ;;
        getrawmempool|setnetworkactive|setscriptthreadsenabled)
            _values 'arg' 'false' 'true'
            return 0
            ;;
    esac
    # Handle current word completions
    case "$words[CURRENT]" in
        -conf=*)
            local conf_path=${words[CURRENT]#-conf=}
            _files -W ${conf_path:h} -g "*"
            return 0
            ;;
        -datadir=*)
            local datadir_path=${words[CURRENT]#-datadir=}
            _files -/ -W ${datadir_path:h}
            return 0
            ;;
        -*=*)
            # prevent nonsense completions
            return 0
            ;;
        *)
            local helpopts commands
            local -a opts

            # only parse -help if sensible (empty or starts with -)
            if [[ -z "$words[CURRENT]" || "$words[CURRENT]" == -* ]]; then
                helpopts="$($bitcoin_cli -help 2>&1 | awk '$1 ~ /^-/ { sub(/=.*/, "="); print $1 }')"
                opts+=(${(f)helpopts})
            fi

            # only parse help if sensible (empty or starts with letter)
            if [[ -z "$words[CURRENT]" || "$words[CURRENT]" == [a-z]* ]]; then
                commands="$(_bitcoin_rpc help 2>/dev/null | awk '$1 ~ /^[a-z]/ { print $1; }')"
                opts+=(${(f)commands})
            fi

            _describe 'bitcoin-cli options and commands' opts

            return 0
            ;;
    esac
}

# Function is now defined and will be called by zsh completion system

# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh
