document.addEventListener('DOMContentLoaded', function () {

    // Get the toggle value
    const toggleHideOnly = document.querySelector('input[name="saprfurl_hide_login_page"]');
    const toggleHideRedirect = document.querySelector('input[name="saprfurl_hide_redirect_page"]');

    // Interact with the upper toggle block
    const messageHideOnly = document.getElementById('saprfurl-hide-page-warning-message');
    const messageHideRedirect = document.getElementById('saprfurl-hide-change-page-warning-message');

    // Interact with the lower toggle block   
    const messagesHideOn = document.getElementById('saprfurl-hide-on-message');
    const messagesHideChangeOn = document.getElementById('saprfurl-hide-change-on-message');

    const messagesHideDefault = document.querySelectorAll('#saprfurl-off-message');

    // Helper function to hide all messages
    const hideAllMessages = () => {
        if (messageHideOnly) messageHideOnly.style.display = 'none';
        if (messageHideRedirect) messageHideRedirect.style.display = 'none';
        if (messagesHideOn) messagesHideOn.style.display = 'none';
        if (messagesHideChangeOn) messagesHideChangeOn.style.display = 'none';
        messagesHideDefault.forEach(el => el.style.display = 'block');
    };

    if (toggleHideOnly && toggleHideRedirect) {
        toggleHideOnly.addEventListener('change', function () {
            if (this.checked) {
                toggleHideRedirect.checked = false;
                if (messageHideOnly) messageHideOnly.style.display = 'block';
                if (messageHideRedirect) messageHideRedirect.style.display = 'none';
                if (messagesHideOn) messagesHideOn.style.display = 'block';
                if (messagesHideChangeOn) messagesHideChangeOn.style.display = 'none';
                messagesHideDefault.forEach(el => el.style.display = 'none');
            } else {
                hideAllMessages();
            }
        });

        toggleHideRedirect.addEventListener('change', function () {
            if (this.checked) {
                toggleHideOnly.checked = false;
                if (messageHideRedirect) messageHideRedirect.style.display = 'block';
                if (messageHideOnly) messageHideOnly.style.display = 'none';
                if (messagesHideOn) messagesHideOn.style.display = 'none';
                if (messagesHideChangeOn) messagesHideChangeOn.style.display = 'block';
                messagesHideDefault.forEach(el => el.style.display = 'none');
            } else {
                hideAllMessages();
            }
        });
    }
});

/** Updated internal link and external link JS */

document.addEventListener('DOMContentLoaded', function () {
    const internalToggle = document.getElementById('saprfurl_internal_link');
    const externalToggle = document.getElementById('saprfurl_external_link');

    const internalFollows = document.querySelectorAll('input[name="saprfurl_internal_follow"]');
    const externalFollows = document.querySelectorAll('input[name="saprfurl_external_follow"]');

    const internalFollowWraps = Array.from(internalFollows).map(cb => cb.closest('label.switch'));
    const externalFollowWraps = Array.from(externalFollows).map(cb => cb.closest('label.switch') || cb.closest('div'));

    function updateFollowOptions(toggle, followCheckboxes, wrapElements) {
        if (!toggle.checked) {
            followCheckboxes.forEach(cb => {
                cb.checked = false;
                cb.disabled = true;
            });
            wrapElements.forEach(el => {
                el.style.opacity = '0.4';
            });
        } else {
            followCheckboxes.forEach(cb => {
                cb.disabled = false;
            });
            wrapElements.forEach(el => {
                el.style.opacity = '1';
            });
        }
    }

    function enforceSingleCheckbox(group) {
        group.forEach(cb => {
            cb.addEventListener('change', function () {
                if (this.checked) {
                    group.forEach(other => {
                        if (other !== this) other.checked = false;
                    });
                }
            });
        });
    }

    // Initial state on load
    updateFollowOptions(internalToggle, internalFollows, internalFollowWraps);
    updateFollowOptions(externalToggle, externalFollows, externalFollowWraps);

    // On change
    internalToggle.addEventListener('change', () => {
        updateFollowOptions(internalToggle, internalFollows, internalFollowWraps);
    });

    externalToggle.addEventListener('change', () => {
        updateFollowOptions(externalToggle, externalFollows, externalFollowWraps);
    });

    // Only one can be selected at a time in each group
    enforceSingleCheckbox(internalFollows);
    enforceSingleCheckbox(externalFollows);
});
