"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.statisticTimerProps = exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _vue = require("vue");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _type = require("../_util/type");
var _Statistic = _interopRequireWildcard(require("./Statistic"));
var _utils = require("./utils");
var _propsUtil = require("../_util/props-util");
var _lodash = require("lodash");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const UPDATE_INTERVAL = 1000 / 60;
const statisticTimerProps = () => (0, _extends2.default)((0, _extends2.default)({}, (0, _Statistic.statisticProps)()), {
  type: (0, _type.stringType)(),
  format: String,
  onFinish: Function,
  onChange: Function
});
exports.statisticTimerProps = statisticTimerProps;
function getTime(value) {
  return new Date(value).getTime();
}
var _default = exports.default = (0, _vue.defineComponent)({
  name: 'AStatisticTimer',
  props: (0, _propsUtil.initDefaultProps)(statisticTimerProps(), {
    format: 'HH:mm:ss'
  }),
  slots: Object,
  setup(props, {
    slots
  }) {
    const down = (0, _vue.computed)(() => props.type === 'countdown');
    const statisticRef = (0, _vue.ref)();
    // ======================== Update ========================
    const update = () => {
      var _a, _b;
      const now = Date.now();
      const timestamp = getTime(props.value);
      const timeDiff = !down.value ? now - timestamp : timestamp - now;
      (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, timeDiff);
      if (down.value && timestamp < now) {
        (_b = props.onFinish) === null || _b === void 0 ? void 0 : _b.call(props);
        return false;
      }
      return true;
    };
    (0, _vue.watch)([down, () => props.value], (_, __, onCleanup) => {
      let intervalId;
      const tick = () => {
        var _a;
        if (!update()) {
          window.clearInterval(intervalId);
        } else {
          (_a = statisticRef.value) === null || _a === void 0 ? void 0 : _a.$forceUpdate();
        }
      };
      const startTimer = () => {
        intervalId = window.setInterval(tick, UPDATE_INTERVAL);
      };
      const stopTimer = () => {
        window.clearInterval(intervalId);
      };
      startTimer();
      onCleanup(stopTimer);
    }, {
      flush: 'post',
      immediate: true
    });
    // ======================== Format ========================
    const formatter = ({
      value,
      config
    }) => (0, _utils.formatCounter)(value, (0, _extends2.default)((0, _extends2.default)({}, config), {
      format: props.format
    }), down.value);
    const valueRender = node => (0, _vue.cloneVNode)(node, {
      title: undefined
    });
    return () => {
      return (0, _vue.createVNode)(_Statistic.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({
        "ref": statisticRef
      }, (0, _lodash.omit)(props, ['onChange', 'onFinish', 'type', 'format'])), {}, {
        "formatter": formatter,
        "valueRender": valueRender
      }), slots);
    };
  }
});