text: 'use strict';
  
  Object.defineProperty(exports, '__esModule', { value: true });
  
  function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  
  var tslib_1 = require('tslib');
  var popcorn = require('@popmotion/popcorn');
  var sync = require('framesync');
  var sync__default = _interopDefault(sync);
  var styleValueTypes = require('style-value-types');
  var easing = require('@popmotion/easing');
  var heyListen = require('hey-listen');
  
  var Observer = (function () {
      function Observer(_a, observer) {
          var _this = this;
          var middleware = _a.middleware, onComplete = _a.onComplete;
          this.isActive = true;
          this.update = function (v) {
              if (_this.observer.update)
                  _this.updateObserver(v);
          };
          this.complete = function () {
              if (_this.observer.complete && _this.isActive)
                  _this.observer.complete();
              if (_this.onComplete)
                  _this.onComplete();
              _this.isActive = false;
          };
          this.error = function (err) {
              if (_this.observer.error && _this.isActive)
                  _this.observer.error(err);
              _this.isActive = false;
          };
          this.observer = observer;
          this.updateObserver = function (v) { return observer.update(v); };
          this.onComplete = onComplete;
          if (observer.update && middleware && middleware.length) {
              middleware.forEach(function (m) { return _this.updateObserver = m(_this.updateObserver, _this.complete); });
          }
      }
      return Observer;
  }());
  var createObserver = (function (observerCandidate, _a, onComplete) {
      var middleware = _a.middleware;
      if (typeof observerCandidate === 'function') {
          return new Observer({ middleware: middleware, onComplete: onComplete }, { update: observerCandidate });
      }
      else {
          return new Observer({ middleware: middleware, onComplete: onComplete }, observerCandidate);
      }
  });
  
  var Action = (function () {
      function Action(props) {
          if (props === void 0) { props = {}; }
          this.props = props;
      }
      Action.prototype.create = function (props) {
          return new Action(props);
      };
      Action.prototype.start = function (observerCandidate) {
          if (observerCandidate === void 0) { observerCandidate = {}; }
          var isComplete = false;
          var subscription = {
              stop: function () { return undefined; }
          };
          var _a = this.props, init = _a.init, observerProps = tslib_1.__rest(_a, ["init"]);
          var observer = createObserver(observerCandidate, observerProps, function () {
              isComplete = true;
              subscription.stop();
          });
          var api = init(observer);
          subscription = api ? tslib_1.__assign({}, subscription, api) : subscription;
          if (isComplete)
              subscription.stop();
          return subscription;
      };
      Action.prototype.applyMiddleware = function (middleware) {
          return this.create(tslib_1.__assign({}, this.props, { middleware: this.props.middleware
                  ? [middleware].concat(this.props.middleware) : [middleware] }));
      };
      Action.prototype.pipe = function () {
          var funcs = [];
          for (var _i = 0; _i < arguments.length; _i++) {
              funcs[_i] = arguments[_i];
          }
          var pipedUpdate = funcs.length === 1 ? funcs[0] : popcorn.pipe.apply(void 0, funcs);
          return this.applyMiddleware(function (update) { return function (v) { return update(pipedUpdate(v)); }; });
      };
      return Action;
  }());
  var action = (function (init) { return new Action({ init: init }); });
  
  var Chainable = (function () {
      function Chainable(props) {
          if (props === void 0) { props = {}; }
          this.props = props;
      }
      Chainable.prototype.applyMiddleware = function (middleware) {
          return this.create(tslib_1.__assign({}, this.props, { middleware: this.props.middleware
                  ? [middleware].concat(this.props.middleware) : [middleware] }));
      };
      Chainable.prototype.pipe = function () {
          var funcs = [];
          for (var _i = 0; _i < arguments.length; _i++) {
              funcs[_i] = arguments[_i];
          }
          var pipedUpdate = funcs.length === 1 ? funcs[0] : popcorn.pipe.apply(void 0, funcs);
          return this.applyMiddleware(function (update) { return function (v) { return update(pipedUpdate(v)); }; });
      };
      Chainable.prototype.while = function (predicate) {
          return this.applyMiddleware(function (update, complete) { return function (v) {
              return predicate(v) ? update(v) : complete();
          }; });
      };
      Chainable.prototype.filter = function (predicate) {
          return this.applyMiddleware(function (update) { return function (v) { return predicate(v) && update(v); }; });
      };
      return Chainable;
  }());
  
  var BaseMulticast = (function (_super) {
      tslib_1.__extends(BaseMulticast, _super);
      function BaseMulticast() {
          var _this = _super !== null && _super.apply(this, arguments) || this;
          _this.subscribers = [];
          return _this;
      }
      BaseMulticast.prototype.complete = function () {
          this.subscribers.forEach(function (subscriber) { return subscriber.complete(); });
      };
      BaseMulticast.prototype.error = function (err) {
          this.subscribers.forEach(function (subscriber) { return subscriber.error(err); });
      };
      BaseMulticast.prototype.update = function (v) {
          for (var i = 0; i < this.subscribers.length; i++) {
              this.subscribers[i].update(v);
          }
      };
      BaseMulticast.prototype.subscribe = function (observerCandidate) {
          var _this = this;
          var observer = createObserver(observerCandidate, this.props);
          this.subscribers.push(observer);
          var subscription = {
              unsubscribe: function () {
                  var index = _this.subscribers.indexOf(observer);
                  if (index !== -1)
                      _this.subscribers.splice(index, 1);
              }
          };
          return subscription;
      };
      BaseMulticast.prototype.stop = function () {
          if (this.parent)
              this.parent.stop();
      };
      return BaseMulticast;
  }(Chainable));
  
  var Multicast = (function (_super) {
      tslib_1.__extends(Multicast, _super);
      function Multicast() {
          return _super !== null && _super.apply(this, arguments) || this;
      }
      Multicast.prototype.create = function (props) {
          return new Multicast(props);
      };
      return Multicast;
  }(BaseMulticast));
  var multicast = (function () { return new Multicast(); });
  
  var ValueReaction = (function (_super) {
      tslib_1.__extends(ValueReaction, _super);
      function ValueReaction(props) {
          var _this = _super.call(this, props) || this;
          _this.scheduleVelocityCheck = function () { return sync__default.postRender(_this.velocityCheck); };
          _this.velocityCheck = function (_a) {
              var timestamp = _a.timestamp;
              if (timestamp !== _this.lastUpdated) {
                  _this.prev = _this.current;
              }
          };
          _this.prev = _this.current = props.value || 0;
          _this.updateCurrent = function (v) { return (_this.current = v); };
          _this.getVelocityOfCurrent = function () {
              return _this.getSingleVelocity(_this.current, _this.prev);
          };
          if (props.initialSubscription)
              _this.subscribe(props.initialSubscription);
          return _this;
      }
      ValueReaction.prototype.create = function (props) {
          return new ValueReaction(props);
      };
      ValueReaction.prototype.get = function () {
          return this.current;
      };
      ValueReaction.prototype.getVelocity = function () {
          return this.getVelocityOfCurrent();
      };
      ValueReaction.prototype.update = function (v) {
          _super.prototype.update.call(this, v);
          this.prev = this.current;
          this.updateCurrent(v);
          var _a = sync.getFrameData(), delta = _a.delta, timestamp = _a.timestamp;
          this.timeDelta = delta;
          this.lastUpdated = timestamp;
          sync__default.postRender(this.scheduleVelocityCheck);
      };
      ValueReaction.prototype.subscribe = function (observerCandidate) {
          var sub = _super.prototype.subscribe.call(this, observerCandidate);
          this.subscribers[this.subscribers.length - 1].update(this.current);
          return sub;
      };
      ValueReaction.prototype.getSingleVelocity = function (current, prev) {
          return typeof current === 'number' && typeof prev === 'number'
              ? popcorn.velocityPerSecond(current - prev, this.timeDelta)
              : popcorn.velocityPerSecond(parseFloat(current) - parseFloat(prev), this.timeDelta) || 0;
      };
      return ValueReaction;
  }(BaseMulticast));
  var value = (function (value, initialSubscription) {
      return new ValueReaction({ value: value, initialSubscription: initialSubscription });
  });
  
  var createVectorTests = function (typeTests) {
      var testNames = Object.keys(typeTests);
      var isVectorProp = function (prop, key) {
          return prop !== undefined && !typeTests[key](prop);
      };
      var getVectorKeys = function (props) {
          return testNames.reduce(function (vectorKeys, key) {
              if (isVectorProp(props[key], key))
                  vectorKeys.push(key);
              return vectorKeys;
          }, []);
      };
      var testVectorProps = function (props) {
          return props && testNames.some(function (key) { return isVectorProp(props[key], key); });
      };
      return { getVectorKeys: getVectorKeys, testVectorProps: testVectorProps };
  };
  var unitTypes = [styleValueTypes.px, styleValueTypes.percent, styleValueTypes.degrees, styleValueTypes.vh, styleValueTypes.vw];
  var findUnitType = function (prop) { return unitTypes.find(function (type) { return type.test(prop); }); };
  var isUnitProp = function (prop) { return Boolean(findUnitType(prop)); };
  var createAction = function (action, props) { return action(props); };
  var createUnitAction = function (action, _a) {
      var from = _a.from, to = _a.to, props = tslib_1.__rest(_a, ["from", "to"]);
      var unitType = findUnitType(from) || findUnitType(to);
      var transform = unitType.transform, parse = unitType.parse;
      return action(tslib_1.__assign({}, props, { from: typeof from === 'string' ? parse(from) : from, to: typeof to === 'string' ? parse(to) : to })).pipe(transform);
  };
  var createMixerAction = function (mixer) { return function (action, _a) {
      var from = _a.from, to = _a.to, props = tslib_1.__rest(_a, ["from", "to"]);
      return action(tslib_1.__assign({}, props, { from: 0, to: 1 })).pipe(mixer(from, to));
  }; };
  var createColorAction = createMixerAction(popcorn.mixColor);
  var createComplexAction = createMixerAction(popcorn.mixComplex);
  var createVectorAction = function (action, typeTests) {
      var _a = createVectorTests(typeTests), testVectorProps = _a.testVectorProps, getVectorKeys = _a.getVectorKeys;
      var vectorAction = function (props) {
          var isVector = testVectorProps(props);
          if (!isVector)
              return action(props);
          var vectorKeys = getVectorKeys(props);
          var testKey = vectorKeys[0];
          var testProp = props[testKey];
          return getActionCreator(testProp)(action, props, vectorKeys);
      };
      return vectorAction;
  };
  var getActionCreator = function (prop) {
      if (typeof prop === 'number') {
          return createAction;
      }
      else if (isUnitProp(prop)) {
          return createUnitAction;
      }
      else if (styleValueTypes.color.test(prop)) {
          return createColorAction;
      }
      else if (styleValueTypes.complex.test(prop)) {
          return createComplexAction;
      }
      else {
          return createAction;
      }
  };
  
  var decay = function (props) {
      if (props === void 0) { props = {}; }
      return action(function (_a) {
          var complete = _a.complete, update = _a.update;
          var _b = props.velocity, velocity = _b === void 0 ? 0 : _b, _c = props.from, from = _c === void 0 ? 0 : _c, _d = props.power, power = _d === void 0 ? 0.8 : _d, _e = props.timeConstant, timeConstant = _e === void 0 ? 350 : _e, _f = props.restDelta, restDelta = _f === void 0 ? 0.5 : _f, modifyTarget = props.modifyTarget;
          var elapsed = 0;
          var amplitude = power * velocity;
          var idealTarget = Math.round(from + amplitude);
          var target = typeof modifyTarget === 'undefined'
              ? idealTarget
              : modifyTarget(idealTarget);
          var process = sync__default.update(function (_a) {
              var frameDelta = _a.delta;
              elapsed += frameDelta;
              var delta = -amplitude * Math.exp(-elapsed / timeConstant);
              var isMoving = delta > restDelta || delta < -restDelta;
              var current = isMoving ? target + delta : target;
              update(current);
              if (!isMoving) {
                  sync.cancelSync.update(process);
                  complete();
              }
          }, true);
          return {
              stop: function () { return sync.cancelSync.update(process); }
          };
      });
  };
  var vectorDecay = createVectorAction(decay, {
      from: styleValueTypes.number.test,
      modifyTarget: function (func) { return typeof func === 'function'; },
      velocity: styleValueTypes.number.test
  });
  
  var spring = function (props) {
      if (props === void 0) { props = {}; }
      return action(function (_a) {
          var update = _a.update, complete = _a.complete;
          var _b = props.velocity, velocity = _b === void 0 ? 0.0 : _b;
          var _c = props.from, from = _c === void 0 ? 0.0 : _c, _d = props.to, to = _d === void 0 ? 0.0 : _d, _e = props.stiffness, stiffness = _e === void 0 ? 100 : _e, _f = props.damping, damping = _f === void 0 ? 10 : _f, _g = props.mass, mass = _g === void 0 ? 1.0 : _g, _h = props.restSpeed, restSpeed = _h === void 0 ? 0.01 : _h, _j = props.restDelta, restDelta = _j === void 0 ? 0.01 : _j;
          var initialVelocity = velocity ? -(velocity / 1000) : 0.0;
          var t = 0;
          var delta = to - from;
          var position = from;
          var prevPosition = position;
          var process = sync__default.update(function (_a) {
              var timeDelta = _a.delta;
              t += timeDelta;
              var dampingRatio = damping / (2 * Math.sqrt(stiffness * mass));
              var angularFreq = Math.sqrt(stiffness / mass) / 1000;
              prevPosition = position;
              if (dampingRatio < 1) {
                  var envelope = Math.exp(-dampingRatio * angularFreq * t);
                  var expoDecay = angularFreq * Math.sqrt(1.0 - dampingRatio * dampingRatio);
                  position =
                      to -
                          envelope *
                              (((initialVelocity + dampingRatio * angularFreq * delta) /
                                  expoDecay) *
                                  Math.sin(expoDecay * t) +
                                  delta * Math.cos(expoDecay * t));
              }
              else {
                  var envelope = Math.exp(-angularFreq * t);
                  position =
                      to -
                          envelope * (delta + (initialVelocity + angularFreq * delta) * t);
              }
              velocity = popcorn.velocityPerSecond(position - prevPosition, timeDelta);
              var isBelowVelocityThreshold = Math.abs(velocity) <= restSpeed;
              var isBelowDisplacementThreshold = Math.abs(to - position) <= restDelta;
              if (isBelowVelocityThreshold && isBelowDisplacementThreshold) {
                  position = to;
                  update(position);
                  sync.cancelSync.update(process);
                  complete();
              }
              else {
                  update(position);
              }
          }, true);
          return {
              stop: function () { return sync.cancelSync.update(process); }
          };
      });
  };
  var vectorSpring = createVectorAction(spring, {
      from: styleValueTypes.number.test,
      to: styleValueTypes.number.test,
      stiffness: styleValueTypes.number.test,
      damping: styleValueTypes.number.test,
      mass: styleValueTypes.number.test,
      velocity: styleValueTypes.number.test
  });
  
  var inertia = function (_a) {
      var _b = _a.from, from = _b === void 0 ? 0 : _b, _c = _a.velocity, velocity = _c === void 0 ? 0 : _c, min = _a.min, max = _a.max, _d = _a.power, power = _d === void 0 ? 0.8 : _d, _e = _a.timeConstant, timeConstant = _e === void 0 ? 700 : _e, _f = _a.bounceStiffness, bounceStiffness = _f === void 0 ? 500 : _f, _g = _a.bounceDamping, bounceDamping = _g === void 0 ? 10 : _g, _h = _a.restDelta, restDelta = _h === void 0 ? 1 : _h, modifyTarget = _a.modifyTarget;
      return action(function (_a) {
          var update = _a.update, complete = _a.complete;
          var prev = from;
          var current = from;
          var activeAnimation;
          var isSpring = false;
          var isLessThanMin = function (v) { return min !== undefined && v <= min; };
          var isMoreThanMax = function (v) { return max !== undefined && v >= max; };
          var isOutOfBounds = function (v) { return isLessThanMin(v) || isMoreThanMax(v); };
          var isTravellingAwayFromBounds = function (v, currentVelocity) {
              return ((isLessThanMin(v) && currentVelocity < 0) ||
                  (isMoreThanMax(v) && currentVelocity > 0));
          };
          var onUpdate = function (v) {
              update(v);
              prev = current;
              current = v;
              velocity = popcorn.velocityPerSecond(current - prev, sync.getFrameData().delta);
              if (activeAnimation &&
                  !isSpring &&
                  isTravellingAwayFromBounds(v, velocity)) {
                  startSpring({ from: v, velocity: velocity });
              }
          };
          var startAnimation = function (animation, next) {
              activeAnimation && activeAnimation.stop();
              activeAnimation = animation.start({
                  update: onUpdate,
                  complete: function () {
                      if (next) {
                          next();
                          return;
                      }
                      complete();
                  }
              });
          };
          var startSpring = function (props) {
              isSpring = true;
              startAnimation(vectorSpring(tslib_1.__assign({}, props, { to: isLessThanMin(props.from) ? min : max, stiffness: bounceStiffness, damping: bounceDamping, restDelta: restDelta })));
          };
          if (isOutOfBounds(from)) {
              startSpring({ from: from, velocity: velocity });
          }
          else if (velocity !== 0) {
              var animation = vectorDecay({
                  from: from,
                  velocity: velocity,
                  timeConstant: timeConstant,
                  power: power,
                  restDelta: isOutOfBounds(from) ? 20 : restDelta,
                  modifyTarget: modifyTarget
              });
              startAnimation(animation, function () {
                  if (isOutOfBounds(current)) {
                      startSpring({ from: current, velocity: velocity });
                  }
                  else {
                      complete();
                  }
              });
          }
          else {
              complete();
          }
          return {
              stop: function () { return activeAnimation && activeAnimation.stop(); }
          };
      });
  };
  var index = createVectorAction(inertia, {
      from: styleValueTypes.number.test,
      velocity: styleValueTypes.number.test,
      min: styleValueTypes.number.test,
      max: styleValueTypes.number.test,
      damping: styleValueTypes.number.test,
      stiffness: styleValueTypes.number.test,
      modifyTarget: function (func) { return typeof func === 'function'; }
  });
  
  var frame = function () {
      return action(function (_a) {
          var update = _a.update;
          var initialTime = 0;
          var process = sync__default.update(function (_a) {
              var timestamp = _a.timestamp;
              if (!initialTime)
                  initialTime = timestamp;
              update(timestamp - initialTime);
          }, true, true);
          return {
              stop: function () { return sync.cancelSync.update(process); }
          };
      });
  };
  
  var scrubber = function (_a) {
      var _b = _a.from, from = _b === void 0 ? 0 : _b, _c = _a.to, to = _c === void 0 ? 1 : _c, _d = _a.ease, ease = _d === void 0 ? easing.linear : _d, _e = _a.reverseEase, reverseEase = _e === void 0 ? false : _e;
      if (reverseEase) {
          ease = easing.createReversedEasing(ease);
      }
      return action(function (_a) {
          var update = _a.update;
          return ({
              seek: function (progress) { return update(progress); }
          });
      }).pipe(ease, function (v) { return popcorn.mix(from, to, v); });
  };
  var vectorScrubber = createVectorAction(scrubber, {
      ease: function (func) { return typeof func === 'function'; },
      from: styleValueTypes.number.test,
      to: styleValueTypes.number.test
  });
  
  var clampProgress = popcorn.clamp(0, 1);
  var tween = function (props) {
      if (props === void 0) { props = {}; }
      return action(function (_a) {
          var update = _a.update, complete = _a.complete;
          var _b = props.duration, duration = _b === void 0 ? 300 : _b, _c = props.ease, ease = _c === void 0 ? easing.easeOut : _c, _d = props.flip, flip = _d === void 0 ? 0 : _d, _e = props.loop, loop = _e === void 0 ? 0 : _e, _f = props.yoyo, yoyo = _f === void 0 ? 0 : _f, _g = props.repeatDelay, repeatDelay = _g === void 0 ? 0 : _g;
          var _h = props.from, from = _h === void 0 ? 0 : _h, _j = props.to, to = _j === void 0 ? 1 : _j, _k = props.elapsed, elapsed = _k === void 0 ? 0 : _k, _l = props.flipCount, flipCount = _l === void 0 ? 0 : _l, _m = props.yoyoCount, yoyoCount = _m === void 0 ? 0 : _m, _o = props.loopCount, loopCount = _o === void 0 ? 0 : _o;
          var playhead = vectorScrubber({ from: from, to: to, ease: ease }).start(update);
          var currentProgress = 0;
          var process;
          var isActive = false;
          var reverseAnimation = function (reverseEase) {
              var _a;
              if (reverseEase === void 0) { reverseEase = false; }
              _a = [to, from], from = _a[0], to = _a[1];
              playhead = vectorScrubber({ from: from, to: to, ease: ease, reverseEase: reverseEase }).start(update);
          };
          var isTweenComplete = function () {
              var isComplete = isActive && elapsed > duration + repeatDelay;
              if (!isComplete)
                  return false;
              if (isComplete && !loop && !flip && !yoyo)
                  return true;
              var overtime = elapsed - duration;
              elapsed = overtime - repeatDelay;
              if (loop && loopCount < loop) {
                  loopCount++;
                  return false;
              }
              else if (flip && flipCount < flip) {
                  flipCount++;
                  reverseAnimation();
                  return false;
              }
              else if (yoyo && yoyoCount < yoyo) {
                  yoyoCount++;
                  reverseAnimation(yoyoCount % 2 !== 0);
                  return false;
              }
              return true;
          };
          var updateTween = function () {
              currentProgress = clampProgress(popcorn.progress(0, duration, elapsed));
              playhead.seek(currentProgress);
          };
          var startTimer = function () {
              isActive = true;
              process = sync__default.update(function (_a) {
                  var delta = _a.delta;
                  elapsed += delta;
                  updateTween();
                  if (isTweenComplete()) {
                      sync.cancelSync.update(process);
                      complete && sync__default.update(complete, false, true);
                  }
              }, true);
          };
          var stopTimer = function () {
              isActive = false;
              if (process)
                  sync.cancelSync.update(process);
          };
          startTimer();
          return {
              isActive: function () { return isActive; },
              getElapsed: function () { return popcorn.clamp(0, duration, elapsed); },
              getProgress: function () { return currentProgress; },
              stop: function () {
                  stopTimer();
              },
              pause: function () {
                  stopTimer();
                  return this;
              },
              resume: function () {
                  if (!isActive)
                      startTimer();
                  return this;
              },
              seek: function (newProgress) {
                  elapsed = popcorn.mix(0, duration, newProgress);
                  sync__default.update(updateTween, false, true);
                  return this;
              },
              reverse: function () {
                  reverseAnimation();
                  return this;
              }
          };
      });
  };
  
  var clampProgress$1 = popcorn.clamp(0, 1);
  var defaultEasings = function (values, easing$1) {
      return values.map(function () { return easing$1 || easing.easeOut; }).splice(0, values.length - 1);
  };
  var defaultTimings = function (values) {
      var numValues = values.length;
      return values.map(function (value, i) { return (i !== 0 ? i / (numValues - 1) : 0); });
  };
  var interpolateScrubbers = function (input, scrubbers, update) {
      var rangeLength = input.length;
      var finalInputIndex = rangeLength - 1;
      var finalScrubberIndex = finalInputIndex - 1;
      var subs = scrubbers.map(function (scrub) { return scrub.start(update); });
      return function (v) {
          if (v <= input[0]) {
              subs[0].seek(0);
          }
          if (v >= input[finalInputIndex]) {
              subs[finalScrubberIndex].seek(1);
          }
          var i = 1;
          for (; i < rangeLength; i++) {
              if (input[i] > v || i === finalInputIndex)
                  break;
          }
          var progressInRange = popcorn.progress(input[i - 1], input[i], v);
          subs[i - 1].seek(clampProgress$1(progressInRange));
      };
  };
  var keyframes = function (_a) {
      var easings = _a.easings, _b = _a.ease, ease = _b === void 0 ? easing.linear : _b, times = _a.times, values = _a.values, tweenProps = tslib_1.__rest(_a, ["easings", "ease", "times", "values"]);
      easings = Array.isArray(easings)
          ? easings
          : defaultEasings(values, easings);
      times = times || defaultTimings(values);
      var scrubbers = easings.map(function (easing, i) {
          return vectorScrubber({
              from: values[i],
              to: values[i + 1],
              ease: easing
          });
      });
      return tween(tslib_1.__assign({}, tweenProps, { ease: ease })).applyMiddleware(function (update) { return interpolateScrubbers(times, scrubbers, update); });
  };
  
  var physics = function (props) {
      if (props === void 0) { props = {}; }
      return action(function (_a) {
          var complete = _a.complete, update = _a.update;
          var _b = props.acceleration, acceleration = _b === void 0 ? 0 : _b, _c = props.friction, friction = _c === void 0 ? 0 : _c, _d = props.velocity, velocity = _d === void 0 ? 0 : _d, springStrength = props.springStrength, to = props.to;
          var _e = props.restSpeed, restSpeed = _e === void 0 ? 0.001 : _e, _f = props.from, from = _f === void 0 ? 0 : _f;
          var current = from;
          var process = sync__default.update(function (_a) {
              var delta = _a.delta;
              var elapsed = Math.max(delta, 16);
              if (acceleration)
                  velocity += popcorn.velocityPerFrame(acceleration, elapsed);
              if (friction)
                  velocity *= Math.pow((1 - friction), (elapsed / 100));
              if (springStrength !== undefined && to !== undefined) {
                  var distanceToTarget = to - current;
                  velocity +=
                      distanceToTarget * popcorn.velocityPerFrame(springStrength, elapsed);
              }
              current += popcorn.velocityPerFrame(velocity, elapsed);
              update(current);
              var isComplete = restSpeed !== false && (!velocity || Math.abs(velocity) <= restSpeed);
              if (isComplete) {
                  sync.cancelSync.update(process);
                  complete();
              }
          }, true);
          return {
              set: function (v) {
                  current = v;
                  return this;
              },
              setAcceleration: function (v) {
                  acceleration = v;
                  return this;
              },
              setFriction: function (v) {
                  friction = v;
                  return this;
              },
              setSpringStrength: function (v) {
                  springStrength = v;
                  return this;
              },
              setSpringTarget: function (v) {
                  to = v;
                  return this;
              },
              setVelocity: function (v) {
                  velocity = v;
                  return this;
              },
              stop: function () { return sync.cancelSync.update(process); }
          };
      });
  };
  var vectorPhysics = createVectorAction(physics, {
      acceleration: styleValueTypes.number.test,
      friction: styleValueTypes.number.test,
      velocity: styleValueTypes.number.test,
      from: styleValueTypes.number.test,
      to: styleValueTypes.number.test,
      springStrength: styleValueTypes.number.test
  });
  
  var multi = function (_a) {
      var getCount = _a.getCount, getFirst = _a.getFirst, getOutput = _a.getOutput, mapApi = _a.mapApi, setProp = _a.setProp, startActions = _a.startActions;
      return function (actions) {
          return action(function (_a) {
              var update = _a.update, complete = _a.complete, error = _a.error;
              var numActions = getCount(actions);
              var output = getOutput();
              var updateOutput = function () { return update(output); };
              var numCompletedActions = 0;
              var subs = startActions(actions, function (a, name) {
                  var hasCompleted = false;
                  return a.start({
                      complete: function () {
                          if (!hasCompleted) {
                              hasCompleted = true;
                              numCompletedActions++;
                              if (numCompletedActions === numActions)
                                  sync__default.update(complete);
                          }
                      },
                      error: error,
                      update: function (v) {
                          setProp(output, name, v);
                          sync__default.update(updateOutput, false, true);
                      }
                  });
              });
              return Object.keys(getFirst(subs)).reduce(function (api, methodName) {
                  api[methodName] = mapApi(subs, methodName);
                  return api;
              }, {});
          });
      };
  };
  
  var composite = multi({
      getOutput: function () { return ({}); },
      getCount: function (subs) { return Object.keys(subs).length; },
      getFirst: function (subs) { return subs[Object.keys(subs)[0]]; },
      mapApi: function (subs, methodName) { return function () {
          var args = [];
          for (var _i = 0; _i < arguments.length; _i++) {
              args[_i] = arguments[_i];
          }
          return Object.keys(subs)
              .reduce(function (output, propKey) {
              var _a;
              if (subs[propKey][methodName]) {
                  (args[0] && args[0][propKey] !== undefined)
                      ? output[propKey] = subs[propKey][methodName](args[0][propKey])
                      : output[propKey] = (_a = subs[propKey])[methodName].apply(_a, args);
              }
              return output;
          }, {});
      }; },
      setProp: function (output, name, v) { return output[name] = v; },
      startActions: function (actions, starter) { return Object.keys(actions)
          .reduce(function (subs, key) {
          subs[key] = starter(actions[key], key);
          return subs;
      }, {}); }
  });
  
  var DEFAULT_DURATION = 300;
  var flattenTimings = function (instructions) {
      var flatInstructions = [];
      var lastArg = instructions[instructions.length - 1];
      var isStaggered = typeof lastArg === 'number';
      var staggerDelay = isStaggered ? lastArg : 0;
      var segments = isStaggered ? instructions.slice(0, -1) : instructions;
      var numSegments = segments.length;
      var offset = 0;
      segments.forEach(function (item, i) {
          flatInstructions.push(item);
          if (i !== numSegments - 1) {
              var duration = item.duration || DEFAULT_DURATION;
              offset += staggerDelay;
              flatInstructions.push("-" + (duration - offset));
          }
      });
      return flatInstructions;
  };
  var flattenArrayInstructions = function (instructions, instruction) {
      Array.isArray(instruction)
          ? instructions.push.apply(instructions, flattenTimings(instruction)) : instructions.push(instruction);
      return instructions;
  };
  var convertDefToProps = function (props, def, i) {
      var duration = props.duration, easings = props.easings, times = props.times, values = props.values;
      var numValues = values.length;
      var prevTimeTo = times[numValues - 1];
      var timeFrom = def.at === 0 ? 0 : def.at / duration;
      var timeTo = (def.at + def.duration) / duration;
      if (i === 0) {
          values.push(def.from);
          times.push(timeFrom);
      }
      else {
          if (prevTimeTo !== timeFrom) {
              if (def.from !== undefined) {
                  values.push(values[numValues - 1]);
                  times.push(timeFrom);
                  easings.push(easing.linear);
              }
              var from = def.from !== undefined ? def.from : values[numValues - 1];
              values.push(from);
              times.push(timeFrom);
              easings.push(easing.linear);
          }
          else if (def.from !== undefined) {
              values.push(def.from);
              times.push(timeFrom);
              easings.push(easing.linear);
          }
      }
      values.push(def.to);
      times.push(timeTo);
      easings.push(def.ease || easing.easeInOut);
      return props;
  };
  var timeline = function (instructions, _a) {
      var _b = _a === void 0 ? {} : _a, duration = _b.duration, elapsed = _b.elapsed, ease = _b.ease, loop = _b.loop, flip = _b.flip, yoyo = _b.yoyo;
      var playhead = 0;
      var calculatedDuration = 0;
      var flatInstructions = instructions.reduce(flattenArrayInstructions, []);
      var animationDefs = [];
      flatInstructions.forEach(function (instruction) {
          if (typeof instruction === 'string') {
              playhead += parseFloat(instruction);
          }
          else if (typeof instruction === 'number') {
              playhead = instruction;
          }
          else {
              var def = tslib_1.__assign({}, instruction, { at: playhead });
              def.duration =
                  def.duration === undefined ? DEFAULT_DURATION : def.duration;
              animationDefs.push(def);
              playhead += def.duration;
              calculatedDuration = Math.max(calculatedDuration, def.at + def.duration);
          }
      });
      var tracks = {};
      var numDefs = animationDefs.length;
      for (var i = 0; i < numDefs; i++) {
          var def = animationDefs[i];
          var track = def.track;
          if (track === undefined) {
              throw new Error('No track defined');
          }
          if (!tracks.hasOwnProperty(track))
              tracks[track] = [];
          tracks[track].push(def);
      }
      var trackKeyframes = {};
      for (var key in tracks) {
          if (tracks.hasOwnProperty(key)) {
              var keyframeProps = tracks[key].reduce(convertDefToProps, {
                  duration: calculatedDuration,
                  easings: [],
                  times: [],
                  values: []
              });
              trackKeyframes[key] = keyframes(tslib_1.__assign({}, keyframeProps, { duration: duration || calculatedDuration, ease: ease,
                  elapsed: elapsed,
                  loop: loop,
                  yoyo: yoyo,
                  flip: flip }));
          }
      }
      return composite(trackKeyframes);
  };
  
  var listen = function (element, events, options) { return action(function (_a) {
      var update = _a.update;
      var eventNames = events.split(' ').map(function (eventName) {
          element.addEventListener(eventName, update, options);
          return eventName;
      });
      return {
          stop: function () { return eventNames.forEach(function (eventName) { return element.removeEventListener(eventName, update, options); }); }
      };
  }); };
  
  var defaultPointerPos = function () { return ({
      clientX: 0,
      clientY: 0,
      pageX: 0,
      pageY: 0,
      x: 0,
      y: 0
  }); };
  var eventToPoint = function (e, point) {
      if (point === void 0) { point = defaultPointerPos(); }
      point.clientX = point.x = e.clientX;
      point.clientY = point.y = e.clientY;
      point.pageX = e.pageX;
      point.pageY = e.pageY;
      return point;
  };
  
  var points = [defaultPointerPos()];
  var isTouchDevice = false;
  if (typeof document !== 'undefined') {
      var updatePointsLocation = function (_a) {
          var touches = _a.touches;
          isTouchDevice = true;
          var numTouches = touches.length;
          points.length = 0;
          for (var i = 0; i < numTouches; i++) {
              var thisTouch = touches[i];
              points.push(eventToPoint(thisTouch));
          }
      };
      listen(document, 'touchstart touchmove', {
          passive: true,
          capture: true
      }).start(updatePointsLocation);
  }
  var multitouch = function (_a) {
      var _b = _a === void 0 ? {} : _a, _c = _b.preventDefault, preventDefault = _c === void 0 ? true : _c, _d = _b.scale, scale = _d === void 0 ? 1.0 : _d, _e = _b.rotate, rotate = _e === void 0 ? 0.0 : _e;
      return action(function (_a) {
          var update = _a.update;
          var output = {
              touches: points,
              scale: scale,
              rotate: rotate
          };
          var initialDistance = 0.0;
          var initialRotation = 0.0;
          var isGesture = points.length > 1;
          if (isGesture) {
              var firstTouch = points[0], secondTouch = points[1];
              initialDistance = popcorn.distance(firstTouch, secondTouch);
              initialRotation = popcorn.angle(firstTouch, secondTouch);
          }
          var updatePoint = function () {
              if (isGesture) {
                  var firstTouch = points[0], secondTouch = points[1];
                  var newDistance = popcorn.distance(firstTouch, secondTouch);
                  var newRotation = popcorn.angle(firstTouch, secondTouch);
                  output.scale = scale * (newDistance / initialDistance);
                  output.rotate = rotate + (newRotation - initialRotation);
              }
              update(output);
          };
          var onMove = function (e) {
              if (preventDefault || e.touches.length > 1)
                  e.preventDefault();
              sync__default.update(updatePoint);
          };
          var updateOnMove = listen(document, 'touchmove', {
              passive: !preventDefault
          }).start(onMove);
          if (isTouchDevice)
              sync__default.update(updatePoint);
          return {
              stop: function () {
                  sync.cancelSync.update(updatePoint);
                  updateOnMove.stop();
              }
          };
      });
  };
  var getIsTouchDevice = function () { return isTouchDevice; };
  
  var point = defaultPointerPos();
  var isMouseDevice = false;
  if (typeof document !== 'undefined') {
      var updatePointLocation = function (e) {
          isMouseDevice = true;
          eventToPoint(e, point);
      };
      listen(document, 'mousedown mousemove', true).start(updatePointLocation);
  }
  var mouse = function (_a) {
      var _b = (_a === void 0 ? {} : _a).preventDefault, preventDefault = _b === void 0 ? true : _b;
      return action(function (_a) {
          var update = _a.update;
          var updatePoint = function () { return update(point); };
          var onMove = function (e) {
              if (preventDefault)
                  e.preventDefault();
              sync__default.update(updatePoint);
          };
          var updateOnMove = listen(document, 'mousemove').start(onMove);
          if (isMouseDevice)
              sync__default.update(updatePoint);
          return {
              stop: function () {
                  sync.cancelSync.update(updatePoint);
                  updateOnMove.stop();
              }
          };
      });
  };
  
  var getFirstTouch = function (_a) {
      var firstTouch = _a[0];
      return firstTouch;
  };
  var pointer = function (props) {
      if (props === void 0) { props = {}; }
      return getIsTouchDevice()
          ? multitouch(props).pipe(function (_a) {
              var touches = _a.touches;
              return touches;
          }, getFirstTouch)
          : mouse(props);
  };
  var index$1 = (function (_a) {
      if (_a === void 0) { _a = {}; }
      var x = _a.x, y = _a.y, props = tslib_1.__rest(_a, ["x", "y"]);
      if (x !== undefined || y !== undefined) {
          var applyXOffset_1 = popcorn.applyOffset(x || 0);
          var applyYOffset_1 = popcorn.applyOffset(y || 0);
          var delta_1 = { x: 0, y: 0 };
          return pointer(props).pipe(function (point) {
              delta_1.x = applyXOffset_1(point.x);
              delta_1.y = applyYOffset_1(point.y);
              return delta_1;
          });
      }
      else {
          return pointer(props);
      }
  });
  
  var chain = function () {
      var actions = [];
      for (var _i = 0; _i < arguments.length; _i++) {
          actions[_i] = arguments[_i];
      }
      return action(function (_a) {
          var update = _a.update, complete = _a.complete;
          var i = 0;
          var current;
          var playCurrent = function () {
              current = actions[i].start({
                  complete: function () {
                      i++;
                      (i >= actions.length) ? complete() : playCurrent();
                  },
                  update: update
              });
          };
          playCurrent();
          return {
              stop: function () { return current && current.stop(); }
          };
      });
  };
  
  var parallel = multi({
      getOutput: function () { return ([]); },
      getCount: function (subs) { return subs.length; },
      getFirst: function (subs) { return subs[0]; },
      mapApi: function (subs, methodName) { return function () {
          var args = [];
          for (var _i = 0; _i < arguments.length; _i++) {
              args[_i] = arguments[_i];
          }
          return subs.map(function (sub, i) {
              if (sub[methodName]) {
                  return Array.isArray(args[0])
                      ? sub[methodName](args[0][i])
                      : sub[methodName].apply(sub, args);
              }
          });
      }; },
      setProp: function (output, name, v) { return output[name] = v; },
      startActions: function (actions, starter) { return actions.map(function (action, i) { return starter(action, i); }); }
  });
  var parallel$1 = (function () {
      var actions = [];
      for (var _i = 0; _i < arguments.length; _i++) {
          actions[_i] = arguments[_i];
      }
      return parallel(actions);
  });
  
  var crossfade = function (a, b) {
      return action(function (observer) {
          var balance = 0;
          var fadable = parallel$1(a, b).start(tslib_1.__assign({}, observer, { update: function (_a) {
                  var va = _a[0], vb = _a[1];
                  observer.update(popcorn.mix(va, vb, balance));
              } }));
          return {
              setBalance: function (v) { return (balance = v); },
              stop: function () { return fadable.stop(); }
          };
      });
  };
  
  var delay = function (timeToDelay) { return action(function (_a) {
      var complete = _a.complete;
      var timeout = setTimeout(complete, timeToDelay);
      return {
          stop: function () { return clearTimeout(timeout); }
      };
  }); };
  
  var merge = function () {
      var actions = [];
      for (var _i = 0; _i < arguments.length; _i++) {
          actions[_i] = arguments[_i];
      }
      return action(function (observer) {
          var subs = actions.map(function (thisAction) { return thisAction.start(observer); });
          return {
              stop: function () { return subs.forEach(function (sub) { return sub.stop(); }); }
          };
      });
  };
  
  var schedule = function (scheduler, schedulee) { return action(function (_a) {
      var update = _a.update, complete = _a.complete;
      var latest;
      var schedulerSub = scheduler.start({
          update: function () { return latest !== undefined && update(latest); },
          complete: complete
      });
      var scheduleeSub = schedulee.start({
          update: function (v) { return latest = v; },
          complete: complete
      });
      return {
          stop: function () {
              schedulerSub.stop();
              scheduleeSub.stop();
          }
      };
  }); };
  
  var stagger = function (actions, interval) {
      var intervalIsNumber = typeof interval === 'number';
      var actionsWithDelay = actions.map(function (a, i) {
          var timeToDelay = intervalIsNumber ? interval * i : interval(i);
          return chain(delay(timeToDelay), a);
      });
      return parallel$1.apply(void 0, actionsWithDelay);
  };
  
  var styler = function () {
      return heyListen.invariant(false, 'styler has been removed from Popmotion in 9.0. Downgrade to 8.x or npm install stylefire');
  };
  
  exports.Action = Action;
  exports.ValueReaction = ValueReaction;
  exports.action = action;
  exports.chain = chain;
  exports.composite = composite;
  exports.crossfade = crossfade;
  exports.decay = vectorDecay;
  exports.delay = delay;
  exports.everyFrame = frame;
  exports.inertia = index;
  exports.keyframes = keyframes;
  exports.listen = listen;
  exports.merge = merge;
  exports.mouse = mouse;
  exports.multicast = multicast;
  exports.multitouch = multitouch;
  exports.parallel = parallel$1;
  exports.physics = vectorPhysics;
  exports.pointer = index$1;
  exports.schedule = schedule;
  exports.spring = vectorSpring;
  exports.stagger = stagger;
  exports.styler = styler;
  exports.timeline = timeline;
  exports.tween = tween;
  exports.value = value;
  