'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.cjs');
var Statistic = require('./Statistic.cjs');
var utils = require('./utils.cjs');
var props = require('./props.cjs');
var initDefaultProps = require('../../../utils/vue/initDefaultProps.cjs');
var omit = require('../../../utils/lang/omit.cjs');

const REFRESH_INTERVAL = 1e3 / 30;
function getTime(value) {
  return new Date(value).getTime();
}
var ACountdown = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AStatisticCountdown",
  props: initDefaultProps.initDefaultProps(props.countdownProps(), {
    format: "HH:mm:ss"
  }),
  // emits: ['finish', 'change'],
  setup(props, {
    emit,
    slots
  }) {
    const countdownId = vue.ref();
    const statistic = vue.ref();
    const startTimer = () => {
      if (countdownId.value)
        return;
      const timestamp = getTime(props.value);
      countdownId.value = setInterval(() => {
        statistic.value.$forceUpdate();
        if (timestamp > Date.now())
          emit("change", timestamp - Date.now());
        syncTimer();
      }, REFRESH_INTERVAL);
    };
    const stopTimer = () => {
      const {
        value
      } = props;
      if (countdownId.value) {
        clearInterval(countdownId.value);
        countdownId.value = void 0;
        const timestamp = getTime(value);
        if (timestamp < Date.now())
          emit("finish");
      }
    };
    const syncTimer = () => {
      const {
        value
      } = props;
      const timestamp = getTime(value);
      if (timestamp >= Date.now())
        startTimer();
      else
        stopTimer();
    };
    const formatCountdown = ({
      value,
      config
    }) => {
      const {
        format
      } = props;
      return utils.formatCountdown(value, {
        ...config,
        format
      });
    };
    const valueRenderHtml = (node) => node;
    vue.onMounted(() => {
      syncTimer();
    });
    vue.onUpdated(() => {
      syncTimer();
    });
    vue.onBeforeUnmount(() => {
      stopTimer();
    });
    return () => {
      const value = props.value;
      return vue.createVNode(Statistic.default, {
        "ref": statistic,
        ...omit.omit(props, ["onFinish", "onChange"]),
        value,
        valueRender: valueRenderHtml,
        formatter: formatCountdown
      }, slots);
    };
  }
});

exports.default = ACountdown;
//# sourceMappingURL=Countdown.cjs.map
