#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[@]}" \
'*--font-path=[Add additional directories to search for fonts]:DIR:_files' \
'--root=[Configure the root for absolute paths]:DIR:_files' \
'*-v[Sets the level of verbosity\: 0 = none, 1 = warning & error, 2 = info, 3 = debug, 4 = trace]' \
'*--verbosity[Sets the level of verbosity\: 0 = none, 1 = warning & error, 2 = info, 3 = debug, 4 = 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[@]}" \
'--open=[Opens the output file after compilation using the default PDF viewer]' \
'--flamegraph=[Produces a flamegraph of the compilation process and saves it to the given file or to \`flamegraph.svg\` in the current working directory]' \
'-h[Print help]' \
'--help[Print help]' \
':input -- Path to input Typst file:_files' \
'::output -- Path to output PDF file:_files' \
&& ret=0
;;
(watch)
_arguments "${_arguments_options[@]}" \
'--open=[Opens the output file after compilation using the default PDF viewer]' \
'--flamegraph=[Produces a flamegraph of the compilation process and saves it to the given file or to \`flamegraph.svg\` in the current working directory]' \
'-h[Print help]' \
'--help[Print help]' \
':input -- Path to input Typst file:_files' \
'::output -- Path to output PDF file:_files' \
&& ret=0
;;
(fonts)
_arguments "${_arguments_options[@]}" \
'--variants[Also list 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
;;
(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 the input file into a PDF file' \
'c:Compiles the input file into a PDF file' \
'watch:Watches the input file and recompiles on changes' \
'w:Watches the input file and recompiles on changes' \
'fonts:List 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 the input file into a PDF file' \
'watch:Watches the input file and recompiles on changes' \
'fonts:List 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__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
