'use strict';

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

var vue = require('vue');
require('../../../utils/index.js');
var countdown = require('./countdown.js');
var createNamespace = require('../../../utils/create-namespace.js');

"use strict";
const _hoisted_1 = {
  key: 0,
  viewBox: "0 0 24 24",
  fill: "currentColor"
};
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{ name: "LlCountdown" },
  __name: "countdown",
  props: countdown.countdownProps,
  emits: countdown.countdownEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const bem = createNamespace.createNamespace("countdown");
    const UNIT_LABELS = {
      yyyy: "\u5E74",
      MM: "\u6708",
      dd: "\u65E5",
      HH: "\u65F6",
      mm: "\u5206",
      ss: "\u79D2"
    };
    const ALL_UNITS = ["yyyy", "MM", "dd", "HH", "mm", "ss"];
    const UNIT_SECONDS = {
      yyyy: 365 * 24 * 60 * 60,
      MM: 30 * 24 * 60 * 60,
      dd: 24 * 60 * 60,
      HH: 60 * 60,
      mm: 60,
      ss: 1
    };
    const targetTime = vue.ref(0);
    const remainingMs = vue.ref(0);
    const isFinish = vue.ref(false);
    const isRunning = vue.ref(false);
    let timer = null;
    let finishTimer = null;
    const confettiContainer = vue.ref(null);
    const resolveTargetTime = () => {
      if (props.mode === "timer") {
        const v = props.value instanceof Date ? props.value : new Date(props.value);
        return v.getTime();
      }
      return Date.now() + Number(props.value);
    };
    const init = () => {
      if (finishTimer !== null) {
        clearTimeout(finishTimer);
        finishTimer = null;
      }
      targetTime.value = resolveTargetTime();
      remainingMs.value = Math.max(0, targetTime.value - Date.now());
      isFinish.value = false;
    };
    const tick = () => {
      const delta = targetTime.value - Date.now();
      if (delta <= 0) {
        remainingMs.value = 0;
        stopTimer();
        isFinish.value = true;
        if (finishTimer !== null) {
          clearTimeout(finishTimer);
        }
        finishTimer = setTimeout(() => {
          isFinish.value = false;
          finishTimer = null;
        }, 600);
        if (props.showConfetti) {
          createConfetti();
        }
        emits("finish");
      } else {
        remainingMs.value = delta;
      }
    };
    const stopTimer = () => {
      if (timer !== null) {
        clearInterval(timer);
        timer = null;
      }
      isRunning.value = false;
    };
    const createConfetti = () => {
      if (!confettiContainer.value) return;
      const container = confettiContainer.value;
      const { width, height } = container.getBoundingClientRect();
      if (width <= 0 || height <= 0) return;
      const confettiCount = 50;
      const colors = [
        "#b24a2e",
        "#d3a437",
        "#4d956f",
        "#537bd0",
        "#9a4faf",
        "#5fa0aa"
      ];
      for (let i = 0; i < confettiCount; i++) {
        const confetti = document.createElement("div");
        const size = Math.random() * 10 + 4;
        const duration = 2.8 + Math.random() * 0.4;
        const delay = Math.random() * 0.2;
        const centerX = width / 2;
        const centerY = height / 2;
        const angle = Math.random() * Math.PI * 2;
        const distance = Math.min(width, height) * (0.2 + Math.random() * 0.45);
        const rotation = Math.random() * 360;
        const rotateMid = rotation + (Math.random() > 0.5 ? 1 : -1) * (80 + Math.random() * 140);
        const rotateEnd = rotateMid + (Math.random() > 0.5 ? 1 : -1) * (180 + Math.random() * 240);
        const color = colors[Math.floor(Math.random() * colors.length)];
        const isStrip = Math.random() > 0.45;
        const pieceWidth = isStrip ? size * 1.6 : size;
        const pieceHeight = isStrip ? Math.max(3, size * 0.45) : size;
        const opacity = 0.88 + Math.random() * 0.1;
        const fromLeft = Math.random() > 0.5;
        const launchX = width * (fromLeft ? 0.18 : 0.82) + (Math.random() - 0.5) * width * 0.08;
        const launchY = height * 0.88 + (Math.random() - 0.5) * height * 0.06;
        const centerTargetX = centerX - pieceWidth / 2 + (Math.random() - 0.5) * width * 0.08;
        const centerTargetY = centerY - pieceHeight / 2 + (Math.random() - 0.5) * height * 0.08;
        const toCenterX = centerTargetX - launchX;
        const toCenterY = centerTargetY - launchY;
        const explodeX = Math.cos(angle) * distance;
        const explodeY = Math.sin(angle) * distance;
        const finalX = toCenterX + explodeX;
        const finalY = toCenterY + explodeY;
        confetti.style.cssText = `
      position: absolute;
      left: ${launchX}px;
      top: ${launchY}px;
      width: ${pieceWidth}px;
      height: ${pieceHeight}px;
      background-color: ${color};
      border-radius: ${isStrip ? pieceHeight / 2 : 50}%;
      pointer-events: none;
      --ll-confetti-center-x: ${toCenterX}px;
      --ll-confetti-center-y: ${toCenterY}px;
      --ll-confetti-final-x: ${finalX}px;
      --ll-confetti-final-y: ${finalY}px;
      --ll-confetti-rotate-start: ${rotation}deg;
      --ll-confetti-rotate-mid: ${rotateMid}deg;
      --ll-confetti-rotate-end: ${rotateEnd}deg;
      animation: confetti-fall ${duration}s linear ${delay}s forwards;
      transform: rotate(${rotation}deg);
      opacity: ${opacity};
      box-shadow: 0 0 ${Math.max(4, size / 1.9)}px ${color};
    `;
        container.appendChild(confetti);
        setTimeout(
          () => {
            confetti.remove();
          },
          (duration + delay) * 1e3
        );
      }
    };
    const startTimer = () => {
      stopTimer();
      tick();
      isRunning.value = true;
      const interval = props.precision > 0 ? Math.pow(10, 3 - props.precision) : 1e3;
      timer = setInterval(tick, interval);
    };
    const reset = () => {
      stopTimer();
      init();
    };
    const togglePlayPause = () => {
      if (isRunning.value) {
        pause();
      } else {
        start();
      }
    };
    const handleReset = () => {
      reset();
    };
    vue.watch(
      () => props.active,
      (val) => {
        if (val) {
          if (props.mode === "timer") {
            targetTime.value = resolveTargetTime();
            remainingMs.value = Math.max(0, targetTime.value - Date.now());
          } else {
            targetTime.value = Date.now() + remainingMs.value;
          }
          startTimer();
        } else {
          stopTimer();
        }
      }
    );
    vue.watch(
      () => props.value,
      () => {
        stopTimer();
        init();
      }
    );
    vue.onMounted(() => {
      init();
      if (props.active) {
        startTimer();
      }
    });
    vue.onUnmounted(() => {
      stopTimer();
      if (finishTimer !== null) {
        clearTimeout(finishTimer);
      }
    });
    const displayItems = vue.computed(() => {
      const selected = new Set(props.units);
      const totalMs = remainingMs.value;
      const totalSeconds = Math.floor(totalMs / 1e3);
      const subMs = totalMs - totalSeconds * 1e3;
      let remaining = totalSeconds;
      const result = [];
      for (let i = 0; i < ALL_UNITS.length; i++) {
        const unit = ALL_UNITS[i];
        if (!selected.has(unit)) {
          continue;
        }
        const factor = UNIT_SECONDS[unit];
        const val = Math.floor(remaining / factor);
        remaining = remaining % factor;
        if (unit === "ss" && props.precision > 0) {
          const decimal = subMs / 1e3;
          const intPart = String(val).padStart(2, "0");
          const decPart = decimal.toFixed(props.precision).slice(1);
          result.push({
            key: unit,
            value: intPart + decPart,
            label: UNIT_LABELS[unit]
          });
        } else {
          const digits = 2;
          result.push({
            key: unit,
            value: String(val).padStart(digits, "0"),
            label: UNIT_LABELS[unit]
          });
        }
      }
      return result;
    });
    const start = () => {
      if (props.mode === "timer") {
        targetTime.value = resolveTargetTime();
        remainingMs.value = Math.max(0, targetTime.value - Date.now());
      } else {
        targetTime.value = Date.now() + remainingMs.value;
      }
      startTimer();
    };
    const pause = () => {
      stopTimer();
    };
    __expose({ reset, start, pause, isRunning });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock(
        "div",
        {
          class: vue.normalizeClass([vue.unref(bem).b(), vue.unref(bem).m(props.theme), isFinish.value ? vue.unref(bem).is("finish") : ""])
        },
        [
          vue.createCommentVNode(" \u793C\u82B1\u5BB9\u5668 "),
          vue.createElementVNode(
            "div",
            {
              ref_key: "confettiContainer",
              ref: confettiContainer,
              class: vue.normalizeClass(vue.unref(bem).e("confetti-container"))
            },
            null,
            2
            /* CLASS */
          ),
          !!props.title ? (vue.openBlock(), vue.createElementBlock(
            "div",
            {
              key: 0,
              class: vue.normalizeClass(vue.unref(bem).e("title")),
              style: vue.normalizeStyle(props.titleStyle)
            },
            vue.toDisplayString(props.title),
            7
            /* TEXT, CLASS, STYLE */
          )) : vue.createCommentVNode("v-if", true),
          vue.createElementVNode(
            "div",
            {
              class: vue.normalizeClass(vue.unref(bem).e("content"))
            },
            [
              (vue.openBlock(true), vue.createElementBlock(
                vue.Fragment,
                null,
                vue.renderList(displayItems.value, (item, index) => {
                  return vue.openBlock(), vue.createElementBlock(
                    vue.Fragment,
                    {
                      key: item.key
                    },
                    [
                      vue.createElementVNode(
                        "div",
                        {
                          class: vue.normalizeClass(vue.unref(bem).e("item"))
                        },
                        [
                          vue.createElementVNode(
                            "span",
                            {
                              class: vue.normalizeClass(vue.unref(bem).e("value"))
                            },
                            vue.toDisplayString(item.value),
                            3
                            /* TEXT, CLASS */
                          ),
                          vue.createElementVNode(
                            "span",
                            {
                              class: vue.normalizeClass(vue.unref(bem).e("label"))
                            },
                            vue.toDisplayString(item.label),
                            3
                            /* TEXT, CLASS */
                          )
                        ],
                        2
                        /* CLASS */
                      ),
                      index < displayItems.value.length - 1 ? (vue.openBlock(), vue.createElementBlock(
                        "span",
                        {
                          key: 0,
                          class: vue.normalizeClass(vue.unref(bem).e("separator"))
                        },
                        " : ",
                        2
                        /* CLASS */
                      )) : vue.createCommentVNode("v-if", true)
                    ],
                    64
                    /* STABLE_FRAGMENT */
                  );
                }),
                128
                /* KEYED_FRAGMENT */
              ))
            ],
            2
            /* CLASS */
          ),
          vue.createCommentVNode(" \u63A7\u5236\u6309\u94AE "),
          props.control ? (vue.openBlock(), vue.createElementBlock(
            "div",
            {
              key: 1,
              class: vue.normalizeClass(vue.unref(bem).e("controls"))
            },
            [
              vue.createElementVNode(
                "button",
                {
                  class: vue.normalizeClass([vue.unref(bem).e("btn"), isRunning.value ? vue.unref(bem).em("btn", "pause") : vue.unref(bem).em("btn", "play")]),
                  onClick: togglePlayPause
                },
                [
                  vue.createCommentVNode(" \u64AD\u653E\u56FE\u6807 "),
                  !isRunning.value ? (vue.openBlock(), vue.createElementBlock("svg", _hoisted_1, [..._cache[0] || (_cache[0] = [
                    vue.createElementVNode(
                      "path",
                      { d: "M8 5v14l11-7z" },
                      null,
                      -1
                      /* CACHED */
                    )
                  ])])) : (vue.openBlock(), vue.createElementBlock(
                    vue.Fragment,
                    { key: 1 },
                    [
                      vue.createCommentVNode(" \u6682\u505C\u56FE\u6807 "),
                      _cache[1] || (_cache[1] = vue.createElementVNode(
                        "svg",
                        {
                          viewBox: "0 0 24 24",
                          fill: "currentColor"
                        },
                        [
                          vue.createElementVNode("path", { d: "M6 19h4V5H6v14zm8-14v14h4V5h-4z" })
                        ],
                        -1
                        /* CACHED */
                      ))
                    ],
                    2112
                    /* STABLE_FRAGMENT, DEV_ROOT_FRAGMENT */
                  ))
                ],
                2
                /* CLASS */
              ),
              vue.createElementVNode(
                "button",
                {
                  class: vue.normalizeClass([vue.unref(bem).e("btn"), vue.unref(bem).em("btn", "reset")]),
                  onClick: handleReset
                },
                [
                  vue.createCommentVNode(" \u91CD\u7F6E\u56FE\u6807 "),
                  _cache[2] || (_cache[2] = vue.createElementVNode(
                    "svg",
                    {
                      viewBox: "0 0 24 24",
                      fill: "currentColor"
                    },
                    [
                      vue.createElementVNode("path", { d: "M12 5V1L7 6l5 5V7c3.31 0 6 2.69 6 6s-2.69 6-6 6-6-2.69-6-6H4c0 4.42 3.58 8 8 8s8-3.58 8-8-3.58-8-8-8z" })
                    ],
                    -1
                    /* CACHED */
                  ))
                ],
                2
                /* CLASS */
              )
            ],
            2
            /* CLASS */
          )) : vue.createCommentVNode("v-if", true)
        ],
        2
        /* CLASS */
      );
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=countdown.vue2.js.map
