#compdef typst

autoload -U is-at-least

_typst() {
    typeset -A opt_args
    typeset -a _arguments_options
    local ret=1

    if is-at-least 5.2; then
        _arguments_options=(-s -S -C)
    else
        _arguments_options=(-s -C)
    fi

    local context curcontext="$curcontext" state line
    _arguments "${_arguments_options[@]}" \
'*-v[Sets the level of logging verbosity\: -v = warning & error, -vv = info, -vvv = debug, -vvvv = trace]' \
'*--verbosity[Sets the level of logging verbosity\: -v = warning & error, -vv = info, -vvv = debug, -vvvv = trace]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
":: :_typst_commands" \
"*::: :->typst" \
&& ret=0
    case $state in
    (typst)
        words=($line[1] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:typst-command-$line[1]:"
        case $line[1] in
            (compile)
_arguments "${_arguments_options[@]}" \
'--root=[Configures the project root]:DIR:_files' \
'*--font-path=[Adds additional directories to search for fonts]:DIR:_files' \
'--diagnostic-format=[In which format to emit diagnostics]:DIAGNOSTIC_FORMAT:(human short)' \
'--open=[Opens the output file using the default viewer after compilation]' \
'--ppi=[The PPI (pixels per inch) to use for PNG export]:PPI: ' \
'--flamegraph=[Produces a flamegraph of the compilation process]' \
'-h[Print help]' \
'--help[Print help]' \
':input -- Path to input Typst file:_files' \
'::output -- Path to output PDF file or PNG file(s):_files' \
&& ret=0
;;
(watch)
_arguments "${_arguments_options[@]}" \
'--root=[Configures the project root]:DIR:_files' \
'*--font-path=[Adds additional directories to search for fonts]:DIR:_files' \
'--diagnostic-format=[In which format to emit diagnostics]:DIAGNOSTIC_FORMAT:(human short)' \
'--open=[Opens the output file using the default viewer after compilation]' \
'--ppi=[The PPI (pixels per inch) to use for PNG export]:PPI: ' \
'--flamegraph=[Produces a flamegraph of the compilation process]' \
'-h[Print help]' \
'--help[Print help]' \
':input -- Path to input Typst file:_files' \
'::output -- Path to output PDF file or PNG file(s):_files' \
&& ret=0
;;
(query)
_arguments "${_arguments_options[@]}" \
'--root=[Configures the project root]:DIR:_files' \
'*--font-path=[Adds additional directories to search for fonts]:DIR:_files' \
'--diagnostic-format=[In which format to emit diagnostics]:DIAGNOSTIC_FORMAT:(human short)' \
'--field=[Extract just one field from all retrieved elements]:FIELD: ' \
'--format=[The format to serialization in]:FORMAT:(json yaml)' \
'--one[Expect and retrieve exactly one element]' \
'-h[Print help]' \
'--help[Print help]' \
':input -- Path to input Typst file:_files' \
':selector -- Define what elements to retrieve:' \
&& ret=0
;;
(fonts)
_arguments "${_arguments_options[@]}" \
'*--font-path=[Adds additional directories to search for fonts]:DIR:_files' \
'--variants[Also lists style variants of each font family]' \
'-h[Print help]' \
'--help[Print help]' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" \
":: :_typst__help_commands" \
"*::: :->help" \
&& ret=0

    case $state in
    (help)
        words=($line[1] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:typst-help-command-$line[1]:"
        case $line[1] in
            (compile)
_arguments "${_arguments_options[@]}" \
&& ret=0
;;
(watch)
_arguments "${_arguments_options[@]}" \
&& ret=0
;;
(query)
_arguments "${_arguments_options[@]}" \
&& ret=0
;;
(fonts)
_arguments "${_arguments_options[@]}" \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" \
&& ret=0
;;
        esac
    ;;
esac
;;
        esac
    ;;
esac
}

(( $+functions[_typst_commands] )) ||
_typst_commands() {
    local commands; commands=(
'compile:Compiles an input file into a PDF or PNG file' \
'c:Compiles an input file into a PDF or PNG file' \
'watch:Watches an input file and recompiles on changes' \
'w:Watches an input file and recompiles on changes' \
'query:Processes an input file to extract provided metadata' \
'fonts:Lists all discovered fonts in system and custom font paths' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'typst commands' commands "$@"
}
(( $+functions[_typst__compile_commands] )) ||
_typst__compile_commands() {
    local commands; commands=()
    _describe -t commands 'typst compile commands' commands "$@"
}
(( $+functions[_typst__help__compile_commands] )) ||
_typst__help__compile_commands() {
    local commands; commands=()
    _describe -t commands 'typst help compile commands' commands "$@"
}
(( $+functions[_typst__fonts_commands] )) ||
_typst__fonts_commands() {
    local commands; commands=()
    _describe -t commands 'typst fonts commands' commands "$@"
}
(( $+functions[_typst__help__fonts_commands] )) ||
_typst__help__fonts_commands() {
    local commands; commands=()
    _describe -t commands 'typst help fonts commands' commands "$@"
}
(( $+functions[_typst__help_commands] )) ||
_typst__help_commands() {
    local commands; commands=(
'compile:Compiles an input file into a PDF or PNG file' \
'watch:Watches an input file and recompiles on changes' \
'query:Processes an input file to extract provided metadata' \
'fonts:Lists all discovered fonts in system and custom font paths' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'typst help commands' commands "$@"
}
(( $+functions[_typst__help__help_commands] )) ||
_typst__help__help_commands() {
    local commands; commands=()
    _describe -t commands 'typst help help commands' commands "$@"
}
(( $+functions[_typst__help__query_commands] )) ||
_typst__help__query_commands() {
    local commands; commands=()
    _describe -t commands 'typst help query commands' commands "$@"
}
(( $+functions[_typst__query_commands] )) ||
_typst__query_commands() {
    local commands; commands=()
    _describe -t commands 'typst query commands' commands "$@"
}
(( $+functions[_typst__help__watch_commands] )) ||
_typst__help__watch_commands() {
    local commands; commands=()
    _describe -t commands 'typst help watch commands' commands "$@"
}
(( $+functions[_typst__watch_commands] )) ||
_typst__watch_commands() {
    local commands; commands=()
    _describe -t commands 'typst watch commands' commands "$@"
}

if [ "$funcstack[1]" = "_typst" ]; then
    _typst "$@"
else
    compdef _typst typst
fi
