"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.numberAnimationProps = exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _vue = require("vue");
var _lodashEs = require("lodash");
var _LocaleReceiver = require("../locale-provider/LocaleReceiver");
var _type = require("../_util/type");
var numberAnimationProps = {
  to: {
    type: Number,
    default: 0
  },
  precision: {
    type: Number,
    default: 0
  },
  showSeparator: Boolean,
  locale: String,
  from: {
    type: Number,
    default: 0
  },
  active: {
    type: Boolean,
    default: true
  },
  duration: {
    type: Number,
    default: 2000
  },
  onFinish: Function
};
exports.numberAnimationProps = numberAnimationProps;
var easeOut = function easeOut(t) {
  return 1 - Math.pow(1 - t, 5);
};
function tween(props) {
  var from = props.from,
    to = props.to,
    duration = props.duration,
    onUpdate = props.onUpdate,
    onFinish = props.onFinish;
  var tick = function tick() {
    var current = performance.now();
    var elapsedTime = Math.min(current - startTime, duration);
    var currentValue = from + (to - from) * easeOut(elapsedTime / duration);
    if (elapsedTime === duration) {
      onFinish();
      return;
    }
    onUpdate(currentValue);
    requestAnimationFrame(tick);
  };
  var startTime = performance.now();
  tick();
}
var NumberAnimation = (0, _vue.defineComponent)({
  name: 'PfNumberAnimation',
  props: numberAnimationProps,
  setup: function setup(props) {
    var _useLocaleReceiver = (0, _LocaleReceiver.useLocaleReceiver)(null),
      _useLocaleReceiver2 = (0, _slicedToArray2.default)(_useLocaleReceiver, 1),
      mergedLocale = _useLocaleReceiver2[0];
    var duration = props.duration;
    var displayedValueRef = (0, _vue.ref)(props.from);
    var mergedLocaleRef = (0, _vue.computed)(function () {
      var locale = props.locale;
      if (locale !== undefined) return locale;
      return mergedLocale.value.lang;
    });
    var animating = false;
    var onUpdate = function onUpdate(currentValue) {
      displayedValueRef.value = currentValue;
    };
    var onFinish = function onFinish() {
      var _props$onFinish;
      displayedValueRef.value = props.to;
      animating = false;
      (_props$onFinish = props.onFinish) === null || _props$onFinish === void 0 ? void 0 : _props$onFinish.call(props);
    };
    var animate = function animate() {
      var from = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : props.from;
      var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : props.to;
      animating = true;
      displayedValueRef.value = props.from;
      if (from !== to) {
        tween({
          from: from,
          to: to,
          duration: duration,
          onUpdate: onUpdate,
          onFinish: onFinish
        });
      }
    };
    var formattedValueRef = (0, _vue.computed)(function () {
      var _numberFormatter$form;
      var formatted = (0, _lodashEs.round)(displayedValueRef.value, props.precision).toFixed(props.precision);
      var splitValue = formatted.split('.');
      var numberFormatter = new Intl.NumberFormat(mergedLocaleRef.value);
      var decimalSeparator = (_numberFormatter$form = numberFormatter.formatToParts(0.5).find(function (part) {
        return part.type === 'decimal';
      })) === null || _numberFormatter$form === void 0 ? void 0 : _numberFormatter$form.value;
      var integer = props.showSeparator ? numberFormatter.format(Number(splitValue[0])) : splitValue[0];
      var decimal = splitValue[1];
      return {
        integer: integer,
        decimal: decimal,
        decimalSeparator: decimalSeparator
      };
    });
    function play() {
      if (animating) return;
      animate();
    }
    (0, _vue.onMounted)(function () {
      (0, _vue.watchEffect)(function () {
        if (props.active) animate();
      });
    });
    var exposedMethods = {
      play: play
    };
    return (0, _objectSpread2.default)({
      formattedValue: formattedValueRef
    }, exposedMethods);
  },
  render: function render() {
    var _this$formattedValue = this.formattedValue,
      integer = _this$formattedValue.integer,
      decimal = _this$formattedValue.decimal,
      decimalSeparator = _this$formattedValue.decimalSeparator;
    return [integer, decimal ? decimalSeparator : null, decimal];
  }
});
var _default = (0, _type.withInstall)(NumberAnimation);
exports.default = _default;