"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var TransitionDetect = exports["default"] = /*#__PURE__*/function () {
  function TransitionDetect() {
    var _this = this;
    var target = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
    var timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
    _classCallCheck(this, TransitionDetect);
    _defineProperty(this, "transitionEnd", function () {
      _this.animating = false;
      clearTimeout(_this._timeout);
      _this._end && _this._end();
      if (_this._continuous) {
        _this.start(false);
      } else {
        _this.target.removeEventListener('transitionend', _this.transitionEnd);
      }
    });
    _defineProperty(this, "update", function () {
      _this._update && _this._update();
      if (_this.animating) {
        window.requestAnimationFrame(_this.update);
      }
    });
    this.target = target;
    this.animating = false;
    this.timeout = timeout;
  }
  _createClass(TransitionDetect, [{
    key: "dismiss",
    value: function dismiss() {
      this.animating = false;
      this.target && this.target.removeEventListener('transitionend', this.transitionEnd);
      clearTimeout(this._timeout);
      return this;
    }
  }, {
    key: "setTarget",
    value: function setTarget(target) {
      this.target = target;
      return this;
    }
  }, {
    key: "onUpdate",
    value: function onUpdate(callback) {
      this._update = callback;
      return this;
    }
  }, {
    key: "onEnd",
    value: function onEnd(callback) {
      this._end = callback;
      return this;
    }
  }, {
    key: "isContinuous",
    value: function isContinuous(is) {
      if (is === undefined) return this._continuous;
      this._continuous = is;
      return this;
    }
  }, {
    key: "start",
    value: function start() {
      var addTransitionEndListener = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
      // if already started just return
      if (this.animating) {
        return this;
      }
      if (!this.target) {
        throw 'Cannot set a transition event listener without a target!';
      }
      if (addTransitionEndListener) {
        this.target.addEventListener('transitionend', this.transitionEnd, false);
      }
      this.animating = true;
      // skip the update if there is no registered callback
      this._update && window.requestAnimationFrame(this.update);
      if (this.timeout) {
        this._timeout = setTimeout(this.transitionEnd, this.timeout);
      }
      return this;
    }
  }]);
  return TransitionDetect;
}();