# 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
        COMPREPLY=( $(compgen -W '--help -h --version --install-completion' -- "${cur}") )
      else
        COMPREPLY=( $(compgen -W 'generate-docs run' -- "${cur}") )
      fi
      ;;
    1)
      case "${cmd_path[@]}" in
        "generate-docs")
          if [[ ${cur} == -* ]]; then
            COMPREPLY=( $(compgen -W '--script --output -o --format -f --include-hidden --heading-level --usage-name --help -h --version' -- "${cur}") )
          else
            case "${prev}" in
              --script)
                COMPREPLY=()
                ;;
              --output)
                COMPREPLY=( $(compgen -f -- "${cur}") )
                ;;
              -o)
                COMPREPLY=( $(compgen -f -- "${cur}") )
                ;;
              --format)
                COMPREPLY=( $(compgen -W 'markdown md html htm rst rest restructuredtext' -- "${cur}") )
                ;;
              -f)
                COMPREPLY=( $(compgen -W 'markdown md html htm rst rest restructuredtext' -- "${cur}") )
                ;;
              --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
            COMPREPLY=( $(compgen -W '--help -h --version' -- "${cur}") )
          else
            case ${positional_count} in
              0)
                COMPREPLY=( $(compgen -f -- "${cur}") )
                ;;
              1)
                COMPREPLY=()
                ;;
              *)
                COMPREPLY=()
                ;;
            esac
          fi
          ;;
        *)
          ;;
      esac
      ;;
    *)
      ;;
  esac
}

complete -F _cyclopts cyclopts
