/* Initialize usercentrics window for client specific handling */
window.addEventListener("load", (e) => {
    const usercentricsRoot = document.getElementById("usercentrics-root") 
    if(usercentricsRoot) {
        initUsercentrisObserver(usercentricsRoot);
        applyConsentManagerStyling(usercentricsRoot);
    }
});

/* Apply changes and observer for "layers" within usercentrics window, e.g. click on further informations */
function initUsercentrisObserver(ucRoot) {
    /* Konfiguration des Observers: alles melden - Änderungen an Daten, Kindelementen und Attributen */
    const config = {
        attributes: true,
        childList: true,
        characterData: true,
        subtree: true,
    };

    /* eine Instanz des Observers erzeugen */
    var appContainerObserver = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
            if (mutation.addedNodes && mutation.addedNodes.length > 0) {
                for (const addedNode of mutation.addedNodes) {
                    try {
                        if (addedNode.nodeName.toLowerCase() !== "style") {
                            applyConsentManagerStyling(ucRoot);
                            return;
                        }
                    } catch (error) {
                        continue;
                    }
                }
            }
        });
    });

    let ucAppContainer = ucRoot.shadowRoot.querySelector(
        '[data-testid="uc-app-container"]'
    );
    /* eigentliche Observierung starten und Zielnode und Konfiguration übergeben */
    appContainerObserver.observe(ucAppContainer, config);
}

/* Process styling for usercentrics window */
function applyConsentManagerStyling(ucRoot) {
    let ucAppContainer = ucRoot.shadowRoot.querySelector(
        '[data-testid="uc-app-container"]'
    );

    if (!ucAppContainer.querySelector("style#oev-style")) {
        const catStyle = document.createElement("style");
        catStyle.id = "oev-style";
        catStyle.appendChild(
            document.createTextNode(`
                      #uc-tab-list > [aria-selected="true"] {
                          border-bottom-color: var(--oev-color-primary);
                          color: var(--oev-color-primary);
                      }
                      [data-testid="uc-accept-all-button"] {
                          background: var(--oev-color-primary) !important;
                          color: #ffffff !important;
                      }
                      * {
                          font-family: var(--oev-font-family) !important;
                      }
                      `)
        );
        ucAppContainer.appendChild(catStyle);
    }

    const ucHeader = ucAppContainer.querySelector('[data-testid="uc-header"]');

    if (ucHeader) {
        ucAppContainer
            .querySelector('[data-testid="uc-header"]')
            .querySelector("img").src = getCurrentLogo();
    }
}

function getCurrentLogo() {
    const oevHeader = document.querySelector("oev-header-v1");
    if (oevHeader) {
        const oevHeaderImageLink = oevHeader.querySelector("oev-image-link-v1");
        if (oevHeaderImageLink && oevHeaderImageLink.hasAttribute("image-src")) {
            return oevHeaderImageLink.getAttribute("image-src");
        } else if (oevHeader && oevHeader.hasAttribute("logo-src")) {
            return oevHeader.getAttribute("logo-src");
        }
    }

    return "";
}