// source --> https://elanillounico.com/wp-content/plugins/eau-imagenes/assets/frontend/lightbox.js?ver=1.2.1 
(function () {
    'use strict';

    var links = [];
    var currentIndex = -1;
    var overlay = null;
    var picture = null;
    var caption = null;
    var closeButton = null;
    var previousButton = null;
    var nextButton = null;
    var lightboxSelector = '[data-eau-lightbox="1"], .entry-content a.ngg-fancybox[href]';
    var imageUrlPattern = /\.(?:avif|webp|jpe?g|png|gif)(?:[?#].*)?$/i;

    function collectLinks() {
        links = Array.prototype.slice.call(document.querySelectorAll(lightboxSelector)).filter(isLightboxLink);
    }

    function isLightboxLink(link) {
        if (!link || link.getAttribute('data-eau-no-lightbox') === '1') {
            return false;
        }

        if (link.getAttribute('data-eau-lightbox') === '1') {
            return true;
        }

        return imageUrlPattern.test(getFullSrc(link));
    }

    function getLightboxLink(target) {
        var link = target.closest(lightboxSelector);
        return isLightboxLink(link) ? link : null;
    }

    function getFullSrc(link) {
        return link.getAttribute('data-full-src') || link.getAttribute('data-src') || link.href || '';
    }

    function getCaption(link) {
        var figure = link.closest('figure');
        var figcaption = figure ? figure.querySelector('figcaption') : null;

        return link.getAttribute('data-caption') ||
            link.getAttribute('data-title') ||
            (figcaption ? figcaption.textContent.trim() : '');
    }

    function getAlt(link) {
        var image = link.querySelector('img');

        return link.getAttribute('data-alt') ||
            (image ? image.getAttribute('alt') || '' : '') ||
            getCaption(link);
    }

    function createOverlay() {
        if (overlay) {
            return;
        }

        overlay = document.createElement('div');
        overlay.className = 'eau-lightbox';
        overlay.setAttribute('role', 'dialog');
        overlay.setAttribute('aria-modal', 'true');
        overlay.setAttribute('aria-hidden', 'true');

        closeButton = document.createElement('button');
        closeButton.type = 'button';
        closeButton.className = 'eau-lightbox__button eau-lightbox__close';
        closeButton.setAttribute('aria-label', 'Cerrar imagen');
        closeButton.textContent = 'x';

        previousButton = document.createElement('button');
        previousButton.type = 'button';
        previousButton.className = 'eau-lightbox__button eau-lightbox__previous';
        previousButton.setAttribute('aria-label', 'Imagen anterior');
        previousButton.textContent = '<';

        nextButton = document.createElement('button');
        nextButton.type = 'button';
        nextButton.className = 'eau-lightbox__button eau-lightbox__next';
        nextButton.setAttribute('aria-label', 'Imagen siguiente');
        nextButton.textContent = '>';

        picture = document.createElement('picture');
        picture.className = 'eau-lightbox__picture';

        caption = document.createElement('div');
        caption.className = 'eau-lightbox__caption';

        overlay.appendChild(closeButton);
        overlay.appendChild(previousButton);
        overlay.appendChild(nextButton);
        overlay.appendChild(picture);
        overlay.appendChild(caption);
        document.body.appendChild(overlay);

        closeButton.addEventListener('click', closeLightbox);
        previousButton.addEventListener('click', function () {
            showImage(currentIndex - 1);
        });
        nextButton.addEventListener('click', function () {
            showImage(currentIndex + 1);
        });
        overlay.addEventListener('click', function (event) {
            if (event.target === overlay) {
                closeLightbox();
            }
        });
    }

    function setPicture(link) {
        var full = getFullSrc(link);
        var avif = link.getAttribute('data-avif-src') || '';
        var webp = link.getAttribute('data-webp-src') || '';
        var alt = getAlt(link);
        var text = getCaption(link);
        var html = '';

        if (avif) {
            html += '<source type="image/avif" srcset="' + escapeAttribute(avif) + '">';
        }
        if (webp) {
            html += '<source type="image/webp" srcset="' + escapeAttribute(webp) + '">';
        }
        html += '<img src="' + escapeAttribute(full) + '" alt="' + escapeAttribute(alt) + '">';

        picture.innerHTML = html;
        caption.textContent = text;
        caption.hidden = !text;
    }

    function showImage(index) {
        if (!links.length) {
            return;
        }

        if (index < 0) {
            index = links.length - 1;
        } else if (index >= links.length) {
            index = 0;
        }

        currentIndex = index;
        setPicture(links[currentIndex]);
        previousButton.hidden = links.length < 2;
        nextButton.hidden = links.length < 2;
    }

    function openLightbox(index) {
        createOverlay();
        showImage(index);
        overlay.classList.add('is-open');
        overlay.setAttribute('aria-hidden', 'false');
        document.documentElement.classList.add('eau-lightbox-open');
        closeButton.focus();
    }

    function closeLightbox() {
        if (!overlay) {
            return;
        }

        overlay.classList.remove('is-open');
        overlay.setAttribute('aria-hidden', 'true');
        document.documentElement.classList.remove('eau-lightbox-open');
    }

    function escapeAttribute(value) {
        return String(value).replace(/[&<>"']/g, function (character) {
            return {
                '&': '&amp;',
                '<': '&lt;',
                '>': '&gt;',
                '"': '&quot;',
                "'": '&#039;'
            }[character];
        });
    }

    document.addEventListener('click', function (event) {
        var link = getLightboxLink(event.target);
        if (!link) {
            return;
        }

        collectLinks();
        var index = links.indexOf(link);
        if (index < 0) {
            return;
        }

        event.preventDefault();
        openLightbox(index);
    });

    document.addEventListener('keydown', function (event) {
        if (!overlay || !overlay.classList.contains('is-open')) {
            return;
        }

        if (event.key === 'Escape') {
            closeLightbox();
        } else if (event.key === 'ArrowLeft') {
            showImage(currentIndex - 1);
        } else if (event.key === 'ArrowRight') {
            showImage(currentIndex + 1);
        }
    });
}());