(function() {
    'use strict';
    console.log('🚀 fix-images.js - COLONNES UNIQUEMENT');
    
    function isPlaceholder(img) {
        return img.src && img.src.indexOf('data:image') === 0;
    }
    
    function isInColumns(img) {
        // Vérifier si l'image est dans un bloc colonnes
        let element = img;
        while (element && element !== document.body) {
            const className = element.className || '';
            
            // Si on trouve wp-block-columns, c'est bon
            if (className.indexOf('wp-block-columns') !== -1) {
                return true;
            }
            
            // Si on trouve wp-block-post-content ou article, on arrête
            if (className.indexOf('wp-block-post-content') !== -1 || 
                element.tagName === 'ARTICLE') {
                return false;
            }
            
            element = element.parentElement;
        }
        return false;
    }
    
    function getAlignment(img) {
        let element = img;
        while (element && element.tagName !== 'ARTICLE') {
            const className = element.className || '';
            
            if (className.indexOf('aligncenter') !== -1 || 
                className.indexOf('align-center') !== -1) {
                return 'center';
            }
            if (className.indexOf('alignright') !== -1 || 
                className.indexOf('align-right') !== -1) {
                return 'right';
            }
            if (className.indexOf('alignleft') !== -1 || 
                className.indexOf('align-left') !== -1) {
                return 'left';
            }
            
            element = element.parentElement;
        }
        
        return 'left';
    }
    
    function fixImageChain(img) {
        if (isPlaceholder(img) || !isInColumns(img)) {
            return false;
        }
        
        const alignment = getAlignment(img);
        
        // Force l'image
        img.style.setProperty('width', '100%', 'important');
        img.style.setProperty('height', 'auto', 'important');
        img.style.setProperty('max-width', '100%', 'important');
        img.style.setProperty('max-height', '90vh', 'important');
        img.style.setProperty('object-fit', 'contain', 'important');
        img.style.setProperty('margin', '0', 'important');
        
        if (alignment === 'center') {
            img.style.setProperty('object-position', 'center', 'important');
        } else if (alignment === 'right') {
            img.style.setProperty('object-position', 'right top', 'important');
        } else {
            img.style.setProperty('object-position', 'left top', 'important');
        }
        
        // Force le parent immédiat (A ou PICTURE)
        let parent = img.parentElement;
        if (parent && (parent.tagName === 'A' || parent.tagName === 'PICTURE')) {
            parent.style.setProperty('width', '100%', 'important');
            parent.style.setProperty('display', 'block', 'important');
            parent.style.setProperty('margin', '0', 'important');
            
            if (alignment === 'center') {
                parent.style.setProperty('text-align', 'center', 'important');
            } else if (alignment === 'right') {
                parent.style.setProperty('text-align', 'right', 'important');
            } else {
                parent.style.setProperty('text-align', 'left', 'important');
            }
            
            parent = parent.parentElement;
        }
        
        // Force le FIGURE
        if (parent && parent.tagName === 'FIGURE') {
            parent.style.setProperty('width', '100%', 'important');
            parent.style.setProperty('max-width', '100%', 'important');
            parent.style.setProperty('float', 'none', 'important');
            parent.style.setProperty('margin', '0', 'important');
            parent.style.setProperty('display', 'block', 'important');
            
            if (alignment === 'center') {
                parent.style.setProperty('text-align', 'center', 'important');
            } else if (alignment === 'right') {
                parent.style.setProperty('text-align', 'right', 'important');
            } else {
                parent.style.setProperty('text-align', 'left', 'important');
            }
        }
        
        return true;
    }
    
    function setupObserver() {
        const images = document.querySelectorAll('img');
        let fixed = 0;
        
        images.forEach(function(img) {
            if (fixImageChain(img)) {
                fixed++;
            }
            
            const observer = new MutationObserver(function(mutations) {
                mutations.forEach(function(mutation) {
                    if ((mutation.attributeName === 'src' || mutation.attributeName === 'style') && 
                        !isPlaceholder(img) && 
                        isInColumns(img)) {
                        fixImageChain(img);
                    }
                });
            });
            
            observer.observe(img, {
                attributes: true,
                attributeFilter: ['src', 'style']
            });
        });
        
        console.log('✅ Images dans colonnes corrigées:', fixed);
    }
    
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', setupObserver);
    } else {
        setupObserver();
    }
    
    setTimeout(setupObserver, 1000);
    setTimeout(setupObserver, 3000);
})();