"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const event = require("../../../constants/event.js");
const raf = require("../../../utils/raf.js");
const countdown = require("./countdown.js");
const index = require("../../statistic/index.js");
const utils = require("./utils.js");
const Vue = require("vue");
var countdown_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ Vue.defineComponent({
  name: "ElCountdown",
  __name: "countdown",
  props: countdown.countdownProps,
  emits: countdown.countdownEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    let timer;
    const rawValue = Vue.ref(0);
    const displayValue = Vue.computed(() => utils.formatTime(rawValue.value, props.format));
    const formatter = (val) => utils.formatTime(val, props.format);
    const stopTimer = () => {
      if (timer) {
        raf.cAF(timer);
        timer = void 0;
      }
    };
    const startTimer = () => {
      const timestamp = utils.getTime(props.value);
      const frameFunc = () => {
        let diff = timestamp - Date.now();
        emit(event.CHANGE_EVENT, diff);
        if (diff <= 0) {
          diff = 0;
          stopTimer();
          emit("finish");
        } else timer = raf.rAF(frameFunc);
        rawValue.value = diff;
      };
      timer = raf.rAF(frameFunc);
    };
    Vue.onMounted(() => {
      rawValue.value = utils.getTime(props.value) - Date.now();
      Vue.watch(() => [props.value, props.format], () => {
        stopTimer();
        startTimer();
      }, { immediate: true });
    });
    Vue.onBeforeUnmount(() => {
      stopTimer();
    });
    __expose({ displayValue });
    return (_ctx, _cache) => {
      return Vue.openBlock(), Vue.createBlock(Vue.unref(index.ElStatistic), {
        value: rawValue.value,
        title: __props.title,
        prefix: __props.prefix,
        suffix: __props.suffix,
        "value-style": __props.valueStyle,
        formatter
      }, Vue.createSlots({ _: 2 }, [Vue.renderList(_ctx.$slots, (_, name) => {
        return {
          name,
          fn: Vue.withCtx(() => [Vue.renderSlot(_ctx.$slots, name)])
        };
      })]), 1032, [
        "value",
        "title",
        "prefix",
        "suffix",
        "value-style"
      ]);
    };
  }
});
exports.default = countdown_vue_vue_type_script_setup_true_lang_default;
