import { ref as d, onMounted as q, onBeforeUnmount as J, computed as D, openBlock as r, createElementBlock as u, normalizeStyle as f, createVNode as _, createCommentVNode as p, unref as K, createElementVNode as M, mergeProps as E, renderSlot as N, normalizeProps as j, guardReactiveProps as B, toDisplayString as Q } from "vue";
import { u as Y } from "./useNestedProp-BbEbJmgv.js";
import { u as Z, c as ee, X as te, D as O } from "./index-CguVaFlt.js";
import { t as ae, u as se } from "./useResponsive-NZB-WLRF.js";
import x from "./BaseIcon-BNFf4B_v.js";
import { _ as le } from "./Title-DbTGPg4G.js";
import { _ as oe } from "./_plugin-vue_export-helper-CHgC5LLL.js";
class re {
  constructor(F, w, I, z = !0, C = !0) {
    this.interval = w, this.elapsed = 0, this.isPaused = !1;
    const $ = `
            let interval;
            let elapsed = 0;
            let paused = false;
            let startTime;
            let tickInterval;

            onmessage = function(e) {
                const { action, data } = e.data;

                switch(action) {
                    case 'start':
                        startTime = Date.now();
                        tickInterval = data.interval;
                        elapsed = 0;
                        paused = false;
                        interval = setInterval(() => {
                            elapsed += tickInterval;
                            postMessage({ elapsed, timestamp: Date.now() });
                        }, tickInterval);
                        break;
                    
                    case 'pause':
                        paused = true;
                        clearInterval(interval);
                        elapsed = Date.now() - startTime;
                        break;

                    case 'resume':
                        if (paused) {
                            startTime = Date.now() - elapsed;
                            interval = setInterval(() => {
                                elapsed += tickInterval;
                                postMessage({ elapsed, timestamp: Date.now() });
                            }, tickInterval);
                        }
                        paused = false;
                        break;

                    case 'stop':
                        clearInterval(interval);
                        elapsed = 0;
                        postMessage({ elapsed });
                        break;

                    case 'reset':
                        elapsed = 0;
                        clearInterval(interval);
                        postMessage({ elapsed });
                        break;

                    case 'lap':
                        postMessage({
                            elapsed,
                            timestamp: Date.now(),
                            action: 'lap'
                        });
                        break;

                    default:
                        break;
                }
            };
        `, P = new Blob([$], { type: "application/javascript" }), m = URL.createObjectURL(P), n = new Worker(m);
    function e(h) {
      let t = Math.floor(h / 1e3), i = Math.floor(h % 1e3 / 10), v = Math.floor(t / 3600), k = Math.floor(t % 3600 / 60), a = t % 60, l = "";
      return C && (l += String(v).padStart(2, "0") + ":"), l += String(k).padStart(2, "0") + ":", l += String(a).padStart(2, "0"), z && (l += "." + String(i).padStart(2, "0")), l;
    }
    this.start = () => {
      this.isPaused = !1, n.postMessage({ action: "start", data: { interval: this.interval } });
    }, this.pause = () => {
      this.isPaused ? this.resume() : (this.isPaused = !0, n.postMessage({ action: "pause" }));
    }, this.resume = () => {
      this.isPaused && (n.postMessage({ action: "resume" }), this.isPaused = !1);
    }, this.stop = () => {
      n.postMessage({ action: "stop" }), this.isPaused = !1;
    }, this.reset = () => {
      n.postMessage({ action: "reset" }), this.elapsed = 0, this.isPaused = !1;
    }, this.restart = () => {
      this.stop(), this.start();
    }, this.lap = () => new Promise((h) => {
      n.postMessage({ action: "lap" });
      const t = (i) => {
        const { elapsed: v, timestamp: k, action: a } = i.data;
        if (a === "lap") {
          const l = e(v);
          h({
            timestamp: k || 0,
            elapsed: v,
            formatted: l
          });
        }
      };
      n.addEventListener("message", t, { once: !0 });
    }), n.onmessage = (h) => {
      const { elapsed: t, timestamp: i } = h.data;
      this.elapsed = t, F({
        timestamp: i || 0,
        elapsed: this.elapsed,
        formatted: e(this.elapsed)
      });
    }, n.onerror = (h) => {
      I && I(h);
    };
  }
}
const ue = ["xmlns", "viewBox"], ie = { key: 0 }, ne = ["id"], ce = ["stop-color"], de = ["stop-color"], he = ["cx", "cy", "r", "fill", "stroke", "stroke-width"], ve = ["d", "stroke", "stroke-width"], pe = ["r", "fill", "stroke", "stroke-width"], fe = ["r", "fill", "stroke", "stroke-width"], we = ["x", "y"], ke = ["x", "y", "font-size", "fill", "font-weight"], ge = {
  key: 0,
  class: "vue-ui-timer-controls"
}, me = ["title"], ye = ["title"], be = ["title"], _e = ["title"], Me = ["title"], Ce = {
  __name: "vue-ui-timer",
  props: {
    config: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  emits: ["start", "pause", "reset", "restart", "lap"],
  setup(U, { expose: F, emit: w }) {
    const I = U, { vue_ui_timer: z } = Z(), C = d(null), $ = d(null), P = d(null), m = d(null), n = d(ee());
    q(() => {
      if (e.value.responsive) {
        const s = ae(() => {
          const { width: o, height: c } = se({
            chart: C.value,
            title: e.value.style.title.text ? $.value : null,
            legend: P.value
          });
          t.value.width = o, t.value.height = c, t.value.tracker.core = O({
            relator: Math.min(o, c),
            adjuster: e.value.style.width,
            source: 6 * e.value.stopwatch.tracker.radiusRatio,
            threshold: 1,
            fallback: 1
          }), t.value.tracker.aura = O({
            relator: Math.min(o, c),
            adjuster: e.value.style.width,
            source: 12 * e.value.stopwatch.tracker.aura.radiusRatio,
            threshold: 1,
            fallback: 1
          }), t.value.label = O({
            relator: Math.min(o, c),
            adjuster: e.value.style.width,
            source: e.value.stopwatch.label.fontSize,
            threshold: 10,
            fallback: 10
          });
        });
        m.value = new ResizeObserver(s), m.value.observe(C.value.parentNode);
      }
    }), J(() => {
      m.value && m.value.disconnect();
    });
    const e = D(() => Y({
      userConfig: I.config,
      defaultConfig: z
    })), h = D(() => {
      if (e.value.stopwatch.showHours && e.value.stopwatch.showHundredth)
        return "00:00:00.00";
      if (e.value.stopwatch.showHours && !e.value.stopwatch.showHundredth)
        return "00:00:00";
      if (!e.value.stopwatch.showHours && e.value.stopwatch.showHundredth)
        return "00:00.00";
      if (!e.value.stopwatch.showHours && !e.value.stopwatch.showHundredth)
        return "00:00";
    }), t = d({
      height: e.value.style.height,
      width: e.value.style.width,
      tracker: {
        core: 6 * e.value.stopwatch.tracker.radiusRatio,
        aura: 12 * e.value.stopwatch.tracker.aura.radiusRatio
      },
      label: e.value.stopwatch.label.fontSize
    }), i = d(0), v = new re(
      (s) => W(s),
      10,
      "",
      e.value.stopwatch.showHundredth,
      e.value.stopwatch.showHours
    ), k = d(!0), a = d(!1), l = d(!1);
    function R() {
      w("start"), k.value && v.start(), k.value = !1, a.value = !0;
    }
    function A() {
      a.value && (w("reset"), v.stop(), y.value = [], k.value = !0, a.value = !1);
    }
    function H() {
      l.value = !l.value, w("pause", i.value), v.pause();
    }
    function L() {
      a.value && (l.value = !1, w("restart"), y.value = [], v.restart());
    }
    const y = d([]);
    async function S() {
      if (!a.value || l.value) return;
      const s = await v.lap();
      s && (y.value.push(s), w("lap", y.value));
    }
    function W({ timestamp: s, elapsed: o, formatted: c }) {
      i.value = { timestamp: s, elapsed: o, formatted: c };
    }
    const g = D(() => Math.min(t.value.width, t.value.height) / 2.5 * e.value.stopwatch.track.radiusRatio);
    function G(s, o) {
      const T = 360 / (o * 1e3);
      return s * T % 360;
    }
    function V(s) {
      let o = s * (Math.PI / 180), c = t.value.width / 2 + g.value * Math.cos(o), T = t.value.height / 2 + g.value * Math.sin(o);
      return { cx: c, cy: T };
    }
    const b = D(() => {
      const s = G(i.value.elapsed, e.value.stopwatch.cycleSeconds), { cx: o, cy: c } = V(s - 90), T = s > 180 ? 1 : 0;
      return {
        cx: o || t.value.width / 2,
        cy: c || t.value.height / 2 - g.value,
        largeArcFlag: T,
        sweepFlag: 1
      };
    });
    return F({
      start: R,
      pause: H,
      reset: A,
      restart: L,
      lap: S
    }), (s, o) => (r(), u("div", {
      ref_key: "timerChart",
      ref: C,
      class: "vue-ui-timer",
      style: f({
        fontFamily: e.value.style.fontFamily,
        width: "100%",
        height: e.value.responsive ? "100%" : "auto",
        textAlign: "center"
      })
    }, [
      e.value.style.title.text ? (r(), u("div", {
        key: 0,
        ref_key: "chartTitle",
        ref: $,
        style: f({
          width: "100%",
          background: e.value.style.backgroundColor
        })
      }, [
        _(le, {
          config: {
            title: {
              cy: "title",
              ...e.value.style.title
            },
            subtitle: {
              cy: "subtitle",
              ...e.value.style.title.subtitle
            }
          }
        }, null, 8, ["config"])
      ], 4)) : p("", !0),
      (r(), u("svg", {
        xmlns: K(te),
        viewBox: `0 0 ${t.value.width <= 0 ? 10 : t.value.width} ${t.value.height <= 0 ? 10 : t.value.height}`,
        style: f({
          maxWidth: "100%",
          overflow: "visible",
          background: e.value.style.backgroundColor
        })
      }, [
        e.value.stopwatch.tracker.gradient.show ? (r(), u("defs", ie, [
          M("radialGradient", {
            id: `tracker_gradient_${n.value}`,
            cx: "50%",
            cy: "50%",
            r: "50%",
            fx: "50%",
            fy: "50%"
          }, [
            M("stop", {
              offset: "0%",
              "stop-color": e.value.stopwatch.tracker.gradient.color
            }, null, 8, ce),
            M("stop", {
              offset: "100%",
              "stop-color": e.value.stopwatch.tracker.fill
            }, null, 8, de)
          ], 8, ne)
        ])) : p("", !0),
        M("circle", {
          cx: t.value.width / 2,
          cy: t.value.height / 2,
          r: g.value,
          fill: e.value.stopwatch.track.fill,
          stroke: e.value.stopwatch.track.stroke,
          "stroke-width": e.value.stopwatch.track.strokeWidth
        }, null, 8, he),
        e.value.stopwatch.cycleTrack.show ? (r(), u("path", {
          key: 1,
          d: `M ${t.value.width / 2},${t.value.height / 2 - g.value} A ${g.value},${g.value} 0 ${b.value.largeArcFlag},${b.value.sweepFlag} ${b.value.cx},${b.value.cy}`,
          stroke: e.value.stopwatch.cycleTrack.stroke,
          "stroke-width": e.value.stopwatch.cycleTrack.strokeWidth,
          "stroke-linecap": "round",
          fill: "none"
        }, null, 8, ve)) : p("", !0),
        M("circle", E(b.value, {
          r: t.value.tracker.core,
          fill: e.value.stopwatch.tracker.gradient.show ? `url(#tracker_gradient_${n.value})` : e.value.stopwatch.tracker.fill,
          stroke: e.value.stopwatch.tracker.stroke,
          "stroke-width": e.value.stopwatch.tracker.strokeWidth
        }), null, 16, pe),
        e.value.stopwatch.tracker.aura.show ? (r(), u("circle", E({ key: 2 }, b.value, {
          r: t.value.tracker.aura,
          fill: `${e.value.stopwatch.tracker.aura.fill}20`,
          stroke: e.value.stopwatch.tracker.aura.stroke,
          "stroke-width": e.value.stopwatch.tracker.aura.strokeWidth
        }), null, 16, fe)) : p("", !0),
        s.$slots.time ? (r(), u("foreignObject", {
          key: 3,
          x: t.value.width / 2,
          y: t.value.height / 2,
          height: "0.1",
          width: "0.1",
          style: { overflow: "visible" }
        }, [
          N(s.$slots, "time", j(B({ ...i.value })), void 0, !0)
        ], 8, we)) : (r(), u("text", {
          key: 4,
          x: t.value.width / 2,
          y: t.value.height / 2 + t.value.label / 4,
          "font-size": t.value.label,
          "text-anchor": "middle",
          fill: e.value.stopwatch.label.color,
          "font-weight": e.value.stopwatch.label.bold ? "bold" : "normal",
          style: { "font-variant-numeric": "tabular-nums !important" }
        }, Q(i.value.formatted || h.value), 9, ke))
      ], 12, ue)),
      M("div", {
        ref_key: "chartLegend",
        ref: P,
        style: f({ width: "100%", backgroundColor: e.value.stopwatch.legend.backgroundColor })
      }, [
        s.$slots.controls ? p("", !0) : (r(), u("div", ge, [
          e.value.stopwatch.legend.buttons.start ? (r(), u("button", {
            key: 0,
            title: e.value.stopwatch.legend.buttonTitles.start,
            onClick: R,
            class: "vue-ui-timer-button",
            style: f({
              opacity: a.value ? 0.2 : 1,
              cursor: a.value ? "default" : "pointer"
            })
          }, [
            _(x, {
              name: "play",
              stroke: e.value.stopwatch.legend.buttons.iconColor
            }, null, 8, ["stroke"])
          ], 12, me)) : p("", !0),
          e.value.stopwatch.legend.buttons.pause ? (r(), u("button", {
            key: 1,
            title: l.value ? e.value.stopwatch.legend.buttonTitles.resume : e.value.stopwatch.legend.buttonTitles.pause,
            onClick: H,
            class: "vue-ui-timer-button",
            style: f({
              opacity: a.value ? 1 : 0.2,
              cursor: a.value ? "pointer" : "default"
            })
          }, [
            _(x, {
              name: "pause",
              stroke: e.value.stopwatch.legend.buttons.iconColor
            }, null, 8, ["stroke"])
          ], 12, ye)) : p("", !0),
          e.value.stopwatch.legend.buttons.reset ? (r(), u("button", {
            key: 2,
            title: e.value.stopwatch.legend.buttonTitles.reset,
            onClick: A,
            class: "vue-ui-timer-button",
            style: f({
              opacity: a.value ? 1 : 0.2,
              cursor: a.value ? "pointer" : "default"
            })
          }, [
            _(x, {
              name: "stop",
              stroke: e.value.stopwatch.legend.buttons.iconColor
            }, null, 8, ["stroke"])
          ], 12, be)) : p("", !0),
          e.value.stopwatch.legend.buttons.restart ? (r(), u("button", {
            key: 3,
            title: e.value.stopwatch.legend.buttonTitles.restart,
            onClick: L,
            class: "vue-ui-timer-button",
            style: f({
              opacity: a.value ? 1 : 0.2,
              cursor: a.value ? "pointer" : "default"
            })
          }, [
            _(x, {
              name: "restart",
              stroke: e.value.stopwatch.legend.buttons.iconColor
            }, null, 8, ["stroke"])
          ], 12, _e)) : p("", !0),
          e.value.stopwatch.legend.buttons.lap ? (r(), u("button", {
            key: 4,
            title: e.value.stopwatch.legend.buttonTitles.lap,
            onClick: S,
            class: "vue-ui-timer-button",
            style: f({
              opacity: a.value && !l.value ? 1 : 0.2,
              cursor: a.value && !l.value ? "pointer" : "default"
            })
          }, [
            _(x, {
              name: "lap",
              stroke: e.value.stopwatch.legend.buttons.iconColor
            }, null, 8, ["stroke"])
          ], 12, Me)) : p("", !0)
        ])),
        N(s.$slots, "controls", j(B({
          start: R,
          pause: H,
          reset: A,
          restart: L,
          lap: S,
          laps: y.value,
          isRunning: a.value,
          isPaused: l.value,
          ...i.value
        })), void 0, !0),
        N(s.$slots, "laps", j(B({
          laps: y.value,
          lap: S,
          isRunning: a.value,
          isPaused: l.value,
          ...i.value
        })), void 0, !0)
      ], 4)
    ], 4));
  }
}, De = /* @__PURE__ */ oe(Ce, [["__scopeId", "data-v-1e579c02"]]);
export {
  De as default
};
