#compdef workforest wf
# zsh completion for workforest / wf — installed to zsh site-functions.
# Dynamic candidates are delegated to `workforest --complete TOPIC`.

local topic cmd line name desc
local -a items cmds
cmd="${words[2]:-}"
if (( CURRENT == 2 )); then
    topic=commands
elif [[ "${words[CURRENT-1]}" == (-o|--opener) ]]; then
    topic=openers
else
    case "$cmd" in
        create) topic=branches ;;
        open|delete|checkout) topic=worktrees ;;
        run) topic=scripts ;;
        claude) topic=claude-sessions ;;
        tui|list|init|config|shell-init) topic=none ;;
        *) topic=worktrees ;;
    esac
fi
if [[ "$topic" != none ]]; then
    items=(${(f)"$(workforest --complete "$topic" 2>/dev/null)"})
    if [[ "$topic" == (commands|openers|branches) ]]; then
        # NAME<TAB>DESCRIPTION → described candidates.
        for line in "${items[@]}"; do
            name="${line%%$'\t'*}"
            desc="${line#*$'\t'}"
            cmds+=("${name//:/\\:}:${desc}")
        done
        (( ${#cmds} )) && _describe -t "$topic" "$topic" cmds
    else
        (( ${#items} )) && compadd -a items
    fi
fi
