"use strict";exports.__esModule = true;exports.createRotationAnimation = exports.createOffsetAnimation = exports.createLengthAnimation = exports.createColorAnimation = exports.SpinnerFallbackAnimationRunner = void 0;
var _globalObject = require("@skbkontur/global-object");

var _ColorFactory = require("../../lib/styles/ColorFactory"); /* eslint-disable no-useless-constructor */var

SpinnerFallbackAnimationRunner = exports.SpinnerFallbackAnimationRunner =


function SpinnerFallbackAnimationRunner(
animations,
FPS)
{var _this = this;this.interval = null;this.



  start = function () {
    _this.animations.forEach(function (animation) {return animation.start();});
    _this.interval = rafInterval(function () {
      _this.animations.forEach(function (animation) {return animation.step();});
    }, _this.FPS);
  };this.

  stop = function () {
    if (_this.interval) {
      _this.interval.clear();
      _this.animations.forEach(function (animation) {return animation.finish();});
    }
  };this.animations = animations;this.FPS = FPS;this.start();};


var rafInterval = function rafInterval(fn, delay) {
  var lastcall = 0;
  var cleared = false;
  var rafId = 0;

  var interval = function interval() {
    if (cleared) {
      return;
    }

    var timestamp = new Date().getTime();
    if (!lastcall) {
      lastcall = timestamp;
    }
    if (timestamp - lastcall > delay) {
      fn();
      lastcall = timestamp;
    }
    if (_globalObject.globalObject.requestAnimationFrame) {
      rafId = _globalObject.globalObject.requestAnimationFrame(interval);
    }
  };
  interval();

  return {
    clear: function clear() {
      cleared = true;
      _globalObject.globalObject.cancelAnimationFrame == null || _globalObject.globalObject.cancelAnimationFrame(rafId);
    }
  };
};var

Animation =



function Animation(
duration,
onProgress,
onFinish)
{var _this2 = this;this.startTime = 0;this.isFinished = false;this.

  step = function () {
    if (_this2.isFinished) {
      return;
    }

    var timestamp = new Date().getTime();
    if (!_this2.startTime) {
      _this2.startTime = timestamp;
    }
    var progress = (timestamp - _this2.startTime) / _this2.duration;

    _this2.onProgress(progress);

    if (progress >= 1) {
      _this2.finish();
    }
  };this.

  reset = function () {
    _this2.startTime = 0;
    _this2.isFinished = false;
  };this.

  finish = function () {
    _this2.isFinished = true;
    _this2.onFinish && _this2.onFinish(_this2);
  };this.

  start = function () {
    if (!_this2.isFinished) {
      _this2.step();
    }
  };this.duration = duration;this.onProgress = onProgress;this.onFinish = onFinish;};


var createOffsetAnimation = exports.createOffsetAnimation = function createOffsetAnimation(
from,
to,
duration,
setStyleProperty,
units)
{if (units === void 0) {units = '';}
  return new Animation(
    duration,
    function (progress) {
      var current = from + (to - from) * progress;
      setStyleProperty('stroke-dashoffset', "" + current + units);
    },
    function (animation) {
      animation.reset();
    }
  );
};

var createLengthAnimation = exports.createLengthAnimation = function createLengthAnimation(
from,
to,
duration,
setStyleProperty,
units)
{if (units === void 0) {units = '';}
  var reverse = false;
  return new Animation(
    duration,
    function (progress) {
      var p = reverse ? 1 - progress : progress;
      var current = [from[0] + (to[0] - from[0]) * p, from[1] + (to[1] - from[1]) * p];
      setStyleProperty('stroke-dasharray', "" + current[0] + units + " " + current[1] + units);
    },
    function (animation) {
      reverse = !reverse;
      animation.reset();
    }
  );
};

var createColorAnimation = exports.createColorAnimation = function createColorAnimation(
colors,
duration,
setStyleProperty)
{
  var rgbColors = colors.map(function (color) {return _ColorFactory.ColorFactory.create(color).rgb;});
  var currentIndex = 0;
  var nextIndex = 1;

  return new Animation(
    duration,
    function (progress) {
      var from = rgbColors[currentIndex];
      var to = rgbColors[nextIndex];
      if (from && to) {
        var current = [
        Math.round(from[0] + (to[0] - from[0]) * progress),
        Math.round(from[1] + (to[1] - from[1]) * progress),
        Math.round(from[2] + (to[2] - from[2]) * progress)];

        setStyleProperty('stroke', "rgb(" + current + ")");
      }
    },
    function (animation) {
      animation.reset();
      currentIndex = nextIndex;
      nextIndex = (nextIndex + 1) % colors.length;
    }
  );
};

var createRotationAnimation = exports.createRotationAnimation = function createRotationAnimation(
from,
to,
duration,
setStyleProperty,
units)
{if (units === void 0) {units = 'deg';}
  return new Animation(
    duration,
    function (progress) {
      var current = Math.round(from + (to - from) * progress);
      setStyleProperty('transform', "rotate(" + current + units + ")");
    },
    function (animation) {
      animation.reset();
    }
  );
};