# Bash completion for cyclopts
# Generated by Cyclopts

_cyclopts() {
  local cur prev

  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"

  # Build list of options that take values (to skip their arguments)
  local options_with_values='--format --heading-level --output --script --usage-name -f -o'

  # Build list of all valid command names (to distinguish from positionals)
  local all_commands='generate-docs run'

  # Detect command path by collecting valid command words only
  local -a cmd_path=()
  local i skip_next=0
  for ((i=1; i<COMP_CWORD; i++)); do
    local word="${COMP_WORDS[i]}"
    if [[ $skip_next -eq 1 ]]; then
      skip_next=0
      continue
    fi
    if [[ $word =~ ^- ]]; then
      # Check if this option takes a value
      if [[ " $options_with_values " =~ " $word " ]]; then
        skip_next=1
      fi
    else
      # Non-option word - only add to cmd_path if it's a valid command
      if [[ " $all_commands " =~ " $word " ]]; then
        cmd_path+=("$word")
      fi
    fi
  done

  # Count positionals (non-option words after command path)
  local positional_count=0
  local cmd_path_len=${#cmd_path[@]}
  skip_next=0
  local cmd_depth=0
  for ((i=1; i<COMP_CWORD; i++)); do
    local word="${COMP_WORDS[i]}"
    if [[ $skip_next -eq 1 ]]; then
      skip_next=0
      continue
    fi
    if [[ $word =~ ^- ]]; then
      if [[ " $options_with_values " =~ " $word " ]]; then
        skip_next=1
      fi
    else
      # Non-option word
      if [[ $cmd_depth -lt $cmd_path_len ]]; then
        # Still in command path
        ((cmd_depth++))
      else
        # Past command path, this is a positional
        ((positional_count++))
      fi
    fi
  done

  # Determine command level and generate completions
  case "${#cmd_path[@]}" in
    0)
      if [[ ${cur} == -* ]]; then
        local -a _c=("--help" "-h" "--version" "--install-completion")
        COMPREPLY=()
        for _x in "${_c[@]}"; do
          [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
        done
      else
        local -a _c=("generate-docs" "run")
        COMPREPLY=()
        for _x in "${_c[@]}"; do
          [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
        done
      fi
      ;;
    1)
      case "${cmd_path[@]}" in
        "generate-docs")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--script" "--output" "-o" "--format" "-f" "--include-hidden" "--heading-level" "--usage-name" "--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            local _value_prev="${prev}"
            if [[ "$_value_prev" == "=" && $COMP_CWORD -ge 2 ]]; then
              _value_prev="${COMP_WORDS[COMP_CWORD-2]}"
            fi
            case "$_value_prev" in
              --script)
                COMPREPLY=()
                ;;
              --output)
                COMPREPLY=( $(compgen -f -- "${cur}") )
                ;;
              -o)
                COMPREPLY=( $(compgen -f -- "${cur}") )
                ;;
              --format)
                local -a _c=("markdown" "md" "html" "htm" "rst" "rest" "restructuredtext")
                COMPREPLY=()
                for _x in "${_c[@]}"; do
                  [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
                done
                ;;
              -f)
                local -a _c=("markdown" "md" "html" "htm" "rst" "rest" "restructuredtext")
                COMPREPLY=()
                for _x in "${_c[@]}"; do
                  [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
                done
                ;;
              --heading-level)
                COMPREPLY=()
                ;;
              --usage-name)
                COMPREPLY=()
                ;;
              *)
                case ${positional_count} in
                  0)
                    COMPREPLY=()
                    ;;
                  1)
                    COMPREPLY=( $(compgen -f -- "${cur}") )
                    ;;
                  *)
                    COMPREPLY=()
                    ;;
                esac
                ;;
            esac
          fi
          ;;
        "run")
          if [[ ${cur} == -* ]]; then
            local -a _c=("--help" "-h" "--version")
            COMPREPLY=()
            for _x in "${_c[@]}"; do
              [[ "$_x" == "${cur}"* ]] && COMPREPLY+=("$_x")
            done
          else
            case ${positional_count} in
              0)
                COMPREPLY=( $(compgen -f -- "${cur}") )
                ;;
              *)
                COMPREPLY=()
                ;;
            esac
          fi
          ;;
        *)
          ;;
      esac
      ;;
    *)
      ;;
  esac
}

complete -F _cyclopts cyclopts
