import { defineComponent as T, ref as D, computed as k, watchEffect as z, openBlock as d, createElementBlock as w, Fragment as F, normalizeStyle as S, toDisplayString as m, createCommentVNode as O } from "vue";
import { autoInstallFont as E, jsonToCssStyle as A } from "../../utils/json2css.mjs";
const j = ["ztitle"], _ = ["ztitle"], $ = ["ztitle"], N = T({ name: "zv-text-count-down" }), I = /* @__PURE__ */ T({
  ...N,
  props: {
    basicOption: {},
    useEvents: {}
  },
  emits: [
    "on-end",
    "on-start"
  ],
  setup(H, { expose: C, emit: Y }) {
    const g = Y, x = window.requestAnimationFrame, n = H, p = D(0), o = D(0), a = k(() => ({
      showMillisecond: n.basicOption.conf.format.includes("SSS"),
      showYear: n.basicOption.conf.format.includes("Y"),
      showMonth: n.basicOption.conf.format.includes("M"),
      showDay: n.basicOption.conf.format.includes("D"),
      showHour: n.basicOption.conf.format.includes("H"),
      showMinute: n.basicOption.conf.format.includes("m"),
      showSecond: n.basicOption.conf.format.includes("s")
    }));
    function l(e) {
      return e < 10 ? "0" + e : String(e);
    }
    function y(e) {
      if (e === null)
        return "--";
      let t = n.basicOption.conf.format, i = 0;
      a.value.showMillisecond && (i = e % 1e3, t = t.replace("SSS", "0".repeat(3 - String(i).length) + i)), e = Math.floor(e / 1e3);
      let s = 0;
      a.value.showYear ? (s = Math.floor(e / (60 * 60 * 24 * 30 * 12)), t = t.includes("YY") ? t.replace("YY", l(s)) : t.replace("Y", String(s))) : s = 0;
      let r = 0;
      a.value.showMonth ? (e = e - s * 60 * 60 * 24 * 30 * 12, r = Math.floor(e / (60 * 60 * 24 * 30)), t = t.includes("MM") ? t.replace("MM", l(r)) : t.replace("M", String(r))) : r = 0;
      let c = 0;
      a.value.showDay ? (e = e - r * 60 * 60 * 24 * 30, c = Math.floor(e / (60 * 60 * 24)), t = t.includes("DD") ? t.replace("DD", l(c)) : t.replace("D", String(c))) : c = 0;
      let u = 0;
      a.value.showHour ? (e = e - c * 60 * 60 * 24, u = Math.floor(e / (60 * 60)), t = t.includes("HH") ? t.replace("HH", l(u)) : t.replace("H", String(u))) : u = 0;
      let f = 0;
      a.value.showMinute ? (e = e - u * 60 * 60, f = Math.floor(e / 60), t = t.includes("mm") ? t.replace("mm", l(f)) : t.replace("m", String(f))) : f = 0;
      let b = 0;
      return a.value.showSecond && (b = e - f * 60, t = t.includes("ss") ? t.replace("ss", l(b)) : t.replace("s", String(b))), t;
    }
    function M() {
      o.value !== null && (p.value > Date.now() ? (o.value = p.value - Date.now(), x(M)) : (o.value = 0, g("on-end"), window.SHJParseEvent(n.useEvents, "end-count-down", null)));
    }
    const h = () => {
      g("on-start"), window.SHJParseEvent(n.useEvents, "on-start", null), o.value = 0, Number.isFinite(n.basicOption.conf.value) ? (n.basicOption.conf.future ? n.basicOption.conf.value >= Date.now() && (p.value = n.basicOption.conf.value) : n.basicOption.conf.value >= 0 && (p.value = n.basicOption.conf.value + Date.now()), x(M)) : o.value = null;
    };
    z(() => {
      n.basicOption.conf.autoStart ? h() : o.value = null;
    });
    const v = (e) => {
      E(e.fontFamily);
      const t = {};
      e.color && e.color.includes("linear-gradient") && (t.backgroundImage = e.color, t["-webkit-background-clip"] = "text", t.color = "transparent");
      const i = {};
      return e.textAlign === "left" && (e.justifyContent = "flex-start"), e.textAlign === "center" && (e.justifyContent = "center"), e.textAlign === "right" && (e.justifyContent = "flex-end"), {
        ...A(e),
        ...t,
        ...i,
        ...e.textShadow.show ? { "--text-shadow": `${e.textShadow.x}px ${e.textShadow.y}px ${e.textShadow.value}px ${e.textShadow.color}` } : {}
      };
    };
    return C({
      // 开始倒计时
      start: () => h(),
      // 重新倒计时
      startAgain: () => h()
    }), (e, t) => (d(), w(F, null, [
      e.basicOption.conf.finishedText && o.value === 0 && o.value !== null ? (d(), w("span", {
        key: 0,
        class: "count-down",
        style: S(v(e.basicOption.css)),
        ztitle: e.basicOption.conf.finishedText
      }, m(e.basicOption.conf.finishedText), 13, j)) : O("", !0),
      e.basicOption.conf.defaultText && o.value === null ? (d(), w("span", {
        key: 1,
        class: "count-down",
        style: S(v(e.basicOption.css)),
        ztitle: e.basicOption.conf.defaultText
      }, m(e.basicOption.conf.defaultText), 13, _)) : O("", !0),
      Number.isFinite(o.value) && o.value > 0 ? (d(), w("span", {
        key: 2,
        class: "count-down",
        style: S(v(e.basicOption.css)),
        ztitle: y(o.value)
      }, m(y(o.value)), 13, $)) : O("", !0)
    ], 64));
  }
});
export {
  I as default
};
