(function($) {
    'use strict';
    class MMPDBModal {
        constructor() {
            // Don't initialize if we're in a page builder EDITOR (not front-end)
            if (this.isInPageBuilderEditor()) {
                return;
            }
            this.init();
        }
        
        isInPageBuilderEditor() {
            // Check for Bricks builder editor
            if (window.Bricks && window.bricksIsFrontend) {
                return true;
            }
            
            // Check for Elementor editor
            if (window.elementorFrontend && window.elementorFrontend.isEditMode && window.elementorFrontend.isEditMode()) {
                return true;
            }
            
            // Check for WPBakery editor
            if (window.wpb && window.vc_iframe) {
                return true;
            }
            
            // Check for Divi builder editor
            if (window.et_pb_custom && window.et_pb_custom.is_builder_plugin_used === 'on') {
                return true;
            }
            
            // Check if we're in any visual builder mode by URL parameters
            const urlParams = new URLSearchParams(window.location.search);
            if (urlParams.get('et_fb') === '1' || 
                urlParams.get('elementor-preview') || 
                urlParams.get('bricks') === 'run' ||
                urlParams.get('vc_editable') === 'true') {
                return true;
            }
            
            // Check for common builder body classes in editor mode
            const body = document.body;
            if (body.classList.contains('et-db') || 
                body.classList.contains('elementor-editor-active') ||
                body.classList.contains('bricks-builder') ||
                body.classList.contains('vc_editor')) {
                return true;
            }
            
            return false;
        }
        init() {
            // Only create modal if we have modal links on the page
            if ($('.modal-link').length === 0) {
                return;
            }
            this.createModalHTML();
            this.bindEvents();
        }
        createModalHTML() {
            // Check if modal already exists
            if ($('.mmpdb-modal').length > 0) {
                return;
            }
            const modalHTML = `
                <div class="mmpdb-modal">
                    <div class="mmpdb-modal__content">
                        <button class="mmpdb-modal__close">&times;</button>
                        <iframe class="mmpdb-modal__iframe" src="" frameborder="0"></iframe>
                    </div>
                </div>
            `;
            
            $('body').append(modalHTML);
        }
        bindEvents() {
            $(document).on('click', '.modal-link', (e) => {
                e.preventDefault();
                this.openModal($(e.currentTarget).attr('href'));
            });
            $('.mmpdb-modal__close').on('click', () => this.closeModal());
            $(document).on('keyup', (e) => {
                if (e.key === 'Escape') this.closeModal();
            });
            // Close on overlay click
            $('.mmpdb-modal').on('click', (e) => {
                if ($(e.target).hasClass('mmpdb-modal')) {
                    this.closeModal();
                }
            });
        }
        openModal(url) {
            $('.mmpdb-modal__iframe').attr('src', url);
            $('.mmpdb-modal').addClass('mmpdb-modal--active');
            $('body').addClass('modal-is-open');
        }
        closeModal() {
            $('.mmpdb-modal').removeClass('mmpdb-modal--active');
            $('body').removeClass('modal-is-open');
            $('.mmpdb-modal__iframe').attr('src', '');
        }
    }
    // Initialize on document ready
    $(document).ready(() => {
        window.mmpdbModal = new MMPDBModal();
    });
})(jQuery); 