
# macOS still ships Bash 3, where bash-completion may not provide
# _init_completion. This is a minimal compatible fallback.
__pinact_init_completion() {
  COMPREPLY=()
  if declare -F _comp_initialize >/dev/null 2>&1; then
    _comp_initialize "$@"
  else
    _get_comp_words_by_ref "$@" cur prev words cword
  fi
}

__pinact_build_completion_request() {
  local -a words_before_cursor=("${COMP_WORDS[@]:0:${COMP_CWORD}}")
  local current_word="${COMP_WORDS[COMP_CWORD]}"

  if [[ "${current_word}" == "-"* ]]; then
    printf '%s %s --generate-shell-completion' "${words_before_cursor[*]}" "${current_word}"
  else
    printf '%s --generate-shell-completion' "${words_before_cursor[*]}"
  fi
}

# Keep Bash 3 compatibility: associative arrays require Bash 4+, so
# descriptions are looked up via parallel indexed arrays.
__pinact_find_description_for() {
  local candidate="$1"
  local i

  for i in "${!__cli_completion_tokens[@]}"; do
    if [[ "${__cli_completion_tokens[i]}" == "${candidate}" ]]; then
      printf '%s' "${__cli_completion_descriptions[i]}"
      return 0
    fi
  done

  return 1
}

__pinact_bash_autocomplete() {
  local words=("${COMP_WORDS[@]}")

  if [[ "${words[0]}" != "source" ]]; then
    local cur opts
    local cword="${COMP_CWORD}"
    local request_comp

    COMPREPLY=()
    cur="${words[$cword]}"

    __pinact_init_completion -n "=:" || return

    request_comp="$(__pinact_build_completion_request)"
    opts=$(eval "${request_comp}" 2>/dev/null)

    # Completion output lines use "token:description" format.
    # Keep token/description in parallel arrays for Bash 3 compatibility.
    __cli_completion_tokens=()
    __cli_completion_descriptions=()

    local line
    local longest=0
    while IFS=$'\n' read -r line; do
      local token="${line}"
      local description=""
      
      if [[ "${line}" == *:* ]]; then
        token="${line%:*}"
        description="${line#*:}"
      fi

      if [[ -z "${token}" ]]; then
        continue
      fi

      __cli_completion_tokens+=("${token}")
      __cli_completion_descriptions+=("${description}")
      (( ${#token} > longest )) && longest=${#token}
    done <<< "${opts}"

    local matches=( $(compgen -W "${__cli_completion_tokens[*]}" -- "${cur}") )

    # COMP_TYPE=63 means Bash is listing matches (usually on second TAB).
    if [[ "${COMP_TYPE:-}" == "63" && ${#matches[@]} -gt 0 ]]; then
      local listed=()
      local candidate
      for candidate in "${matches[@]}"; do
        local desc="$(__pinact_find_description_for "${candidate}")"

        if [[ -n "${desc}" ]]; then
          local padded="$(printf '%-*s' "${longest}" "${candidate}")"
          listed+=("${padded}  -- ${desc}")
        else
          listed+=("${candidate}")
        fi
      done
      COMPREPLY=("${listed[@]}")
    else
      COMPREPLY=("${matches[@]}")
    fi

    return 0
  fi
}

complete -o bashdefault -o default -o nospace -F __pinact_bash_autocomplete pinact
