#!/bin/bash

__xtool_cursor_index_in_current_word() {
    local remaining="${COMP_LINE}"

    local word
    for word in "${COMP_WORDS[@]::COMP_CWORD}"; do
        remaining="${remaining##*([[:space:]])"${word}"*([[:space:]])}"
    done

    local -ir index="$((COMP_POINT - ${#COMP_LINE} + ${#remaining}))"
    if [[ "${index}" -le 0 ]]; then
        printf 0
    else
        printf %s "${index}"
    fi
}

# positional arguments:
#
# - 1: the current (sub)command's count of positional arguments
#
# required variables:
#
# - repeating_flags: the repeating flags that the current (sub)command can accept
# - non_repeating_flags: the non-repeating flags that the current (sub)command can accept
# - repeating_options: the repeating options that the current (sub)command can accept
# - non_repeating_options: the non-repeating options that the current (sub)command can accept
# - positional_number: value ignored
# - unparsed_words: unparsed words from the current command line
#
# modified variables:
#
# - non_repeating_flags: remove flags for this (sub)command that are already on the command line
# - non_repeating_options: remove options for this (sub)command that are already on the command line
# - positional_number: set to the current positional number
# - unparsed_words: remove all flags, options, and option values for this (sub)command
__xtool_offer_flags_options() {
    local -ir positional_count="${1}"
    positional_number=0

    local was_flag_option_terminator_seen=false
    local is_parsing_option_value=false

    local -ar unparsed_word_indices=("${!unparsed_words[@]}")
    local -i word_index
    for word_index in "${unparsed_word_indices[@]}"; do
        if "${is_parsing_option_value}"; then
            # This word is an option value:
            # Reset marker for next word iff not currently the last word
            [[ "${word_index}" -ne "${unparsed_word_indices[${#unparsed_word_indices[@]} - 1]}" ]] && is_parsing_option_value=false
            unset "unparsed_words[${word_index}]"
            # Do not process this word as a flag or an option
            continue
        fi

        local word="${unparsed_words["${word_index}"]}"
        if ! "${was_flag_option_terminator_seen}"; then
            case "${word}" in
            --)
                unset "unparsed_words[${word_index}]"
                # by itself -- is a flag/option terminator, but if it is the last word, it is the start of a completion
                if [[ "${word_index}" -ne "${unparsed_word_indices[${#unparsed_word_indices[@]} - 1]}" ]]; then
                    was_flag_option_terminator_seen=true
                fi
                continue
                ;;
            -*)
                # ${word} is a flag or an option
                # If ${word} is an option, mark that the next word to be parsed is an option value
                local option
                for option in "${repeating_options[@]}" "${non_repeating_options[@]}"; do
                    [[ "${word}" = "${option}" ]] && is_parsing_option_value=true && break
                done

                # Remove ${word} from ${non_repeating_flags} or ${non_repeating_options} so it isn't offered again
                local not_found=true
                local -i index
                for index in "${!non_repeating_flags[@]}"; do
                    if [[ "${non_repeating_flags[${index}]}" = "${word}" ]]; then
                        unset "non_repeating_flags[${index}]"
                        non_repeating_flags=("${non_repeating_flags[@]}")
                        not_found=false
                        break
                    fi
                done
                if "${not_found}"; then
                    for index in "${!non_repeating_flags[@]}"; do
                        if [[ "${non_repeating_flags[${index}]}" = "${word}" ]]; then
                            unset "non_repeating_flags[${index}]"
                            non_repeating_flags=("${non_repeating_flags[@]}")
                            break
                        fi
                    done
                fi
                unset "unparsed_words[${word_index}]"
                continue
                ;;
            esac
        fi

        # ${word} is neither a flag, nor an option, nor an option value
        if [[ "${positional_number}" -lt "${positional_count}" || "${positional_count}" -lt 0 ]]; then
            # ${word} is a positional
            ((positional_number++))
            unset "unparsed_words[${word_index}]"
        else
            if [[ -z "${word}" ]]; then
                # Could be completing a flag, option, or subcommand
                positional_number=-1
            else
                # ${word} is a subcommand or invalid, so stop processing this (sub)command
                positional_number=-2
            fi
            break
        fi
    done

    unparsed_words=("${unparsed_words[@]}")

    if\
        ! "${was_flag_option_terminator_seen}"\
        && ! "${is_parsing_option_value}"\
        && [[ ("${cur}" = -* && "${positional_number}" -ge 0) || "${positional_number}" -eq -1 ]]
    then
        COMPREPLY+=($(compgen -W "${repeating_flags[*]} ${non_repeating_flags[*]} ${repeating_options[*]} ${non_repeating_options[*]}" -- "${cur}"))
    fi
}

__xtool_add_completions() {
    local completion
    while IFS='' read -r completion; do
        COMPREPLY+=("${completion}")
    done < <(IFS=$'\n' compgen "${@}" -- "${cur}")
}

__xtool_custom_complete() {
    if [[ -n "${cur}" || -z ${COMP_WORDS[${COMP_CWORD}]} || "${COMP_LINE:${COMP_POINT}:1}" != ' ' ]]; then
        local -ar words=("${COMP_WORDS[@]}")
    else
        local -ar words=("${COMP_WORDS[@]::${COMP_CWORD}}" '' "${COMP_WORDS[@]:${COMP_CWORD}}")
    fi

    "${COMP_WORDS[0]}" "${@}" "${words[@]}"
}

_xtool() {
    local state
    state="$(shopt -p;shopt -po)"
    trap "${state//$'\n'/;}" RETURN
    shopt -s extglob
    set +o history +o posix

    local -xr SAP_SHELL=bash
    local -x SAP_SHELL_VERSION
    SAP_SHELL_VERSION="$(IFS='.';printf %s "${BASH_VERSINFO[*]}")"
    local -r SAP_SHELL_VERSION

    local -r cur="${2}"
    local -r prev="${3}"

    local -i positional_number
    local -a unparsed_words=("${COMP_WORDS[@]:1:${COMP_CWORD}}")

    local -a repeating_flags=()
    local -a non_repeating_flags=(--version -h --help)
    local -a repeating_options=()
    local -a non_repeating_options=()
    __xtool_offer_flags_options 0

    # Offer subcommand / subcommand argument completions
    local -r subcommand="${unparsed_words[0]}"
    unset 'unparsed_words[0]'
    unparsed_words=("${unparsed_words[@]}")
    case "${subcommand}" in
    setup|auth|sdk|new|dev|ds|devices|install|uninstall|launch|help)
        # Offer subcommand argument completions
        "_xtool_${subcommand}"
        ;;
    *)
        # Offer subcommand completions
        COMPREPLY+=($(compgen -W 'setup auth sdk new dev ds devices install uninstall launch help' -- "${cur}"))
        ;;
    esac
}

_xtool_setup() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_auth() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0

    # Offer subcommand / subcommand argument completions
    local -r subcommand="${unparsed_words[0]}"
    unset 'unparsed_words[0]'
    unparsed_words=("${unparsed_words[@]}")
    case "${subcommand}" in
    login|logout|status)
        # Offer subcommand argument completions
        "_xtool_auth_${subcommand}"
        ;;
    *)
        # Offer subcommand completions
        COMPREPLY+=($(compgen -W 'login logout status' -- "${cur}"))
        ;;
    esac
}

_xtool_auth_login() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=(-u --username -p --password -m --mode)
    __xtool_offer_flags_options 0

    # Offer option value completions
    case "${prev}" in
    '-u'|'--username')
        return
        ;;
    '-p'|'--password')
        return
        ;;
    '-m'|'--mode')
        __xtool_add_completions -W 'key'$'\n''password'
        return
        ;;
    esac
}

_xtool_auth_logout() {
    repeating_flags=()
    non_repeating_flags=(-r --reset-2fa --version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_auth_status() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_sdk() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0

    # Offer subcommand / subcommand argument completions
    local -r subcommand="${unparsed_words[0]}"
    unset 'unparsed_words[0]'
    unparsed_words=("${unparsed_words[@]}")
    case "${subcommand}" in
    install|update|remove|build|status)
        # Offer subcommand argument completions
        "_xtool_sdk_${subcommand}"
        ;;
    *)
        # Offer subcommand completions
        COMPREPLY+=($(compgen -W 'install update remove build status' -- "${cur}"))
        ;;
    esac
}

_xtool_sdk_install() {
    repeating_flags=()
    non_repeating_flags=(--slim --version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 1

    # Offer positional completions
    case "${positional_number}" in
    1)
        __xtool_add_completions -o plusdirs -fX '!*.@(xip|app|xtoolsdk)'
        return
        ;;
    esac
}

_xtool_sdk_update() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_sdk_remove() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_sdk_build() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=(--arch)
    __xtool_offer_flags_options 2

    # Offer option value completions
    case "${prev}" in
    '--arch')
        return
        ;;
    esac

    # Offer positional completions
    case "${positional_number}" in
    1)
        __xtool_add_completions -o plusdirs -fX '!*.@(xip|app)'
        return
        ;;
    esac
}

_xtool_sdk_status() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_new() {
    repeating_flags=()
    non_repeating_flags=(--skip-setup --version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 1
}

_xtool_dev() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0

    # Offer subcommand / subcommand argument completions
    local -r subcommand="${unparsed_words[0]}"
    unset 'unparsed_words[0]'
    unparsed_words=("${unparsed_words[@]}")
    case "${subcommand}" in
    generate-xcode-project|build|run|build-server)
        # Offer subcommand argument completions
        "_xtool_dev_${subcommand}"
        ;;
    *)
        # Offer subcommand completions
        COMPREPLY+=($(compgen -W 'generate-xcode-project build run build-server' -- "${cur}"))
        ;;
    esac
}

_xtool_dev_generate-xcode-project() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_dev_build() {
    repeating_flags=()
    non_repeating_flags=(--sign -s --ipa -i --version -h --help)
    repeating_options=()
    non_repeating_options=(--configuration -c --triple)
    __xtool_offer_flags_options 0

    # Offer option value completions
    case "${prev}" in
    '--configuration'|'-c')
        __xtool_add_completions -W 'debug'$'\n''release'
        return
        ;;
    '--triple')
        return
        ;;
    esac
}

_xtool_dev_run() {
    repeating_flags=()
    non_repeating_flags=(--simulator -s --usb --network --all --version -h --help)
    repeating_options=()
    non_repeating_options=(--configuration -c --udid -u)
    __xtool_offer_flags_options 0

    # Offer option value completions
    case "${prev}" in
    '--configuration'|'-c')
        __xtool_add_completions -W 'debug'$'\n''release'
        return
        ;;
    '--udid'|'-u')
        return
        ;;
    esac
}

_xtool_dev_build-server() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=(--triple)
    __xtool_offer_flags_options 0

    # Offer option value completions
    case "${prev}" in
    '--triple')
        return
        ;;
    esac
}

_xtool_ds() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0

    # Offer subcommand / subcommand argument completions
    local -r subcommand="${unparsed_words[0]}"
    unset 'unparsed_words[0]'
    unparsed_words=("${unparsed_words[@]}")
    case "${subcommand}" in
    teams|certificates|identifiers|devices|profiles|anisette)
        # Offer subcommand argument completions
        "_xtool_ds_${subcommand}"
        ;;
    *)
        # Offer subcommand completions
        COMPREPLY+=($(compgen -W 'teams certificates identifiers devices profiles anisette' -- "${cur}"))
        ;;
    esac
}

_xtool_ds_teams() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0

    # Offer subcommand / subcommand argument completions
    local -r subcommand="${unparsed_words[0]}"
    unset 'unparsed_words[0]'
    unparsed_words=("${unparsed_words[@]}")
    case "${subcommand}" in
    list)
        # Offer subcommand argument completions
        "_xtool_ds_teams_${subcommand}"
        ;;
    *)
        # Offer subcommand completions
        COMPREPLY+=($(compgen -W 'list' -- "${cur}"))
        ;;
    esac
}

_xtool_ds_teams_list() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_ds_certificates() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0

    # Offer subcommand / subcommand argument completions
    local -r subcommand="${unparsed_words[0]}"
    unset 'unparsed_words[0]'
    unparsed_words=("${unparsed_words[@]}")
    case "${subcommand}" in
    list)
        # Offer subcommand argument completions
        "_xtool_ds_certificates_${subcommand}"
        ;;
    *)
        # Offer subcommand completions
        COMPREPLY+=($(compgen -W 'list' -- "${cur}"))
        ;;
    esac
}

_xtool_ds_certificates_list() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_ds_identifiers() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0

    # Offer subcommand / subcommand argument completions
    local -r subcommand="${unparsed_words[0]}"
    unset 'unparsed_words[0]'
    unparsed_words=("${unparsed_words[@]}")
    case "${subcommand}" in
    list)
        # Offer subcommand argument completions
        "_xtool_ds_identifiers_${subcommand}"
        ;;
    *)
        # Offer subcommand completions
        COMPREPLY+=($(compgen -W 'list' -- "${cur}"))
        ;;
    esac
}

_xtool_ds_identifiers_list() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_ds_devices() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0

    # Offer subcommand / subcommand argument completions
    local -r subcommand="${unparsed_words[0]}"
    unset 'unparsed_words[0]'
    unparsed_words=("${unparsed_words[@]}")
    case "${subcommand}" in
    list)
        # Offer subcommand argument completions
        "_xtool_ds_devices_${subcommand}"
        ;;
    *)
        # Offer subcommand completions
        COMPREPLY+=($(compgen -W 'list' -- "${cur}"))
        ;;
    esac
}

_xtool_ds_devices_list() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_ds_profiles() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0

    # Offer subcommand / subcommand argument completions
    local -r subcommand="${unparsed_words[0]}"
    unset 'unparsed_words[0]'
    unparsed_words=("${unparsed_words[@]}")
    case "${subcommand}" in
    list)
        # Offer subcommand argument completions
        "_xtool_ds_profiles_${subcommand}"
        ;;
    *)
        # Offer subcommand completions
        COMPREPLY+=($(compgen -W 'list' -- "${cur}"))
        ;;
    esac
}

_xtool_ds_profiles_list() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_ds_anisette() {
    repeating_flags=()
    non_repeating_flags=(--version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_devices() {
    repeating_flags=()
    non_repeating_flags=(--usb --network --all --wait --no-wait --version -h --help)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options 0
}

_xtool_install() {
    repeating_flags=()
    non_repeating_flags=(--usb --network --all --version -h --help)
    repeating_options=()
    non_repeating_options=(--udid -u)
    __xtool_offer_flags_options 1

    # Offer option value completions
    case "${prev}" in
    '--udid'|'-u')
        return
        ;;
    esac
}

_xtool_uninstall() {
    repeating_flags=()
    non_repeating_flags=(--usb --network --all --version -h --help)
    repeating_options=()
    non_repeating_options=(--udid -u)
    __xtool_offer_flags_options 1

    # Offer option value completions
    case "${prev}" in
    '--udid'|'-u')
        return
        ;;
    esac
}

_xtool_launch() {
    repeating_flags=()
    non_repeating_flags=(--usb --network --all --version -h --help)
    repeating_options=()
    non_repeating_options=(--udid -u)
    __xtool_offer_flags_options -1

    # Offer option value completions
    case "${prev}" in
    '--udid'|'-u')
        return
        ;;
    esac
}

_xtool_help() {
    repeating_flags=()
    non_repeating_flags=(--version)
    repeating_options=()
    non_repeating_options=()
    __xtool_offer_flags_options -1
}

complete -o filenames -F _xtool xtool
