#compdef git-p4son

# Zsh completion for git-p4son
# Also works as _git-p4son for "git p4son" subcommand completion.
#
# Delegates to "git-p4son complete" for candidate generation.
#
# Installation (pip install):
#   Add to ~/.zshrc before compinit:
#     fpath=($(git-p4son completion -d zsh) $fpath)
#     autoload -Uz compinit && compinit
#
# Installation (from repo):
#   Add to ~/.zshrc before compinit:
#     fpath=(/path/to/git-p4son/git_p4son/completions $fpath)
#     autoload -Uz compinit && compinit

_git-p4son() {
    local -a comp_words
    # words[1] is "git-p4son" or "p4son"; pass the rest including current word
    comp_words=("${words[@]:1}")

    local output
    output=$(git-p4son complete -- "${comp_words[@]}" 2>/dev/null) || return

    # Handle special directives
    if [[ "$output" == "__branch__" ]]; then
        __git_branch_names
        return
    fi

    # Parse tab-separated output into _describe format (name:description)
    local -a descriptions
    local line name desc
    while IFS=$'\t' read -r name desc; do
        [[ -z "$name" ]] && continue
        if [[ -n "$desc" ]]; then
            descriptions+=("${name}:${desc}")
        else
            descriptions+=("$name")
        fi
    done <<< "$output"

    if (( ${#descriptions} )); then
        _describe '' descriptions
    fi
}

_git-p4son "$@"
