#compdef swift-section

__swift-section_complete() {
    local -ar non_empty_completions=("${@:#(|:*)}")
    local -ar empty_completions=("${(M)@:#(|:*)}")
    _describe -V '' non_empty_completions -- empty_completions -P $'\'\''
}

__swift-section_custom_complete() {
    local -a completions
    completions=("${(@f)"$("${command_name}" "${@}" "${command_line[@]}")"}")
    if [[ "${#completions[@]}" -gt 1 ]]; then
        __swift-section_complete "${completions[@]:0:-1}"
    fi
}

__swift-section_cursor_index_in_current_word() {
    if [[ -z "${QIPREFIX}${IPREFIX}${PREFIX}" ]]; then
        printf 0
    else
        printf %s "${#${(z)LBUFFER}[-1]}"
    fi
}

_swift-section() {
    emulate -RL zsh -G
    setopt extendedglob nullglob numericglobsort
    unsetopt aliases banghist

    local -xr SAP_SHELL=zsh
    local -x SAP_SHELL_VERSION
    SAP_SHELL_VERSION="$(builtin emulate zsh -c 'printf %s "${ZSH_VERSION}"')"
    local -r SAP_SHELL_VERSION

    local context state state_descr line
    local -A opt_args

    local -r command_name="${words[1]}"
    local -ar command_line=("${words[@]}")
    local -ir current_word_index="$((CURRENT - 1))"

    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
        '(-): :->command'
        '(-)*:: :->arg'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0
    case "${state}" in
    command)
        local -ar subcommands=(
            'dump:Dump Swift information from a Mach-O file or dyld shared cache.'
            'interface:Generate Swift interface from a Mach-O file.'
            'help:Show subcommand help information.'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        dump|interface|help)
            "_swift-section_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_swift-section_dump() {
    local -i ret=1
    local -ar ___architecture=('x86_64' 'arm64' 'arm64e')
    local -ar ___demangle_options=('default' 'simplified' 'interface')
    local -ar ___sections=('types' 'protocols' 'protocolConformances' 'associatedTypes')
    local -ar ___color_scheme=('none' 'light' 'dark')
    local -ar arg_specs=(
        ':file-path:_files'
        '(--cache-image-path -p)'{--cache-image-path,-p}'[The path to the dyld shared cache image. If filePath is a Mach-O file, this option is ignored.]:cache-image-path:'
        '(--cache-image-name -n)'{--cache-image-name,-n}'[The name of the dyld shared cache image. If filePath is a Mach-O file, this option is ignored.]:cache-image-name:'
        '--dyld-shared-cache[The flag to indicate if the Mach-O file is a dyld shared cache.]'
        '--uses-system-dyld-shared-cache[Use the current dyld shared cache instead of the specified one. This option is ignored if filePath is a Mach-O file.]'
        '(--architecture -a)'{--architecture,-a}'[The architecture of the Mach-O file. If not specified, the current architecture will be used.]:architecture:{__swift-section_complete "${___architecture[@]}"}'
        '--demangle-options[Specify the Swift demangle options to use.]:demangle-options:{__swift-section_complete "${___demangle_options[@]}"}'
        '--enable-synthesize-sugar-on-types'
        '--disable-synthesize-sugar-on-types'
        '--enable-display-debugger-generated-module'
        '--disable-display-debugger-generated-module'
        '--enable-qualify-entities'
        '--disable-qualify-entities'
        '--enable-display-extension-contexts'
        '--disable-display-extension-contexts'
        '--enable-display-unmangled-suffix'
        '--disable-display-unmangled-suffix'
        '--enable-display-module-names'
        '--disable-display-module-names'
        '--enable-display-generic-specializations'
        '--disable-display-generic-specializations'
        '--enable-display-protocol-conformances'
        '--disable-display-protocol-conformances'
        '--enable-display-where-clauses'
        '--disable-display-where-clauses'
        '--enable-display-entity-types'
        '--disable-display-entity-types'
        '--enable-shorten-partial-apply'
        '--disable-shorten-partial-apply'
        '--enable-shorten-thunk'
        '--disable-shorten-thunk'
        '--enable-shorten-value-witness'
        '--disable-shorten-value-witness'
        '--enable-shorten-archetype'
        '--disable-shorten-archetype'
        '--enable-show-private-discriminators'
        '--disable-show-private-discriminators'
        '--enable-show-function-argument-types'
        '--disable-show-function-argument-types'
        '--enable-show-async-resume-partial'
        '--disable-show-async-resume-partial'
        '--enable-display-stdlib-module'
        '--disable-display-stdlib-module'
        '--enable-display-objc-module'
        '--disable-display-objc-module'
        '--enable-print-for-type-name'
        '--disable-print-for-type-name'
        '--enable-show-closure-signature'
        '--disable-show-closure-signature'
        '--enable-show-module-in-dependent-member-type'
        '--disable-show-module-in-dependent-member-type'
        '(--output-path -o)'{--output-path,-o}'[The output path for the dump. If not specified, the output will be printed to the console.]:output-path:_files'
        '(--sections -s)'{--sections,-s}'[The sections to dump. If not specified, all sections will be dumped.]:sections:{__swift-section_complete "${___sections[@]}"}'
        '(--color-scheme -c)'{--color-scheme,-c}'[The color scheme for the output.]:color-scheme:{__swift-section_complete "${___color_scheme[@]}"}'
        '--emit-offset-comments[Generate field offset and PWT offset comments, if possible]'
        '--emit-member-addresses[Generate member address comments for each member symbol]'
        '--emit-vtable-offsets[Generate vtable offset comments for class methods]'
        '--emit-expanded-field-offsets[Expand nested struct fields with their absolute offsets (implies --emit-offset-comments)]'
        '--emit-pwt-addresses[Generate PWT (Protocol Witness Table) address comments for protocol conformances]'
        '--preferred-binary-order[The definitions of types and protocols will be output in the order they are stored in the binary.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_swift-section_interface() {
    local -i ret=1
    local -ar ___architecture=('x86_64' 'arm64' 'arm64e')
    local -ar ___color_scheme=('none' 'light' 'dark')
    local -ar arg_specs=(
        ':file-path:_files'
        '(--cache-image-path -p)'{--cache-image-path,-p}'[The path to the dyld shared cache image. If filePath is a Mach-O file, this option is ignored.]:cache-image-path:'
        '(--cache-image-name -n)'{--cache-image-name,-n}'[The name of the dyld shared cache image. If filePath is a Mach-O file, this option is ignored.]:cache-image-name:'
        '--dyld-shared-cache[The flag to indicate if the Mach-O file is a dyld shared cache.]'
        '--uses-system-dyld-shared-cache[Use the current dyld shared cache instead of the specified one. This option is ignored if filePath is a Mach-O file.]'
        '(--architecture -a)'{--architecture,-a}'[The architecture of the Mach-O file. If not specified, the current architecture will be used.]:architecture:{__swift-section_complete "${___architecture[@]}"}'
        '(--output-path -o)'{--output-path,-o}'[The output path for the dump. If not specified, the output will be printed to the console.]:output-path:_files'
        '--show-c-imported-types[Show imported C types in the generated Swift interface.]'
        '--parse-opaque-return-type[Parse opaque return value, this option is an experimental feature and may result in parsing errors for complex return types.]'
        '--emit-offset-comments[Generate field offset and PWT offset comments, if possible]'
        '--emit-member-addresses[Generate member address comments for each member symbol]'
        '--emit-vtable-offsets[Generate vtable offset comments for class methods and computed properties]'
        '--emit-expanded-field-offsets[Expand nested struct fields with their absolute offsets (implies --emit-offset-comments)]'
        '--sort-members-by-offset[Sort members by binary layout offset instead of grouping by category]'
        '(--color-scheme -c)'{--color-scheme,-c}'[The color scheme for the output.]:color-scheme:{__swift-section_complete "${___color_scheme[@]}"}'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_swift-section_help() {
    local -i ret=1
    local -ar arg_specs=(
        '*:subcommands:'
        '--version[Show the version.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

if [[ "${funcstack[1]}" = _swift-section ]]; then
    _swift-section "${@}"
else
    compdef _swift-section swift-section
fi
