/*!
 * Custom elements for Nanopore-Digital Web applications
 */
'use strict';

var nanoSlides_entry = require('./nano-slides-Bsyo_UkD.js');
require('./index-DGttnXif.js');
require('./renderer-jVpDRMfV.js');

nanoSlides_entry.Flickity.createMethods.push('_createLazyload');
const proto = nanoSlides_entry.Flickity.prototype;
proto._createLazyload = function () {
    this.on('select', this.lazyLoad);
};
proto.lazyLoad = function () {
    const lazyLoad = this.options.lazyLoad;
    if (!lazyLoad) {
        return;
    }
    // get adjacent cells, use lazyLoad option for adjacent count
    const adjCount = typeof lazyLoad == 'number' ? lazyLoad : 0;
    const cellElems = this.getAdjacentCellElements(adjCount);
    // get lazy images in those cells
    let lazyImages = [];
    cellElems.forEach(function (cellElem) {
        const lazyCellImages = getCellLazyImages(cellElem);
        lazyImages = lazyImages.concat(lazyCellImages);
    });
    // load lazy images
    lazyImages.forEach(function (img) {
        new LazyLoader(img, this);
    }, this);
};
function getCellLazyImages(cellElem) {
    // check if cell element is lazy image
    if (cellElem.nodeName == 'IMG') {
        const lazyloadAttr = cellElem.getAttribute('data-flickity-lazyload');
        const srcAttr = cellElem.getAttribute('data-flickity-lazyload-src');
        const srcsetAttr = cellElem.getAttribute('data-flickity-lazyload-srcset');
        if (lazyloadAttr || srcAttr || srcsetAttr) {
            return [cellElem];
        }
    }
    // select lazy images in cell
    const lazySelector = 'img[data-flickity-lazyload], ' +
        'img[data-flickity-lazyload-src], img[data-flickity-lazyload-srcset]';
    const imgs = cellElem.querySelectorAll(lazySelector);
    return nanoSlides_entry.utils.makeArray(imgs);
}
// -------------------------- LazyLoader -------------------------- //
/**
 * class to handle loading images
 */
function LazyLoader(img, flickity) {
    this.img = img;
    this.flickity = flickity;
    this.load();
}
LazyLoader.prototype.handleEvent = nanoSlides_entry.utils.handleEvent;
LazyLoader.prototype.load = function () {
    this.img.addEventListener('load', this);
    this.img.addEventListener('error', this);
    // get src & srcset
    const src = this.img.getAttribute('data-flickity-lazyload') ||
        this.img.getAttribute('data-flickity-lazyload-src');
    const srcset = this.img.getAttribute('data-flickity-lazyload-srcset');
    // set src & serset
    this.img.src = src;
    if (srcset) {
        this.img.setAttribute('srcset', srcset);
    }
    // remove attr
    this.img.removeAttribute('data-flickity-lazyload');
    this.img.removeAttribute('data-flickity-lazyload-src');
    this.img.removeAttribute('data-flickity-lazyload-srcset');
};
LazyLoader.prototype.onload = function (event) {
    this.complete(event, 'flickity-lazyloaded');
};
LazyLoader.prototype.onerror = function (event) {
    this.complete(event, 'flickity-lazyerror');
};
LazyLoader.prototype.complete = function (event, className) {
    // unbind events
    this.img.removeEventListener('load', this);
    this.img.removeEventListener('error', this);
    const cell = this.flickity.getParentCell(this.img);
    const cellElem = cell && cell.element;
    this.flickity.cellSizeChange(cellElem);
    this.img.classList.add(className);
    this.flickity.dispatchEvent('lazyLoad', event, cellElem);
};
// @ts-ignore
nanoSlides_entry.Flickity.LazyLoader = LazyLoader;
