/*
* scroll bar to the top of each page 7
* scroll to top 37
*/

/** JS script for adding scroll bar to the top of each page */
document.addEventListener('DOMContentLoaded', function () {
    const pageKey = 'sapmfly_hide_bar_<?php echo esc_js($page_identifier); ?>';
    if (document.cookie.indexOf(pageKey + '=true') !== -1) return;

    // get all the relevant class and assign here
    const container = document.getElementById('sapmfly-progress-bar-container');
    const progress = document.getElementById('sapmfly-progress-bar');

    if (container && progress) {
        container.style.display = 'block';

        function updateProgressBar() {
            const scroll = window.scrollY;
            const height = document.documentElement.scrollHeight - window.innerHeight;
            const scrolled = (scroll / height) * 100;
            progress.style.width = scrolled + '%';
        }

        window.addEventListener('scroll', updateProgressBar);
        updateProgressBar();
    }
});

// Code for scroll to top
document.addEventListener('DOMContentLoaded', function () {
    const btnBackToTop = document.getElementById('sapmfly-back-to-top');
    if (!btnBackToTop) return;

    window.addEventListener('scroll', function () {
        if (window.scrollY > 0) {
            btnBackToTop.classList.add('show');
        } else {
            btnBackToTop.classList.remove('show');
        }
    });

    btnBackToTop.addEventListener('click', function () {
        window.scrollTo({ top: 0, behavior: 'smooth' });
    });
});

// Code for front end dark mood
(function () {
    const toggleButton = document.getElementById("sapmfly-dark-mode-toggle");
    if (!toggleButton) return; // <-- IMPORTANT FIX

    const darkClass = "sapmfly-dark-mode";

    function getCookie(name) {
        const value = "; " + document.cookie;
        const parts = value.split("; " + name + "=");
        if (parts.length === 2) return parts.pop().split(";").shift();
    }

    function setCookie(name, value) {
        document.cookie = name + "=" + value + "; path=/";
    }

    function deleteCookie(name) {
        document.cookie = name + "=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
    }

    if (getCookie("sapmfly_dark_mode") === "yes") {
        document.body.classList.add(darkClass);
        toggleButton.textContent = "☀️ Light Mode";
    }

    toggleButton.addEventListener("click", function () {
        if (document.body.classList.contains(darkClass)) {
            document.body.classList.remove(darkClass);
            deleteCookie("sapmfly_dark_mode");
            toggleButton.textContent = "🌙 Dark Mode";
        } else {
            document.body.classList.add(darkClass);
            setCookie("sapmfly_dark_mode", "yes");
            toggleButton.textContent = "☀️ Light Mode";
        }
    });
})();


// Floating button cross related JS
document.addEventListener("DOMContentLoaded", function () {
    const closeBtn = document.getElementById("sapmfly-floating-close");
    const wrapper = document.getElementById("sapmfly-floating-wrapper");

    if (!closeBtn || !wrapper) {
        return;
    }

    // If already closed in this session
    if (sessionStorage.getItem("sapmflyFloatingClosed") === "true") {
        wrapper.style.display = "none";
    }

    // Close click event
    closeBtn.addEventListener("click", function () {
        wrapper.style.display = "none";
        sessionStorage.setItem("sapmflyFloatingClosed", "true");
    });
});

