/*! Built with http://stenciljs.com */
import { h } from './webcomponents.core.js';
/*
  You can use this component to lazy load below the fold images to improve load time.
  Below the fold images are images that are not seen by the user until they have started to scroll.
  Instead of loading all these images up front before the user even sees them, which will eat up
  network bandwith, we can use this component to put off loading these images until the user has
  scrolled to where that image is in your PWA.
  To use this component use the following markup: <ab-lazy-img src={/path/to/img} alt={alt text for img}></ab-lazy-img>
*/
var LazyImg = /** @class */ (function () {
    function LazyImg() {
    }
    LazyImg.prototype.componentDidLoad = function () {
        this.addIntersectionObserver();
    };
    LazyImg.prototype.componentWillUpdate = function () {
        if (this.src !== this.oldSrc) {
            this.addIntersectionObserver();
        }
        this.oldSrc = this.src;
    };
    LazyImg.prototype.handleImage = function () {
        var image = this.el.shadowRoot.querySelector('img');
        image.setAttribute('src', image.getAttribute('data-src'));
        image.onload = function () {
            image.removeAttribute('data-src');
        };
    };
    LazyImg.prototype.addIntersectionObserver = function () {
        var _this = this;
        if (!this.src) {
            return;
        }
        if ('IntersectionObserver' in window) {
            this.io = new IntersectionObserver(function (data) {
                // because there will only ever be one instance
                // of the element we are observing
                // we can just use data[0]
                if (data[0].isIntersecting) {
                    _this.handleImage();
                    _this.removeIntersectionObserver();
                }
            });
            this.io.observe(this.el.shadowRoot.querySelector('img'));
        }
        else {
            // fall back to just loading the image for Safari and IE
            this.handleImage();
        }
    };
    LazyImg.prototype.removeIntersectionObserver = function () {
        if (this.io) {
            this.io.disconnect();
            this.io = null;
        }
    };
    LazyImg.prototype.render = function () {
        return (h("img", { "data-src": this.src, alt: this.alt }));
    };
    Object.defineProperty(LazyImg, "is", {
        get: function () { return "ema-lazy-img"; },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LazyImg, "encapsulation", {
        get: function () { return "shadow"; },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LazyImg, "properties", {
        get: function () {
            return {
                "alt": {
                    "type": String,
                    "attr": "alt"
                },
                "el": {
                    "elementRef": true
                },
                "oldSrc": {
                    "state": true
                },
                "src": {
                    "type": String,
                    "attr": "src"
                }
            };
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LazyImg, "style", {
        get: function () { return "/*\\n * \\\@emas/js-base default theme\\n */\\n:host {\\n  display: block; }\\n\\nimg {\\n  max-width: 100%; }"; },
        enumerable: true,
        configurable: true
    });
    return LazyImg;
}());
export { LazyImg as EmaLazyImg };
