# keepass_list
# ------------
# Lista paths de entries del KeePassXC database como array JSON.
# Filtra opcionalmente por prefijo de grupo.
#
# REQUIERE:
#   - keepass_dump (sourced o en PATH del registry)
#   - jq instalado
#
# USO (sourced):
#   source keepass_dump.sh
#   source keepass_list.sh
#   all=$(keepass_list)
#   servers=$(keepass_list "Servers/")

keepass_list() {
    local prefix="$1"

    if ! declare -F keepass_dump >/dev/null 2>&1; then
        local here
        here=$(dirname "${BASH_SOURCE[0]}")
        if [ -f "$here/keepass_dump.sh" ]; then
            # shellcheck source=keepass_dump.sh
            source "$here/keepass_dump.sh"
        else
            echo "keepass_list: keepass_dump no disponible" >&2
            return 1
        fi
    fi

    local dump
    dump=$(keepass_dump) || return 1

    if [ -n "$prefix" ]; then
        printf '%s' "$dump" | jq --arg p "$prefix" '[.[] | .path | select(startswith($p))]'
    else
        printf '%s' "$dump" | jq '[.[] | .path]'
    fi
}
