#!/bin/bash
# shellcheck disable=2207

_lwr_update_completions() {
    local cur prev subcommands
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Define the subcommands
    subcommands="validate prepare perform clean status progress get full-update checkcompatible"

    # Handle the first argument (subcommand)
    if [[ ${COMP_CWORD} -eq 1 ]]; then
        COMPREPLY=( $(compgen -W "${subcommands}" -- "${cur}") )
        return 0
    fi

    # Handle the 'prepare' subcommand with optional 'force' argument
    if [[ "${prev}" == "prepare" ]]; then
        COMPREPLY=( $(compgen -W "force" -- "${cur}") )
        return 0
    fi

    # Handle the 'get' subcommand with file or directory completion
    if [[ "${prev}" == "get" || "${prev}" == "full-update" || "${prev}" == "checkcompatible" ]]; then
        compopt -o nospace  # Disable space after completion
        COMPREPLY=( $(compgen -f -d -- "${cur}") )
        for ((ff=0; ff<${#COMPREPLY[@]}; ff++)); do
            [[ -d ${COMPREPLY[$ff]} ]] && COMPREPLY[$ff]+='/'
            [[ -f ${COMPREPLY[$ff]} ]] && COMPREPLY[$ff]+=' '
        done
        return 0
    fi

    # Default to file completion if no match
    COMPREPLY=( $(compgen -f -- "${cur}") )
}

# Register the function for lwr-update command
complete -F _lwr_update_completions lwr-update