"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _canUseDom = _interopRequireDefault(require("can-use-dom"));

var _simplebar = _interopRequireDefault(require("./simplebar"));

var _helpers = require("./helpers");

var _getLocale = require("../../../utils/getLocale");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

_simplebar["default"].initDOMLoadedElements = function () {
  document.removeEventListener("DOMContentLoaded", this.initDOMLoadedElements);
  window.removeEventListener("load", this.initDOMLoadedElements);
  Array.prototype.forEach.call(document.querySelectorAll("[data-simplebar]"), function (el) {
    if (el.getAttribute("data-simplebar") !== "init" && !_simplebar["default"].instances.has(el)) new _simplebar["default"](el, (0, _helpers.getOptions)(el.attributes));
  });
};

_simplebar["default"].removeObserver = function () {
  this.globalObserver.disconnect();
};

_simplebar["default"].initHtmlApi = function () {
  this.initDOMLoadedElements = this.initDOMLoadedElements.bind(this); // MutationObserver is IE11+

  if (typeof MutationObserver !== "undefined") {
    // Mutation observer to observe dynamically added elements
    this.globalObserver = new MutationObserver(_simplebar["default"].handleMutations);
    this.globalObserver.observe(document, {
      childList: true,
      subtree: true
    });
  } // Taken from jQuery `ready` function
  // Instantiate elements already present on the page


  if (document.readyState === "complete" || document.readyState !== "loading" && !document.documentElement.doScroll) {
    // Handle it asynchronously to allow scripts the opportunity to delay init
    window.setTimeout(this.initDOMLoadedElements);
  } else {
    document.addEventListener("DOMContentLoaded", this.initDOMLoadedElements);
    window.addEventListener("load", this.initDOMLoadedElements);
  }
};

_simplebar["default"].handleMutations = function (mutations) {
  mutations.forEach(function (mutation) {
    Array.prototype.forEach.call(mutation.addedNodes, function (addedNode) {
      if (addedNode.nodeType === 1) {
        if (addedNode.hasAttribute("data-simplebar")) {
          !_simplebar["default"].instances.has(addedNode) && document.documentElement.contains(addedNode) && new _simplebar["default"](addedNode, (0, _helpers.getOptions)(addedNode.attributes));
        } else {
          Array.prototype.forEach.call(addedNode.querySelectorAll("[data-simplebar]"), function (el) {
            if (el.getAttribute("data-simplebar") !== "init" && !_simplebar["default"].instances.has(el) && document.documentElement.contains(el)) new _simplebar["default"](el, (0, _helpers.getOptions)(el.attributes));
          });
        }
      }
    });
    Array.prototype.forEach.call(mutation.removedNodes, function (removedNode) {
      if (removedNode.nodeType === 1) {
        if (removedNode.getAttribute("data-simplebar") === "init") {
          _simplebar["default"].instances.has(removedNode) && !document.documentElement.contains(removedNode) && _simplebar["default"].instances.get(removedNode).unMount();
        } else {
          Array.prototype.forEach.call(removedNode.querySelectorAll("[data-simplebar=\"init\"]"), function (el) {
            _simplebar["default"].instances.has(el) && !document.documentElement.contains(el) && _simplebar["default"].instances.get(el).unMount();
          });
        }
      }
    });
  });
};

_simplebar["default"].getOptions = _helpers.getOptions;
/**
 * HTML API
 * Called only in a browser env.
 */

if (_canUseDom["default"]) {
  _simplebar["default"].initHtmlApi();
}

var _default = _simplebar["default"];
exports["default"] = _default;