"use strict";

exports.__esModule = true;
exports.default = exports.SWIFT_OUT = exports.LINEAR = exports.EASE_OUT_SINE = exports.EASE_OUT_QUINT = exports.EASE_OUT_QUART = exports.EASE_OUT_QUAD = exports.EASE_OUT_EXPO = exports.EASE_OUT_CUBIC = exports.EASE_OUT_CIRC = exports.EASE_OUT_BACK = exports.EASE_IN_SINE = exports.EASE_IN_QUINT = exports.EASE_IN_QUART = exports.EASE_IN_QUAD = exports.EASE_IN_OUT_SINE = exports.EASE_IN_OUT_QUINT = exports.EASE_IN_OUT_QUART = exports.EASE_IN_OUT_QUAD = exports.EASE_IN_OUT_EXPO = exports.EASE_IN_OUT_CUBIC = exports.EASE_IN_OUT_CIRC = exports.EASE_IN_OUT_BACK = exports.EASE_IN_EXPO = exports.EASE_IN_CUBIC = exports.EASE_IN_CIRC = exports.EASE_IN_BACK = exports.BOUNCE = void 0;
var _noop2 = _interopRequireDefault(require("lodash/fp/noop"));
var _min2 = _interopRequireDefault(require("lodash/fp/min"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _cubicBezier = _interopRequireDefault(require("cubic-bezier"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const EPSILON = 1000;
const LINEAR = exports.LINEAR = (0, _cubicBezier.default)(0, 0, 1, 1, EPSILON);
const EASE_IN_QUAD = exports.EASE_IN_QUAD = (0, _cubicBezier.default)(0.55, 0.08, 0.68, 0.53, EPSILON);
const EASE_IN_CUBIC = exports.EASE_IN_CUBIC = (0, _cubicBezier.default)(0.55, 0.05, 0.67, 0.19, EPSILON);
const EASE_IN_QUART = exports.EASE_IN_QUART = (0, _cubicBezier.default)(0.89, 0.03, 0.68, 0.22, EPSILON);
const EASE_IN_QUINT = exports.EASE_IN_QUINT = (0, _cubicBezier.default)(0.75, 0.05, 0.85, 0.06, EPSILON);
const EASE_IN_SINE = exports.EASE_IN_SINE = (0, _cubicBezier.default)(0.47, 0, 0.74, 0.71, EPSILON);
const EASE_IN_EXPO = exports.EASE_IN_EXPO = (0, _cubicBezier.default)(0.95, 0.05, 0.79, 0.03, EPSILON);
const EASE_IN_CIRC = exports.EASE_IN_CIRC = (0, _cubicBezier.default)(0.6, 0.04, 0.98, 0.33, EPSILON);
const EASE_IN_BACK = exports.EASE_IN_BACK = (0, _cubicBezier.default)(0.6, -0.28, 0.73, 0.04, EPSILON);
const EASE_OUT_QUAD = exports.EASE_OUT_QUAD = (0, _cubicBezier.default)(0.25, 0.46, 0.45, 0.94, EPSILON);
const EASE_OUT_CUBIC = exports.EASE_OUT_CUBIC = (0, _cubicBezier.default)(0.21, 0.61, 0.35, 1, EPSILON);
const EASE_OUT_QUART = exports.EASE_OUT_QUART = (0, _cubicBezier.default)(0.16, 0.84, 0.44, 1, EPSILON);
const EASE_OUT_QUINT = exports.EASE_OUT_QUINT = (0, _cubicBezier.default)(0.23, 1, 0.32, 1, EPSILON);
const EASE_OUT_SINE = exports.EASE_OUT_SINE = (0, _cubicBezier.default)(0.39, 0.57, 0.56, 1, EPSILON);
const EASE_OUT_EXPO = exports.EASE_OUT_EXPO = (0, _cubicBezier.default)(0.19, 1, 0.22, 1, EPSILON);
const EASE_OUT_CIRC = exports.EASE_OUT_CIRC = (0, _cubicBezier.default)(0.07, 0.82, 0.16, 1, EPSILON);
const EASE_OUT_BACK = exports.EASE_OUT_BACK = (0, _cubicBezier.default)(0.17, 0.88, 0.32, 1.27, EPSILON);
const EASE_IN_OUT_QUAD = exports.EASE_IN_OUT_QUAD = (0, _cubicBezier.default)(0.45, 0.03, 0.51, 0.95, EPSILON);
const EASE_IN_OUT_CUBIC = exports.EASE_IN_OUT_CUBIC = (0, _cubicBezier.default)(0.64, 0.04, 0.35, 1, EPSILON);
const EASE_IN_OUT_QUART = exports.EASE_IN_OUT_QUART = (0, _cubicBezier.default)(0.77, 0, 0.17, 1, EPSILON);
const EASE_IN_OUT_QUINT = exports.EASE_IN_OUT_QUINT = (0, _cubicBezier.default)(0.86, 0, 0.07, 1, EPSILON);
const EASE_IN_OUT_SINE = exports.EASE_IN_OUT_SINE = (0, _cubicBezier.default)(0.44, 0.05, 0.55, 0.95, EPSILON);
const EASE_IN_OUT_EXPO = exports.EASE_IN_OUT_EXPO = (0, _cubicBezier.default)(1, 0, 0, 1, EPSILON);
const EASE_IN_OUT_CIRC = exports.EASE_IN_OUT_CIRC = (0, _cubicBezier.default)(0.78, 0.13, 0.15, 0.86, EPSILON);
const EASE_IN_OUT_BACK = exports.EASE_IN_OUT_BACK = (0, _cubicBezier.default)(0.68, -0.55, 0.26, 1.55, EPSILON);
const SWIFT_OUT = exports.SWIFT_OUT = (0, _cubicBezier.default)(0.55, 0, 0.1, 1, EPSILON);
const BOUNCE = exports.BOUNCE = (0, _cubicBezier.default)(0.65, 1.95, 0.03, 0.3, EPSILON);
class Animation extends _react.default.Component {
  constructor(props) {
    super(props);
    this.state = {
      progress: 0,
      requestID: null
    };
  }
  componentDidMount() {
    const {
      animated = false
    } = this.props;
    if (animated) this.startAnimation();
  }
  componentDidUpdate(prevProps) {
    const {
      animated: nextAnimated = false
    } = this.props;
    const {
      animated: prevAnimated = false
    } = prevProps;
    if (nextAnimated === prevAnimated) return;
    if (nextAnimated) this.startAnimation();else this.stopAnimation();
  }
  componentWillUnmount() {
    this.stopAnimation();
  }
  startAnimation() {
    this.setState(prevState => {
      const {
        requestID
      } = prevState;
      if (requestID) cancelAnimationFrame(requestID);
      return {
        requestID: requestAnimationFrame(this.tick.bind(this))
      };
    });
  }
  stopAnimation() {
    this.setState(prevState => {
      const {
        requestID
      } = prevState;
      if (requestID) cancelAnimationFrame(requestID);
      return {
        lastTimestamp: null,
        requestID: null
      };
    });
  }
  tick(timestamp) {
    this.setState(prevState => {
      const {
        lastTimestamp
      } = prevState;
      if (!lastTimestamp) return {
        lastTimestamp: timestamp
      };
      const deltaDuration = timestamp - lastTimestamp;
      return {
        progress: (0, _min2.default)([prevState.progress + this.getProgress(deltaDuration), 1]),
        lastTimestamp: timestamp
      };
    }, () => {
      const {
        progress
      } = this.state;
      if (progress < 1) return this.startAnimation();
      const {
        onAnimationEnd = _noop2.default,
        name
      } = this.props;
      onAnimationEnd(name);
    });
  }
  getProgress(deltaDuration) {
    const {
      duration
    } = this.props;
    return deltaDuration / duration;
  }
  render() {
    const {
      bezier: fnBezier,
      children
    } = this.props;
    const {
      progress
    } = this.state;
    const value = fnBezier(progress);
    return children(value);
  }
}
Animation.propTypes = process.env.NODE_ENV !== "production" ? {
  name: _propTypes.default.string.isRequired,
  bezier: _propTypes.default.func.isRequired,
  duration: _propTypes.default.number.isRequired,
  onAnimationEnd: _propTypes.default.func,
  animated: _propTypes.default.bool,
  children: _propTypes.default.func
} : {};
var _default = exports.default = Animation;
//# sourceMappingURL=index.js.map