// ==UserScript==
// @name         ScreenCloud AWS: region dropdown cleanup
// @namespace    http://tampermonkey.net/
// @version      0.1
// @author       wolf@screencloud.io
// @include      https://*.console.aws.amazon.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // fill in the regions you're actually interested in
    const regions = [
        { id: "us-east-1", primary: true },
        { id: "us-east-2", primary: true },
        { id: "us-west-2" },
        { id: "eu-west-1", primary: true },
        { id: "eu-central-1" },
    ];

    // work horse
    const fix = (menuNode) => {
        menuNode.childNodes.forEach((liNode) => {
            // hide dividers
            if (!liNode.childNodes[0]) {
                liNode.style.display = 'none';
                return;
            }
            const liRegion = liNode.childNodes[0].getAttribute("data-region-id");
            const region = regions.find((r) => r.id === liRegion);
            console.log({ liRegion, ...region });

            // remove?
            if (!region) {
                console.log(`hiding region: ${liRegion}`);
                liNode.style.display = 'none';
            } else {
                console.log(`keeping region: ${liRegion}`);
            }
        });
        console.log('all done');
    };

    // simple check function to see if html was rendered
    const interval = setInterval(() => {
        const menuNode = document.getElementById("menu--regions");

        if (!menuNode) {
            console.log("menuNode not found (yet...)");
            return;
        }

        console.log("menuNode found!");
        clearInterval(interval);
        fix(menuNode);
    }, 100);
})();