#compdef cyclopts

_cyclopts() {
  local line state

  _arguments -C \
    '--help[Display this message and exit.]' \
    '-h[Display this message and exit.]' \
    '--version[Display application version.]' \
    '--install-completion[Register shell-completion for the cyclopts CLI itself.]' \
    '1: :->cmds' \
    '*::arg:->args'

  case $state in
    cmds)
      local -a commands
      commands=(
        'generate-docs:Generate documentation for a Cyclopts application.'
        'run:Run a Cyclopts application from a Python script with dynamic shell completion.'
      )
      _describe -t commands 'command' commands
      ;;
    args)
      case $words[1] in
        generate-docs)
          _arguments \
            '1:Python script path, optionally with '\''\:app_object'\'' notation to specify the App o…' \
            '2:Output file path. If not specified, prints to stdout.:_files' \
            '--script[Python script path, optionally with '\''\:app_object'\'' notation to specify the App o…]:script' \
            '--output[Output file path. If not specified, prints to stdout.]:output:_files' \
            '-o[Output file path. If not specified, prints to stdout.]:o:_files' \
            '--format[Output format for documentation. If not specified, inferred from output file ex…]:format:(markdown md html htm rst rest restructuredtext)' \
            '-f[Output format for documentation. If not specified, inferred from output file ex…]:f:(markdown md html htm rst rest restructuredtext)' \
            '--include-hidden[Include hidden commands in documentation.]' \
            '--heading-level[Starting heading level for markdown format.]:heading-level' \
            '--usage-name[Replace the app name shown in Usage\: lines with this string. For example, \"uv r…]:usage-name' \
            '--help[Display this message and exit.]' \
            '-h[Display this message and exit.]' \
            '--version[Display application version.]'

          ;;
        run)
          local script_path
          local -a completions
          local -a remaining_words

          # If completing first argument (the script path), suggest files
          if [[ $CURRENT -eq 2 ]]; then
            _files
            return
          fi

          # Get absolute path to the script file
          script_path=${words[2]}
          script_path=${script_path:a}
          if [[ -f $script_path ]]; then
            remaining_words=(${words[3,-1]})
            local result
            local cmd

            if command -v cyclopts &>/dev/null; then
              cmd="cyclopts"
            else
              return
            fi
            # Call back into cyclopts to get dynamic completions from the script
            result=$($cmd _complete run "$script_path" "${remaining_words[@]}" 2>/dev/null)
            if [[ -n $result ]]; then
              # Parse and display completion results
              completions=()
              while IFS= read -r line; do
                completions+=($line)
              done <<< $result
              _describe 'command' completions
            fi
          fi
          ;;
      esac
      ;;
  esac
}

