"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const comp = require("../../ui/src/comp.js");
require("../../ui/index.js");
const vn = require("../../ui/src/vn.js");
const text = require("../../text/src/text.js");
const XEUtils = require("xe-utils");
const config = require("../../../../@vxe-ui/core/es/src/config.js");
const useFns = require("../../../../@vxe-ui/core/es/src/useFns.js");
const event = require("../../../../@vxe-ui/core/es/src/event.js");
const index_esm = require("../../../../@vxe-ui/core/es/index.esm.js");
const i18n = require("../../../../@vxe-ui/core/es/src/i18n.js");
const VxeCountdownComponent = comp.defineVxeComponent({
  name: "VxeCountdown",
  props: {
    modelValue: [Number, String],
    format: String,
    prefixConfig: Object,
    suffixConfig: Object,
    size: {
      type: String,
      default: () => config.getConfig().countdown.size || config.getConfig().size
    }
  },
  emits: [
    "update:modelValue",
    "start",
    "end"
  ],
  setup(props, context) {
    const { slots, emit } = context;
    const xID = XEUtils.uniqueId();
    const refElem = vue.ref();
    const { computeSize } = useFns.useSize(props);
    const reactData = vue.reactive({
      currNum: 0,
      secondNum: 0
    });
    const internalData = {
      dnTimeout: void 0
    };
    const refMaps = {
      refElem
    };
    const computeTimeFormats = vue.computed(() => {
      const { secondNum } = reactData;
      if (secondNum >= 316224e5) {
        return ["yyyy", "MM", "dd", "HH", "mm", "ss"];
      }
      if (secondNum >= 26784e5) {
        return ["MM", "dd", "HH", "mm", "ss"];
      }
      if (secondNum >= 864e5) {
        return ["dd", "HH", "mm", "ss"];
      }
      if (secondNum >= 36e5) {
        return ["HH", "mm", "ss"];
      }
      if (secondNum >= 6e4) {
        return ["mm", "ss"];
      }
      return ["ss"];
    });
    const computeDiffConf = vue.computed(() => {
      const { currNum } = reactData;
      const diffRest = XEUtils.getDateDiff(Date.now(), Date.now() + currNum);
      return Object.assign(diffRest, { done: !(XEUtils.isBoolean(diffRest.status) ? diffRest.status : diffRest.done) });
    });
    const computeFormatLabel = vue.computed(() => {
      const { format } = props;
      const diffConf = computeDiffConf.value;
      let rest = "";
      if (format) {
        rest = `${format}`;
        XEUtils.each(diffConf, (val, key) => {
          rest = rest.replace(new RegExp(key, "g"), XEUtils.padStart(val, key.length, "0"));
        });
        return rest;
      }
      return rest;
    });
    const computePrefixOpts = vue.computed(() => {
      return Object.assign({}, props.prefixConfig, config.getConfig().countdown.prefixConfig);
    });
    const computeSuffixOpts = vue.computed(() => {
      return Object.assign({}, props.suffixConfig, config.getConfig().countdown.suffixConfig);
    });
    const computeMaps = {
      computeSize
    };
    const $xeCountdown = {
      xID,
      props,
      context,
      reactData,
      internalData,
      getRefMaps: () => refMaps,
      getComputeMaps: () => computeMaps
    };
    const dispatchEvent = (type, params, evnt) => {
      emit(type, event.createEvent(evnt, { $carousel: $xeCountdown }, params));
    };
    const updateCount = () => {
      const secondNum = XEUtils.toNumber(props.modelValue || 0);
      reactData.secondNum = secondNum;
      reactData.currNum = secondNum;
    };
    const handleTime = () => {
      const { currNum } = reactData;
      if (currNum > 1e3) {
        reactData.currNum -= 1e3;
        internalData.dnTimeout = setTimeout(() => {
          handleTime();
        }, 1e3);
      } else {
        reactData.currNum = 0;
        handleStop();
      }
    };
    const countdownMethods = {
      dispatchEvent
    };
    const handleStart = () => {
      dispatchEvent("start", {}, null);
      handleTime();
    };
    const handleStop = () => {
      const { dnTimeout } = internalData;
      if (dnTimeout) {
        clearTimeout(dnTimeout);
        internalData.dnTimeout = void 0;
        dispatchEvent("end", {}, null);
      }
    };
    const countdownPrivateMethods = {};
    Object.assign($xeCountdown, countdownMethods, countdownPrivateMethods);
    const renderDefaultContentVNs = () => {
      const { format } = props;
      const timeFormats = computeTimeFormats.value;
      const diffConf = computeDiffConf.value;
      const formatLabel = computeFormatLabel.value;
      if (format) {
        return [
          vue.h("div", {
            key: "format",
            class: "vxe-countdown--content-format"
          }, formatLabel)
        ];
      }
      return timeFormats.map((key, index) => {
        return vue.h("div", {
          key: index,
          class: "vxe-countdown--content-item"
        }, [
          vue.h("div", {
            class: "vxe-countdown--content-num"
          }, `${diffConf[key] || 0}`),
          vue.h("div", {
            class: "vxe-countdown--content-unit"
          }, i18n.getI18n(`vxe.countdown.formats.${key}`))
        ]);
      });
    };
    const renderVN = () => {
      const { prefixConfig, suffixConfig } = props;
      const { currNum } = reactData;
      const vSize = computeSize.value;
      const diffConf = computeDiffConf.value;
      const prefixOpts = computePrefixOpts.value;
      const suffixOpts = computeSuffixOpts.value;
      const prefixSlot = slots.prefix;
      const suffixSlot = slots.suffix;
      const defaultSlot = slots.default;
      return vue.h("div", {
        ref: refElem,
        class: ["vxe-countdown", diffConf.done ? "is--progress" : "is-end", {
          [`size--${vSize}`]: vSize
        }]
      }, [
        prefixSlot || prefixConfig ? vue.h("div", {
          class: "vxe-countdown--prefix"
        }, prefixSlot ? vn.getSlotVNs(prefixSlot({ currentValue: currNum, diffConf })) : [
          vue.h(text.default, {
            content: prefixOpts.content,
            icon: prefixOpts.icon,
            status: prefixOpts.status
          })
        ]) : index_esm.renderEmptyElement(),
        vue.h("div", {
          class: "vxe-countdown--content"
        }, defaultSlot ? vn.getSlotVNs(defaultSlot({ currentValue: currNum, diffConf })) : renderDefaultContentVNs()),
        suffixSlot || suffixConfig ? vue.h("div", {
          class: "vxe-countdown--suffix"
        }, suffixSlot ? vn.getSlotVNs(suffixSlot({ currentValue: currNum, diffConf })) : [
          vue.h(text.default, {
            content: suffixOpts.content,
            icon: suffixOpts.icon,
            status: suffixOpts.status
          })
        ]) : index_esm.renderEmptyElement()
      ]);
    };
    vue.watch(() => props.modelValue, () => {
      updateCount();
      handleStop();
      handleStart();
    });
    vue.onUnmounted(() => {
      handleStop();
    });
    vue.onMounted(() => {
      handleStart();
    });
    updateCount();
    $xeCountdown.renderVN = renderVN;
    return $xeCountdown;
  },
  render() {
    return this.renderVN();
  }
});
exports.default = VxeCountdownComponent;
