#!/bin/bash

sources_build_options_from_context() {
    local current_word="$1"
    local method="${COMP_WORDS[2]}"

    if [[ "${COMP_CWORD}" -eq 2 ]]; then
        printf '%s\n' list list-available show entries enable disable check-sign \
            list-removable-media probe-source add remove
        return
    fi

    if [[ "${COMP_CWORD}" -eq 3 && "$current_word" != -* ]]; then
        case "$method" in
            add|probe-source)
                alteratorctl sources list-removable-media --name-only 2>/dev/null || true
                return
                ;;
            show|enable|disable|check-sign|remove)
                LC_ALL=C alteratorctl sources list --name-only 2>/dev/null |
                    awk '/^[^[:space:]]/ && NF == 3 && ($3 == "active" || $3 == "inactive") {print $2}'
                return
                ;;
        esac
    fi

    case "$method" in
        list) printf '%s\n' --name-only --path-only --display-name-only --no-display-name ;;
        show) printf '%s\n' --full ;;
        list-removable-media) printf '%s\n' --name-only ;;
        enable) printf '%s\n' --mirror --component --multiarch --no-sign --dry-run --yes ;;
        disable) printf '%s\n' --mirror --component --multiarch --dry-run --yes ;;
        check-sign) printf '%s\n' --mirror --export-keys ;;
        remove) printf '%s\n' --yes ;;
    esac
    printf '%s\n' --help
}
