(function () {
    if (typeof CSInterface === "undefined") return;

    var cs = new CSInterface();
    var btn = null;

    function tryAttach() {
        var toolbar = document.querySelector(
            'ul.global[data-tools="default"]'
        );
        btn = document.getElementById("supportButtonPortal");

        if (toolbar && btn) {
            btn.classList.remove("support-hidden");
            return true;
        }
        return false;
    }

    // tenta imediatamente
    if (!tryAttach()) {
        // tenta até a UI carregar
        var i = 0;
        var timer = setInterval(function () {
            if (tryAttach() || i++ > 20) {
                clearInterval(timer);
            }
        }, 300);
    }

    // clique
    document.addEventListener("click", function (e) {
        var el = e.target;
        while (el && !el.dataset?.action) {
            el = el.parentElement;
        }
        if (!el) return;

        if (el.dataset.action === "open_support") {
            cs.openURLInDefaultBrowser(
                "https://pacotesfx.com/ajuda-e-suporte/"
            );
        }
    });
})();
