#compdef xtool

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

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

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

_xtool() {
    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=(
            'setup:Set up xtool for iOS development'
            'auth:Manage Apple Developer Services authentication'
            'sdk:Manage the Darwin Swift SDK'
            'new:Create a new xtool SwiftPM project'
            'dev:Build and run an xtool SwiftPM project'
            'ds:Interact with Apple Developer Services'
            'devices:List devices'
            'install:Install an ipa file to your device'
            'uninstall:Uninstall an installed app'
            'launch:Launch an installed app'
            'help:Show subcommand help information.'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        setup|auth|sdk|new|dev|ds|devices|install|uninstall|launch|help)
            "_xtool_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_xtool_setup() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_auth() {
    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=(
            'login:Log in to Apple Developer Services'
            'logout:Log out of Apple Developer Services'
            'status:Get Apple Developer Services auth status'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        login|logout|status)
            "_xtool_auth_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_xtool_auth_login() {
    local -i ret=1
    local -ar ___mode=('key' 'password')
    local -ar arg_specs=(
        '(-u --username)'{-u,--username}'[Apple ID]:username:'
        '(-p --password)'{-p,--password}':password:'
        '(-m --mode)'{-m,--mode}':mode:{__xtool_complete "${___mode[@]}"}'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_auth_logout() {
    local -i ret=1
    local -ar arg_specs=(
        '(-r --reset-2fa)'{-r,--reset-2fa}'[Reset 2-factor authentication data]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_auth_status() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_sdk() {
    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=(
            'install:Install the Darwin Swift SDK'
            'update:Update the installed Darwin Swift SDK'
            'remove:Remove the Darwin Swift SDK'
            'build:Build the Darwin SDK from Xcode.xip'
            'status:Get the status of the Darwin Swift SDK'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        install|update|remove|build|status)
            "_xtool_sdk_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_xtool_sdk_install() {
    local -i ret=1
    local -ar arg_specs=(
        ':path:_files -g '\''*.xip *.app *.xtoolsdk'\'''
        '--slim[Install a slim SDK (uses less disk space). Slim SDKs cannot be updated in place.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_sdk_update() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_sdk_remove() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_sdk_build() {
    local -i ret=1
    local -ar arg_specs=(
        ':path:_files -g '\''*.xip *.app'\'''
        ':output-dir:'
        '--arch[The architecture of the Linux host the SDK is being built for.]:arch:'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_sdk_status() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_new() {
    local -i ret=1
    local -ar arg_specs=(
        ':name:'
        '--skip-setup[Skip setup steps. (default\: false)]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_dev() {
    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=(
            'generate-xcode-project:Generate Xcode project'
            'build:Build app with SwiftPM'
            'run:Build and run app with SwiftPM'
            'build-server:Run build server'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        generate-xcode-project|build|run|build-server)
            "_xtool_dev_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_xtool_dev_generate-xcode-project() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_dev_build() {
    local -i ret=1
    local -ar ___configuration=('debug' 'release')
    local -ar arg_specs=(
        '(--configuration -c)'{--configuration,-c}'[Build with configuration]:configuration:{__xtool_complete "${___configuration[@]}"}'
        '(--sign -s)'{--sign,-s}'[Codesign the built app]'
        '(--ipa -i)'{--ipa,-i}'[Output a .ipa file instead of a .app]'
        '--triple[Custom target triple to build for]:triple:'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_dev_run() {
    local -i ret=1
    local -ar ___configuration=('debug' 'release')
    local -ar arg_specs=(
        '(--configuration -c)'{--configuration,-c}'[Build with configuration]:configuration:{__xtool_complete "${___configuration[@]}"}'
        '(--simulator -s)'{--simulator,-s}'[Target the iOS Simulator]'
        '(--udid -u)'{--udid,-u}':udid:'
        '--usb'
        '--network'
        '--all'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_dev_build-server() {
    local -i ret=1
    local -ar arg_specs=(
        '--triple:triple:'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_ds() {
    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=(
            'teams:Interact with development teams'
            'certificates:Interact with certificates'
            'identifiers:Interact with bundle identifiers'
            'devices:Interact with devices'
            'profiles:Interact with profiles'
            'anisette:Test out Anisette data'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        teams|certificates|identifiers|devices|profiles|anisette)
            "_xtool_ds_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_xtool_ds_teams() {
    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=(
            'list:List teams'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        list)
            "_xtool_ds_teams_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_xtool_ds_teams_list() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_ds_certificates() {
    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=(
            'list:List certificates'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        list)
            "_xtool_ds_certificates_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_xtool_ds_certificates_list() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_ds_identifiers() {
    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=(
            'list:List bundle identifiers'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        list)
            "_xtool_ds_identifiers_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_xtool_ds_identifiers_list() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_ds_devices() {
    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=(
            'list:List devices'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        list)
            "_xtool_ds_devices_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_xtool_ds_devices_list() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_ds_profiles() {
    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=(
            'list:List profiles'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        list)
            "_xtool_ds_profiles_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

_xtool_ds_profiles_list() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_ds_anisette() {
    local -i ret=1
    local -ar arg_specs=(
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_devices() {
    local -i ret=1
    local -ar arg_specs=(
        '--usb[Which devices to search for]'
        '--network[Which devices to search for]'
        '--all[Which devices to search for]'
        '--wait[If no devices are found at first, wait until at least one is connected.]'
        '--no-wait[If no devices are found at first, wait until at least one is connected.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_install() {
    local -i ret=1
    local -ar arg_specs=(
        '(--udid -u)'{--udid,-u}':udid:'
        '--usb'
        '--network'
        '--all'
        ':path:'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_uninstall() {
    local -i ret=1
    local -ar arg_specs=(
        '(--udid -u)'{--udid,-u}':udid:'
        '--usb'
        '--network'
        '--all'
        ':bundle-id:'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_launch() {
    local -i ret=1
    local -ar arg_specs=(
        '(--udid -u)'{--udid,-u}':udid:'
        '--usb'
        '--network'
        '--all'
        ':bundle-id:'
        '*:arg:'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_xtool_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]}" = _xtool ]]; then
    _xtool "${@}"
else
    compdef _xtool xtool
fi
