#compdef slpkg

# Zsh completion for slpkg package manager
# This script calls the Python helper for actual completion logic.

_slpkg() {
  local -a opts           # Array for completion options

  # Zsh's $CURRENT is 1-indexed, while the Python script expects a 0-indexed cword.
  local python_cword=$((CURRENT - 1))

  # Call the Python helper script to get the completions.
  # The Python script expects:
  # 1. The 0-indexed cword (current word index)
  # 2. All command-line words, space-separated (words[@] is Zsh's array of words).
  local completions
  completions=$(python3 /usr/libexec/slpkg/shell_completion.py "$python_cword" "${words[@]}")

  # Convert the Python script's output (which is newline-separated) into a Zsh array.
  if [[ -n "$completions" ]]; then
    opts=("${(f)completions}") # The (f) flag splits by newline
  else
    opts=()
  fi

  # Use Zsh's _describe function to add the completion options.
  # This function is native to Zsh's completion system.
  _describe 'commands' opts

  return 0
}

# This registers the '_slpkg' completion function for the 'slpkg' command.
# It ensures that when you type 'slpkg' and press Tab, the _slpkg function is called.
_slpkg "$@"