(function () {

    "use strict";


    /*
     * This JavaScript is ONLY for the solid-red footer on
     * authenticated/non-login WINGS pages.
     *
     * login.php already has its own footer HTML:
     *     #dswdGlobalFooter
     *
     * Therefore this file must NOT create, remove, move,
     * resize, or reposition the login footer.
     */


    var FOOTER_ID =
        "dswd-global-footer";


    /* =====================================================
       LOGIN CHECK
       ===================================================== */

    function isLoginPage() {

        var body =
            document.body;


        var pathname =
            window.location.pathname
                .toLowerCase();


        return (
            (
                body &&
                body.classList.contains(
                    "function-login"
                )
            ) ||

            (
                body &&
                body.getAttribute(
                    "data-pageid"
                ) ===
                "login"
            ) ||

            pathname.indexOf(
                "login.php"
            ) !==
                -1
        );
    }


    /* =====================================================
       GET PHPRUNNER SIDEBAR
       ===================================================== */

    function getSidebar() {

        return (
            document.querySelector(
                ".r-vbar-page .r-left"
            ) ||

            document.querySelector(
                ".r-left"
            )
        );
    }


    /* =====================================================
       SIDEBAR RIGHT EDGE
       ===================================================== */

    function getSidebarRight() {

        if (
            window.innerWidth <= 767
        ) {

            return 0;
        }


        var sidebar =
            getSidebar();


        if (!sidebar) {

            return 0;
        }


        var css =
            window.getComputedStyle(
                sidebar
            );


        if (
            css.display === "none" ||
            css.visibility === "hidden"
        ) {

            return 0;
        }


        var rect =
            sidebar.getBoundingClientRect();


        if (
            !rect ||
            rect.width <= 0
        ) {

            return 0;
        }


        return Math.max(
            0,
            Math.round(
                rect.right
            )
        );
    }


    /* =====================================================
       REMOVE EXISTING NORMAL APP FOOTER ONLY

       IMPORTANT:
       This looks only for #dswd-global-footer.
       It does NOT touch #dswdGlobalFooter used by login.php.
       ===================================================== */

    function removeOldFooter() {

        var old =
            document.getElementById(
                FOOTER_ID
            );


        if (old) {

            old.remove();
        }
    }


    /* =====================================================
       CREATE NORMAL APP FOOTER
       ===================================================== */

    function createFooter() {

        /*
         * NEVER touch login.php.
         */
        if (
            isLoginPage()
        ) {

            return;
        }


        /*
         * Do not create inside iframe/modal contexts.
         */
        if (
            window.self !==
            window.top
        ) {

            return;
        }


        removeOldFooter();


        var footer =
            document.createElement(
                "footer"
            );


        footer.id =
            FOOTER_ID;


        footer.className =
            "dswd-global-footer";


        footer.innerHTML = `

            <div class="dswd-footer-content">

                <div class="dswd-footer-left">

                    <div class="dswd-footer-hashtag">
                        #BawatBuhayMahalagaSaDSWD
                    </div>

                </div>


                <div class="dswd-footer-socials">


                    <a
                        href="https://www.dswd.gov.ph/"
                        target="_blank"
                        rel="noopener noreferrer"
                        class="dswd-footer-link">

                        <img
                            src="dswd-footer/website.png"
                            class="dswd-social-icon"
                            alt="DSWD website">

                        <span>
                            www.dswd.gov.ph
                        </span>

                    </a>


                    <a
                        href="#"
                        class="dswd-footer-link">

                        <img
                            src="dswd-footer/facebook.png"
                            class="dswd-social-icon"
                            alt="Facebook">

                        <span>
                            OplanPagAbot
                        </span>

                    </a>


                    <a
                        href="#"
                        class="dswd-footer-link">

                        <img
                            src="dswd-footer/x.png"
                            class="dswd-social-icon"
                            alt="X">

                        <span>
                            @oplanpagabot
                        </span>

                    </a>


                </div>

            </div>

        `;


        document.body.appendChild(
            footer
        );


        updateFooterPosition();
    }


    /* =====================================================
       UPDATE NORMAL APP FOOTER LEFT POSITION
       ===================================================== */

    function updateFooterPosition() {

        /*
         * Login footer position is controlled only by
         * login CSS. Never calculate it here.
         */
        if (
            isLoginPage()
        ) {

            return;
        }


        var footer =
            document.getElementById(
                FOOTER_ID
            );


        if (!footer) {

            return;
        }


        var left =
            getSidebarRight();


        document
            .documentElement
            .style
            .setProperty(
                "--dswd-footer-left",
                left + "px"
            );
    }


    /* =====================================================
       WATCH SIDEBAR EXPAND / COLLAPSE
       ===================================================== */

    function watchSidebar() {

        if (
            isLoginPage()
        ) {

            return;
        }


        var sidebar =
            getSidebar();


        if (!sidebar) {

            return;
        }


        /*
         * Actual width changes.
         */

        if (
            typeof ResizeObserver !==
            "undefined"
        ) {

            var resizeObserver =
                new ResizeObserver(
                    function () {

                        window.requestAnimationFrame(
                            updateFooterPosition
                        );
                    }
                );


            resizeObserver.observe(
                sidebar
            );
        }


        /*
         * PHPRunner can also change class/style.
         */

        var mutationObserver =
            new MutationObserver(
                function () {

                    window.requestAnimationFrame(
                        updateFooterPosition
                    );
                }
            );


        mutationObserver.observe(
            sidebar,
            {
                attributes: true,

                attributeFilter: [
                    "class",
                    "style"
                ]
            }
        );
    }


    /* =====================================================
       INITIALIZE
       ===================================================== */

    function initialize() {

        /*
         * This is intentionally a complete no-op on login.php.
         *
         * No footer creation.
         * No footer deletion.
         * No footer movement.
         * No CSS variable changes.
         * No resize listener.
         */
        if (
            isLoginPage()
        ) {

            return;
        }


        createFooter();


        watchSidebar();


        updateFooterPosition();


        window.addEventListener(
            "resize",
            updateFooterPosition
        );


        /*
         * Allow PHPRunner layout to settle.
         */

        [
            100,
            300,
            700,
            1200
        ].forEach(
            function (delay) {

                window.setTimeout(
                    updateFooterPosition,
                    delay
                );
            }
        );
    }


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

        document.addEventListener(
            "DOMContentLoaded",
            initialize
        );

    } else {

        initialize();
    }

})();
