#compdef peel

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

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

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

_peel() {
    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=(
            'apps:List installed apps.'
            'inventory:Write out what is installed and where each app came from.'
            'leftovers:Show the files an app leaves behind.'
            'uninstall:Move an app and its leftovers to the Trash.'
            'orphans:List files left by apps that are no longer installed, and move a group to the Trash.'
            'history:List what Peel moved to the Trash.'
            'restore:Put a removal back where it came from.'
            'exclusions:Show and change what Peel leaves alone.'
            'caches:List caches of developer tools, and move them to the Trash.'
            'projects:List what builds left behind in your projects, and move it to the Trash.'
            'duplicates:Find folders and files with identical contents.'
            'search:Search the Spotlight index for files.'
            'updates:Check installed apps for updates.'
            'help:Show subcommand help information.'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        apps|inventory|leftovers|uninstall|orphans|history|restore|exclusions|caches|projects|duplicates|search|updates|help)
            "_peel_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

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

    return "${ret}"
}

_peel_inventory() {
    local -i ret=1
    local -ar ___format=('text' 'json' 'csv' 'brewfile')
    local -ar arg_specs=(
        '--format[What to write. Without it, text.]:format:{__peel_complete "${___format[@]}"}'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_leftovers() {
    local -i ret=1
    local -ar arg_specs=(
        ':app:'
        '--all[Include files that need review before removing them.]'
        '--json[Print JSON instead of text.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_uninstall() {
    local -i ret=1
    local -ar arg_specs=(
        ':app:'
        '--keep-leftovers[Move only the app, not its leftovers.]'
        '--reset-privacy[Also reset the app'\''s privacy permissions, just before it moves, so it asks again if you reinstall it. The Trash can'\''t bring them back.]'
        '--dry-run[Show what would move without moving anything.]'
        '--keep-in-dock[Leave the app'\''s icon in the Dock. Without it, the icon comes out once the app is in the Trash, and goes back where it was if the app is put back.]'
        '(--yes -y)'{--yes,-y}'[Don'\''t ask for confirmation.]'
        '--json[Print JSON instead of text.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_orphans() {
    local -i ret=1
    local -ar arg_specs=(
        ':group:'
        '--remove[Move the named group to the Trash, leaving out what Peel holds back.]'
        '--dry-run[Show what would go without moving anything.]'
        '(--yes -y)'{--yes,-y}'[Don'\''t ask for confirmation.]'
        '--json[Print JSON instead of text.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_history() {
    local -i ret=1
    local -ar arg_specs=(
        '--limit[Show at most this many removals. Without it, 20.]:limit:'
        '--refused[List what stayed and why, instead of what moved.]'
        '--clear[Move the record of every refusal to the Trash. Only with --refused.]'
        '(--yes -y)'{--yes,-y}'[Don'\''t ask for confirmation. Only with --clear.]'
        '--json[Print JSON instead of text.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_restore() {
    local -i ret=1
    local -ar arg_specs=(
        ':id:'
        '--dry-run[Show what would go back without moving anything.]'
        '(--yes -y)'{--yes,-y}'[Don'\''t ask for confirmation.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_exclusions() {
    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:Show what Peel leaves alone.'
            'add:Leave a file, a folder, or an app alone.'
            'remove:Stop leaving something alone.'
        )
        _describe -V subcommand subcommands && ret=0
        ;;
    arg)
        case "${words[1]}" in
        list|add|remove)
            "_peel_exclusions_${words[1]}" && ret=0
            ;;
        esac
        ;;
    esac

    return "${ret}"
}

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

    return "${ret}"
}

_peel_exclusions_add() {
    local -i ret=1
    local -ar arg_specs=(
        '*:paths:_files'
        '--app[An app to leave alone\: its path, bundle identifier, or name. Write ./Name.app for a bundle in the current folder.]:app:'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_exclusions_remove() {
    local -i ret=1
    local -ar arg_specs=(
        '*:paths:_files'
        '--app[An app to stop leaving alone\: its path, bundle identifier, or name. Write ./Name.app for a bundle in the current folder.]:app:'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_caches() {
    local -i ret=1
    local -ar arg_specs=(
        '*:tools:'
        '--remove[Move what Peel suggests to the Trash.]'
        '--dry-run[Show what would go without moving anything.]'
        '(--yes -y)'{--yes,-y}'[Don'\''t ask for confirmation.]'
        '--json[Print JSON instead of text.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_projects() {
    local -i ret=1
    local -ar arg_specs=(
        '*:folders:_files -/'
        '--remove[Move what Peel suggests to the Trash.]'
        '--dry-run[Show what would go without moving anything.]'
        '(--yes -y)'{--yes,-y}'[Don'\''t ask for confirmation.]'
        '--json[Print JSON instead of text.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_duplicates() {
    local -i ret=1
    local -ar ___kind=('documents' 'images' 'movies' 'audio' 'archives' 'disk-images')
    local -ar arg_specs=(
        '*:folders:_files -/'
        '--kind[Only files of this kind.]:kind:{__peel_complete "${___kind[@]}"}'
        '--min-size[Only files at least this large, and folders holding at least this much, like 500KB or 1.5GB, or 0 for every size. Without it, 100KB, as in the Peel app.]:min-size:'
        '--remove[Move what Peel suggests to the Trash.]'
        '--dry-run[Show what would go without moving anything.]'
        '(--yes -y)'{--yes,-y}'[Don'\''t ask for confirmation.]'
        '--json[Print JSON instead of text.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_search() {
    local -i ret=1
    local -ar ___kind=('documents' 'images' 'movies' 'audio' 'archives' 'disk-images')
    local -ar arg_specs=(
        '--name[Part of the file name.]:name:'
        '--kind[The kind of file.]:kind:{__peel_complete "${___kind[@]}"}'
        '--min-size[Only files at least this large, like 500KB or 1.5GB.]:min-size:'
        '--older-than[Only files not modified in this many days.]:older-than:'
        '--everywhere[Search every disk on this Mac, not only your home folder. Network volumes aren'\''t searched.]'
        '--json[Print JSON instead of text.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_updates() {
    local -i ret=1
    local -ar arg_specs=(
        '--all[Show every app, not only those with an update.]'
        '--json[Print JSON instead of text.]'
        '--version[Show the version.]'
        '(-h --help)'{-h,--help}'[Show help information.]'
    )
    _arguments -w -s -S : "${arg_specs[@]}" && ret=0

    return "${ret}"
}

_peel_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]}" = _peel ]]; then
    _peel "${@}"
else
    compdef _peel peel
fi
