#compdef tl

_tl() {
  local context state state_descr line
  typeset -A opt_args

  local -a options=(
    {-h,--help}"[Show this help message and exit]"
    "*--global-env-def[Predefined types from a custom global environment]: :_files"
    "*"{-I,--include-dir}"[Prepend this directory to the module search path]: :_files"
    "*--wdisable[Disable the given kind of warning]: :(branch debug hint redeclaration unknown unread unused)"
    "*--werror[Promote the given kind of warning to an error]: :(all branch debug hint redeclaration unknown unread unused)"
    "--feat-arity[Define minimum arities for functions based on optional argument annotations]: :(off on)"
    "--gen-compat[Generate compatibility code for targeting different Lua VM versions]: :(off optional required)"
    "--gen-target[Minimum targeted Lua version for generated code]: :(5.1 5.3 5.4)"
    "--skip-compat53[Skip compat53 insertions]"
    "--version[Print version and exit]"
    {-q,--quiet}"[Do not print information messages to stdout]"
    {-p,--pretend}"[Do not write to any files, type check and output what files would be generated]"
  )
  _arguments -s -S \
    $options \
    ": :_tl_cmds" \
    "*:: :->args" \
    && return 0

  case $words[1] in
    completion)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
      )
      _arguments -s -S \
        $options \
        ": :(bash zsh fish)" \
        && return 0
      ;;

    check)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
      )
      _arguments -s -S \
        $options \
        "*: :_files" \
        && return 0
      ;;

    gen)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        {-c,--check}"[Type check and fail on type errors]"
        "--keep-hashbang[Preserve hashbang line (#!) at the top of file if present]"
        {-o,--output}"[Write to <filename> instead]: :_files"
      )
      _arguments -s -S \
        $options \
        "*: :_files" \
        && return 0
      ;;

    run)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "*"{-l,--require}"[Require module for execution]: :_files"
      )
      _arguments -s -S \
        $options \
        "*: :_files" \
        && return 0
      ;;

    warnings)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
      )
      _arguments -s -S \
        $options \
        && return 0
      ;;

    types)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        {-p,--position}"[Report values in scope in position line[\:column\]]: :_files"
      )
      _arguments -s -S \
        $options \
        "*: :_files" \
        && return 0
      ;;

  esac

  return 1
}

_tl_cmds() {
  local -a commands=(
    "completion:Output a shell completion script"
    "check:Type-check one or more Teal files"
    "gen:Generate a Lua file for one or more Teal files"
    "run:Run a Teal script"
    "warnings:List each kind of warning the compiler can produce"
    "types:Report all types found in one or more Teal files"
  )
  _describe "command" commands
}

_tl
