"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _gpuInterpolationTransition = _interopRequireDefault(require("../../transitions/gpu-interpolation-transition"));

var _gpuSpringTransition = _interopRequireDefault(require("../../transitions/gpu-spring-transition"));

var _log = _interopRequireDefault(require("../../utils/log"));

var TRANSITION_TYPES = {
  interpolation: _gpuInterpolationTransition.default,
  spring: _gpuSpringTransition.default
};

var AttributeTransitionManager = function () {
  function AttributeTransitionManager(device, _ref) {
    var id = _ref.id,
        timeline = _ref.timeline;
    (0, _classCallCheck2.default)(this, AttributeTransitionManager);
    this.id = void 0;
    this.isSupported = void 0;
    this.device = void 0;
    this.timeline = void 0;
    this.transitions = void 0;
    this.needsRedraw = void 0;
    this.numInstances = void 0;
    if (!device) throw new Error('AttributeTransitionManager is constructed without device');
    this.id = id;
    this.device = device;
    this.timeline = timeline;
    this.transitions = {};
    this.needsRedraw = false;
    this.numInstances = 1;
    this.isSupported = device.features.has('transform-feedback-webgl2');
  }

  var _proto = AttributeTransitionManager.prototype;

  _proto.finalize = function finalize() {
    for (var attributeName in this.transitions) {
      this._removeTransition(attributeName);
    }
  };

  _proto.update = function update(_ref2) {
    var attributes = _ref2.attributes,
        transitions = _ref2.transitions,
        numInstances = _ref2.numInstances;
    this.numInstances = numInstances || 1;

    for (var attributeName in attributes) {
      var attribute = attributes[attributeName];
      var settings = attribute.getTransitionSetting(transitions);
      if (!settings) continue;

      this._updateAttribute(attributeName, attribute, settings);
    }

    for (var _attributeName in this.transitions) {
      var _attribute = attributes[_attributeName];

      if (!_attribute || !_attribute.getTransitionSetting(transitions)) {
        this._removeTransition(_attributeName);
      }
    }
  };

  _proto.hasAttribute = function hasAttribute(attributeName) {
    var transition = this.transitions[attributeName];
    return transition && transition.inProgress;
  };

  _proto.getAttributes = function getAttributes() {
    var animatedAttributes = {};

    for (var attributeName in this.transitions) {
      var transition = this.transitions[attributeName];

      if (transition.inProgress) {
        animatedAttributes[attributeName] = transition.attributeInTransition;
      }
    }

    return animatedAttributes;
  };

  _proto.run = function run() {
    if (!this.isSupported || this.numInstances === 0) {
      return false;
    }

    for (var attributeName in this.transitions) {
      var updated = this.transitions[attributeName].update();

      if (updated) {
        this.needsRedraw = true;
      }
    }

    var needsRedraw = this.needsRedraw;
    this.needsRedraw = false;
    return needsRedraw;
  };

  _proto._removeTransition = function _removeTransition(attributeName) {
    this.transitions[attributeName].cancel();
    delete this.transitions[attributeName];
  };

  _proto._updateAttribute = function _updateAttribute(attributeName, attribute, settings) {
    var transition = this.transitions[attributeName];
    var isNew = !transition || transition.type !== settings.type;

    if (isNew) {
      if (!this.isSupported) {
        _log.default.warn("WebGL2 not supported by this browser. Transition for ".concat(attributeName, " is disabled."))();

        return;
      }

      if (transition) {
        this._removeTransition(attributeName);
      }

      var TransitionType = TRANSITION_TYPES[settings.type];

      if (TransitionType) {
        this.transitions[attributeName] = new TransitionType({
          attribute: attribute,
          timeline: this.timeline,
          device: this.device
        });
      } else {
        _log.default.error("unsupported transition type '".concat(settings.type, "'"))();

        isNew = false;
      }
    }

    if (isNew || attribute.needsRedraw()) {
      this.needsRedraw = true;
      this.transitions[attributeName].start(settings, this.numInstances);
    }
  };

  return AttributeTransitionManager;
}();

exports.default = AttributeTransitionManager;
//# sourceMappingURL=attribute-transition-manager.js.map