#compdef task
#
# Thin wrapper around `task __complete`. All suggestion logic lives in the
# Go engine — do not add completion logic here.

TASK_CMD="${TASK_EXE:-task}"

_task() {
    local -a args lines completions opts
    local output directive line

    # (@) preserves a trailing empty string, which the engine relies on to
    # know the cursor is on a fresh word.
    args=("${(@)words[2,CURRENT]}")
    (( ${#args} == 0 )) && args=("")

    output=$("$TASK_CMD" __complete "${args[@]}" 2>/dev/null)
    if [[ -z "$output" ]]; then
        _files
        return
    fi

    lines=("${(f)output}")
    directive="${lines[-1]#:}"
    lines=("${(@)lines[1,-2]}")

    if (( directive & 8 )); then
        local -a globs
        for line in "${lines[@]}"; do
            globs+=("*.${line}")
        done
        _files -g "(${(j:|:)globs})"
        return
    fi

    if (( directive & 16 )); then
        _path_files -/
        return
    fi

    # `:` inside the value must be escaped: _describe splits on the first
    # unescaped colon (e.g. "docs:serve" would otherwise become value "docs").
    local value desc
    for line in "${lines[@]}"; do
        if [[ "$line" == *$'\t'* ]]; then
            value="${line%%$'\t'*}"
            desc="${line#*$'\t'}"
            completions+=("${value//:/\\:}:$desc")
        else
            completions+=("${line//:/\\:}")
        fi
    done

    (( directive & 2 ))  && opts+=(-S '')
    (( directive & 32 )) && opts+=(-V)

    if (( ${#completions} > 0 )); then
        _describe -t tasks 'task' completions "${opts[@]}"
    fi

    (( directive & 4 )) && return
    _files
}

compdef _task "$TASK_CMD"
