var ECL = (function (exports) {
  'use strict';

  var queryOne = function queryOne(selector, context) {
    if (context === void 0) {
      context = document;
    }
    return context.querySelector(selector);
  };

  /**
   * @param {HTMLElement} element DOM element for component instantiation and scope
   * @param {Object} options
   * @param {Boolean} options.attachClickListener Whether or not to bind click events
   * @param (String) options.targetSelector The selector of the element where to attach the click listener
   * @param (String) options.titleSelector  The selector of the element containing the link
   * @param (Integer) options.maxIterations Maximum number of ancestors to look for the element
   */
  var ContentBlock = /*#__PURE__*/function () {
    /**
     * @static
     * Shorthand for instance creation and initialisation.
     *
     * @param {HTMLElement} root DOM element for component instantiation and scope
     *
     * @return {ContentBlock} An instance of ContentBlock.
     */
    ContentBlock.autoInit = function autoInit(root, _temp) {
      var _ref = _temp === void 0 ? {} : _temp,
        _ref$CONTENT_BLOCK = _ref.CONTENT_BLOCK,
        defaultOptions = _ref$CONTENT_BLOCK === void 0 ? {} : _ref$CONTENT_BLOCK;
      var contentBlock = new ContentBlock(root, defaultOptions);
      contentBlock.init();
      root.ECLContentBlock = contentBlock;
      return contentBlock;
    };
    function ContentBlock(element, _temp2) {
      var _ref2 = _temp2 === void 0 ? {} : _temp2,
        _ref2$targetSelector = _ref2.targetSelector,
        targetSelector = _ref2$targetSelector === void 0 ? '[data-ecl-picture-link]' : _ref2$targetSelector,
        _ref2$titleSelector = _ref2.titleSelector,
        titleSelector = _ref2$titleSelector === void 0 ? '[data-ecl-title-link]' : _ref2$titleSelector,
        _ref2$attachClickList = _ref2.attachClickListener,
        attachClickListener = _ref2$attachClickList === void 0 ? true : _ref2$attachClickList,
        _ref2$maxIterations = _ref2.maxIterations,
        maxIterations = _ref2$maxIterations === void 0 ? 2 : _ref2$maxIterations,
        _ref2$withTitleAttr = _ref2.withTitleAttr,
        withTitleAttr = _ref2$withTitleAttr === void 0 ? false : _ref2$withTitleAttr;
      // Check element
      if (!element || element.nodeType !== Node.ELEMENT_NODE) {
        throw new TypeError('DOM element should be given to initialize this widget.');
      }
      this.element = element;

      // Options
      this.targetSelector = targetSelector;
      this.titleSelector = titleSelector;
      this.attachClickListener = attachClickListener;
      this.maxIterations = maxIterations;
      this.withTitleAttr = withTitleAttr;

      // Bind `this` for use in callbacks
      this.linkTo = this.linkTo.bind(this);
      this.findElementInCommonAncestor = this.findElementInCommonAncestor.bind(this);
    }

    /**
     * Initialise component.
     */
    var _proto = ContentBlock.prototype;
    _proto.init = function init() {
      if (!ECL) {
        throw new TypeError('Called init but ECL is not present');
      }
      ECL.components = ECL.components || new Map();
      this.picture = this.findElementInCommonAncestor(this.element, this.targetSelector, this.maxIterations);

      // Exit early if no picture is present.
      if (!this.picture) {
        return;
      }
      this.title = queryOne(this.titleSelector, this.element);
      this.linkEl = this.title.nodeName === 'A' ? this.title : queryOne('a', this.title);
      if (this.linkEl) {
        this.picture.style.cursor = 'pointer';
        var img = queryOne('img', this.picture);
        if (img && this.withTitleAttr) {
          img.title = this.constructor.convertToFullURL(this.linkEl.getAttribute('href'));
        }
        if (this.attachClickListener) {
          this.picture.addEventListener('click', this.linkTo);
        }
      }
      this.element.setAttribute('data-ecl-auto-initialized', true);
      ECL.components.set(this.element, this);
    }

    /**
     * Redirect the user to the desired url.
     */;
    _proto.linkTo = function linkTo() {
      if (this.linkEl) {
        // Click the linking element.
        this.linkEl.click();
      }
    }

    /**
     * Find an element in a common ancestor.
     *
     * @param {HTMLElement} element
     * @param {string} selector
     */;
    _proto.findElementInCommonAncestor = function findElementInCommonAncestor(element, selector, maxIterations) {
      var eureka = queryOne(selector, element);
      if (eureka) {
        return eureka;
      }
      if (element === document.documentElement || maxIterations <= 0) {
        return null;
      }
      return this.findElementInCommonAncestor(element.parentElement, selector, maxIterations - 1);
    }

    /**
     * Convert a path to a full url.
     *
     * @param {String} href
     */;
    ContentBlock.convertToFullURL = function convertToFullURL(href) {
      if (href.startsWith('http://') || href.startsWith('https://')) {
        return href;
      }
      var baseUrl = new URL(window.location.href);
      var fullUrl = new URL(href, baseUrl);
      return fullUrl.href;
    }

    /**
     * Destroy component.
     */;
    _proto.destroy = function destroy() {
      if (this.attachClickListener && this.picture) {
        this.picture.removeEventListener('click', this.linkto);
      }
      if (this.element) {
        this.element.removeAttribute('data-ecl-auto-initialized');
        ECL.components["delete"](this.element);
      }
    };
    return ContentBlock;
  }();

  exports.ContentBlock = ContentBlock;
  exports.default = ContentBlock;

  Object.defineProperty(exports, '__esModule', { value: true });

  return exports;

})({});
