"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ExperienceCheckDomMutation = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _consts = require("./consts");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
 * Check for the completion of an experience based on DOM mutations
 *
 * Uses a MutationObserver to monitor DOM changes and invokes the provided
 * callback when relevant mutations are detected.
 *
 * Will result in success or failure based on the outcome of the callback.
 */
var ExperienceCheckDomMutation = exports.ExperienceCheckDomMutation = /*#__PURE__*/function () {
  function ExperienceCheckDomMutation(_ref) {
    var onDomMutation = _ref.onDomMutation,
      observeConfig = _ref.observeConfig;
    (0, _classCallCheck2.default)(this, ExperienceCheckDomMutation);
    this.onDomMutation = onDomMutation;
    this.observeConfig = observeConfig;
  }
  return (0, _createClass2.default)(ExperienceCheckDomMutation, [{
    key: "start",
    value: function start(callback) {
      var _this = this;
      this.stop();
      var configResult = this.observeConfig();
      var configs = Array.isArray(configResult) ? configResult : [configResult];
      var validConfigs = configs.filter(function (config) {
        return !!(config !== null && config !== void 0 && config.target);
      });
      if (validConfigs.length === 0) {
        callback({
          status: 'failure',
          reason: _consts.EXPERIENCE_FAILURE_REASON.DOM_MUTATION_TARGET_NOT_FOUND
        });
        return;
      }
      this.mutationObserver = new MutationObserver(function (mutations) {
        try {
          var result = _this.onDomMutation({
            mutations: mutations
          });
          if (result) {
            callback(result);
          }
        } catch (_unused) {
          callback({
            status: 'failure',
            reason: _consts.EXPERIENCE_FAILURE_REASON.DOM_MUTATION_CHECK_ERROR
          });
        }
      });
      var _iterator = _createForOfIteratorHelper(validConfigs),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var config = _step.value;
          var target = config.target,
            options = config.options;
          if (target) {
            this.mutationObserver.observe(target, options !== null && options !== void 0 ? options : {
              childList: true
            });
          }
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
    }
  }, {
    key: "stop",
    value: function stop() {
      if (this.mutationObserver) {
        this.mutationObserver.disconnect();
        this.mutationObserver = undefined;
      }
    }
  }]);
}();