import { defineComponent, ref, onMounted, onUpdated, onBeforeUnmount, createVNode } from 'vue';
import '../../../utils/index.mjs';
import AStatistic from './Statistic.mjs';
import { formatCountdown } from './utils.mjs';
import { countdownProps } from './props.mjs';
import { initDefaultProps } from '../../../utils/vue/initDefaultProps.mjs';
import { omit } from '../../../utils/lang/omit.mjs';

const REFRESH_INTERVAL = 1e3 / 30;
function getTime(value) {
  return new Date(value).getTime();
}
var ACountdown = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AStatisticCountdown",
  props: initDefaultProps(countdownProps(), {
    format: "HH:mm:ss"
  }),
  // emits: ['finish', 'change'],
  setup(props, {
    emit,
    slots
  }) {
    const countdownId = ref();
    const statistic = 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$1 = ({
      value,
      config
    }) => {
      const {
        format
      } = props;
      return formatCountdown(value, {
        ...config,
        format
      });
    };
    const valueRenderHtml = (node) => node;
    onMounted(() => {
      syncTimer();
    });
    onUpdated(() => {
      syncTimer();
    });
    onBeforeUnmount(() => {
      stopTimer();
    });
    return () => {
      const value = props.value;
      return createVNode(AStatistic, {
        "ref": statistic,
        ...omit(props, ["onFinish", "onChange"]),
        value,
        valueRender: valueRenderHtml,
        formatter: formatCountdown$1
      }, slots);
    };
  }
});

export { ACountdown as default };
//# sourceMappingURL=Countdown.mjs.map
