# nordvpn_list_countries
# ----------------------
# Lista los paises disponibles en NordVPN como array JSON.
#
# USO (sourced):
#   source nordvpn_list_countries.sh
#   nordvpn_list_countries

nordvpn_list_countries() {
    if ! command -v nordvpn &>/dev/null; then
        echo '{"ok":false,"error":"nordvpn no instalado"}' >&2
        return 1
    fi

    local output
    output=$(nordvpn countries 2>&1)
    local rc=$?

    if [ $rc -ne 0 ]; then
        echo "{\"ok\":false,\"error\":$(echo "$output" | python3 -c 'import sys,json; print(json.dumps(sys.stdin.read().strip()))' 2>/dev/null || echo "\"$output\"")}"
        return 1
    fi

    echo "$output" | python3 -c '
import sys, json, re

text = sys.stdin.read()
text = re.sub(r"\x1b\[[0-9;]*m", "", text)
text = re.sub(r"[\t\r]", " ", text)
# Split by comma, whitespace, or newline and clean
countries = [c.strip().replace("_", " ") for c in re.split(r"[,\n]+", text) if c.strip() and c.strip() != "-"]
countries = [c for c in countries if len(c) > 1]
countries.sort()
print(json.dumps({"ok": True, "count": len(countries), "countries": countries}))
'
}
