(function () {
    "use strict";

    var PARTNER_ID = "wings-global-sidebar-partners";
    var PARTNER_HEIGHT = 86;

    function isLoginPage() {
        return (
            document.body &&
            document.body.classList.contains("function-login")
        );
    }

    function getSidebar() {
        return (
            document.querySelector(".r-vbar-page .r-left") ||
            document.querySelector(".r-left")
        );
    }

    function getMenu() {
        return (
            document.querySelector(".r-left [data-itemid='menu']") ||
            document.querySelector(".r-left .r-menu")
        );
    }

    function createPartners() {

        if (isLoginPage()) {
            return;
        }

        if (document.getElementById(PARTNER_ID)) {
            return;
        }

        var sidebar = getSidebar();

        if (!sidebar) {
            return;
        }

        var partners = document.createElement("div");

        partners.id = PARTNER_ID;

        partners.innerHTML = `
            <div class="wings-partners-label">
                In partnership with
            </div>

            <div class="wings-partners-row">

                <img
                    src="images/australian-aid.png"
                    alt="Australian Aid">

                <img
                    src="images/wfp.png"
                    alt="World Food Programme">

            </div>
        `;

        /*
         * Append to BODY, not to PHPRunner's menu cells.
         *
         * We will position it over the actual sidebar
         * using the sidebar's real rendered dimensions.
         */
        document.body.appendChild(partners);

        updatePartners();
    }


    function updatePartners() {

        var sidebar = getSidebar();

        var partners =
            document.getElementById(PARTNER_ID);

        var menu = getMenu();

        if (!sidebar || !partners) {
            return;
        }

        var rect =
            sidebar.getBoundingClientRect();

        /*
         * Mobile
         */
        if (window.innerWidth < 768) {

            partners.style.setProperty(
                "display",
                "none",
                "important"
            );

            if (menu) {
                menu.style.removeProperty(
                    "padding-bottom"
                );
            }

            return;
        }

        /*
         * PHPRunner collapsed / icon-only sidebar.
         */
        if (rect.width <= 95) {

            partners.style.setProperty(
                "display",
                "none",
                "important"
            );

            if (menu) {
                menu.style.setProperty(
                    "padding-bottom",
                    "12px",
                    "important"
                );
            }

            return;
        }

        /*
         * Expanded sidebar.
         */
        partners.style.setProperty(
            "display",
            "flex",
            "important"
        );

        partners.style.setProperty(
            "left",
            Math.round(rect.left) + "px",
            "important"
        );

        partners.style.setProperty(
            "width",
            Math.round(rect.width) + "px",
            "important"
        );

        /*
         * Make sure menu can scroll without its final
         * items being covered by the logos.
         */
        if (menu) {

            menu.style.setProperty(
                "padding-bottom",
                (PARTNER_HEIGHT + 14) + "px",
                "important"
            );
        }
    }


    function observeSidebar() {

        var sidebar = getSidebar();

        if (!sidebar) {
            return;
        }

        /*
         * PHPRunner changes actual sidebar width when
         * the hamburger/collapse button is clicked.
         */
        if (typeof ResizeObserver !== "undefined") {

            var ro = new ResizeObserver(function () {

                window.requestAnimationFrame(
                    updatePartners
                );

            });

            ro.observe(sidebar);
        }

        /*
         * Also catch PHPRunner class/style changes.
         */
        var mo = new MutationObserver(function () {

            window.requestAnimationFrame(
                updatePartners
            );

        });

        mo.observe(
            sidebar,
            {
                attributes: true,
                attributeFilter: [
                    "class",
                    "style"
                ]
            }
        );
    }


    function init() {

        if (isLoginPage()) {
            return;
        }

        if (window.self !== window.top) {
            return;
        }

        createPartners();

        observeSidebar();

        updatePartners();

        window.addEventListener(
            "resize",
            updatePartners
        );

        /*
         * PHPRunner may finish rendering in stages.
         */
        [100, 300, 700, 1200].forEach(
            function (delay) {

                setTimeout(
                    updatePartners,
                    delay
                );

            }
        );
    }


    if (document.readyState === "loading") {

        document.addEventListener(
            "DOMContentLoaded",
            init
        );

    } else {

        init();
    }

})();