#!/bin/zsh
# Completion registration file
# This file handles all completion registrations

# Load custom completion files
for completion_file in ~/.local/share/zsh/*-autocomplete.zsh(N); do
    if [ -f "$completion_file" ]; then
        source "$completion_file"
    fi
done

# Lazy loading function for completions
_lazy_load_completion() {
    local cmd="$1"
    local completion_cmd="$2"

    eval "${cmd}() {
        unfunction $cmd
        eval \"\$($completion_cmd)\"
        $cmd \"\$@\"
    }"
}

# Load lightweight completions immediately
if command -v eza &> /dev/null; then compdef eza=ls; fi

# Defer heavy completions with lazy loading
if command -v kubefwd &> /dev/null; then _lazy_load_completion kubefwd "kubefwd completion zsh"; fi
if command -v bombadil &> /dev/null; then _lazy_load_completion bombadil "bombadil generate-completions zsh"; fi

# Load remaining completions normally (they're lightweight)
if command -v rop &> /dev/null; then eval "$(rop completion zsh)"; fi
if command -v goq &> /dev/null; then eval "$(goq completion zsh)"; fi
if command -v drop &> /dev/null; then eval "$(drop completion zsh)"; fi

# Custom completion for kf function using kubectx contexts
_kf_completion() {
    local -a contexts
    # Get all contexts and extract unique cluster names (remove -read and -security suffixes)
    contexts=($(kubectx 2>/dev/null | sed 's/-read$//; s/-security$//' | sort -u))

    if [[ ${#contexts[@]} -gt 0 ]]; then
        _values 'cluster' "${contexts[@]}"
    fi
}

# Register the completion
compdef _kf_completion kf
