import { ref as n, onMounted as Q, onBeforeUnmount as Y, computed as z, watch as Z, openBlock as l, createElementBlock as r, normalizeStyle as f, createBlock as ee, createCommentVNode as v, unref as te, createVNode as M, createElementVNode as C, mergeProps as V, renderSlot as O, normalizeProps as U, guardReactiveProps as E, toDisplayString as ae } from "vue";
import { u as se } from "./useNestedProp-BYPZJNhi.js";
import { u as le, c as oe, X as re, F as W } from "./index-Cm2_6s9o.js";
import { t as ue, u as ie } from "./useResponsive-CoxXLe23.js";
import P from "./BaseIcon-DpLJi6ci.js";
import { _ as ne } from "./Title-Coit3NcT.js";
import { _ as ce } from "./PackageVersion-CTMY4zeY.js";
import { _ as de } from "./_plugin-vue_export-helper-CHgC5LLL.js";
class he {
  constructor(A, k, T, H = !0, x = !0) {
    this.interval = k, this.elapsed = 0, this.isPaused = !1;
    const S = `
            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;
                }
            };
        `, R = new Blob([S], { type: "application/javascript" }), m = URL.createObjectURL(R), i = new Worker(m);
    function I(c) {
      let e = Math.floor(c / 1e3), w = Math.floor(c % 1e3 / 10), y = Math.floor(e / 3600), t = Math.floor(e % 3600 / 60), d = e % 60, u = "";
      return x && (u += String(y).padStart(2, "0") + ":"), u += String(t).padStart(2, "0") + ":", u += String(d).padStart(2, "0"), H && (u += "." + String(w).padStart(2, "0")), u;
    }
    this.start = () => {
      this.isPaused = !1, i.postMessage({ action: "start", data: { interval: this.interval } });
    }, this.pause = () => {
      this.isPaused ? this.resume() : (this.isPaused = !0, i.postMessage({ action: "pause" }));
    }, this.resume = () => {
      this.isPaused && (i.postMessage({ action: "resume" }), this.isPaused = !1);
    }, this.stop = () => {
      i.postMessage({ action: "stop" }), this.isPaused = !1;
    }, this.reset = () => {
      i.postMessage({ action: "reset" }), this.elapsed = 0, this.isPaused = !1;
    }, this.restart = () => {
      this.stop(), this.start();
    }, this.lap = () => new Promise((c) => {
      i.postMessage({ action: "lap" });
      const e = (w) => {
        const { elapsed: y, timestamp: t, action: d } = w.data;
        if (d === "lap") {
          const u = I(y);
          c({
            timestamp: t || 0,
            elapsed: y,
            formatted: u
          });
        }
      };
      i.addEventListener("message", e, { once: !0 });
    }), i.onmessage = (c) => {
      const { elapsed: e, timestamp: w } = c.data;
      this.elapsed = e, A({
        timestamp: w || 0,
        elapsed: this.elapsed,
        formatted: I(this.elapsed)
      });
    }, i.onerror = (c) => {
      T && T(c);
    };
  }
}
const ve = ["xmlns", "viewBox"], pe = { key: 0 }, fe = ["id"], we = ["stop-color"], ke = ["stop-color"], ge = ["cx", "cy", "r", "fill", "stroke", "stroke-width"], me = ["d", "stroke", "stroke-width"], ye = ["r", "fill", "stroke", "stroke-width"], be = ["r", "fill", "stroke", "stroke-width"], _e = ["x", "y"], Me = ["x", "y", "font-size", "fill", "font-weight"], Ce = {
  key: 0,
  class: "vue-ui-timer-controls"
}, Te = ["title"], xe = ["title"], Ie = ["title"], $e = ["title"], Pe = ["title"], Se = {
  __name: "vue-ui-timer",
  props: {
    config: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  emits: ["start", "pause", "reset", "restart", "lap"],
  setup(G, { expose: A, emit: k }) {
    const T = G, { vue_ui_timer: H } = le(), x = n(null), S = n(null), R = n(null), m = n(null), i = n(oe()), I = n(0);
    Q(() => {
      c();
    });
    function c() {
      if (e.value.responsive) {
        const a = ue(() => {
          const { width: o, height: h } = ie({
            chart: x.value,
            title: e.value.style.title.text ? S.value : null,
            legend: R.value
          });
          t.value.width = o, t.value.height = h, t.value.tracker.core = W({
            relator: Math.min(o, h),
            adjuster: e.value.style.width,
            source: 6 * e.value.stopwatch.tracker.radiusRatio,
            threshold: 1,
            fallback: 1
          }), t.value.tracker.aura = W({
            relator: Math.min(o, h),
            adjuster: e.value.style.width,
            source: 12 * e.value.stopwatch.tracker.aura.radiusRatio,
            threshold: 1,
            fallback: 1
          }), t.value.label = W({
            relator: Math.min(o, h),
            adjuster: e.value.style.width,
            source: e.value.stopwatch.label.fontSize,
            threshold: 10,
            fallback: 10
          });
        });
        m.value = new ResizeObserver(a), m.value.observe(x.value.parentNode);
      }
    }
    Y(() => {
      m.value && m.value.disconnect();
    });
    const e = z({
      get: () => w(),
      set: (a) => a
    });
    function w() {
      return se({
        userConfig: T.config,
        defaultConfig: H
      });
    }
    Z(() => T.config, (a) => {
      e.value = w(), c(), I.value += 1;
    }, { deep: !0 });
    const y = z(() => {
      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 = n({
      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
    }), d = n(0), u = new he(
      (a) => X(a),
      10,
      "",
      e.value.stopwatch.showHundredth,
      e.value.stopwatch.showHours
    ), D = n(!0), s = n(!1), p = n(!1);
    function L() {
      k("start"), D.value && u.start(), D.value = !1, s.value = !0;
    }
    function N() {
      s.value && (k("reset"), u.stop(), b.value = [], D.value = !0, s.value = !1);
    }
    function B() {
      p.value = !p.value, k("pause", d.value), u.pause();
    }
    function j() {
      s.value && (p.value = !1, k("restart"), b.value = [], u.restart());
    }
    const b = n([]);
    async function F() {
      if (!s.value || p.value) return;
      const a = await u.lap();
      a && (b.value.push(a), k("lap", b.value));
    }
    function X({ timestamp: a, elapsed: o, formatted: h }) {
      d.value = { timestamp: a, elapsed: o, formatted: h };
    }
    const g = z(() => Math.min(t.value.width, t.value.height) / 2.5 * e.value.stopwatch.track.radiusRatio);
    function q(a, o) {
      const $ = 360 / (o * 1e3);
      return a * $ % 360;
    }
    function J(a) {
      let o = a * (Math.PI / 180), h = t.value.width / 2 + g.value * Math.cos(o), $ = t.value.height / 2 + g.value * Math.sin(o);
      return { cx: h, cy: $ };
    }
    const _ = z(() => {
      const a = q(d.value.elapsed, e.value.stopwatch.cycleSeconds), { cx: o, cy: h } = J(a - 90), $ = a > 180 ? 1 : 0;
      return {
        cx: o || t.value.width / 2,
        cy: h || t.value.height / 2 - g.value,
        largeArcFlag: $,
        sweepFlag: 1
      };
    });
    return A({
      start: L,
      pause: B,
      reset: N,
      restart: j,
      lap: F
    }), (a, o) => (l(), r("div", {
      ref_key: "timerChart",
      ref: x,
      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 ? (l(), r("div", {
        key: 0,
        ref_key: "chartTitle",
        ref: S,
        style: f({
          width: "100%",
          background: e.value.style.backgroundColor
        })
      }, [
        (l(), ee(ne, {
          key: `title_${I.value}`,
          config: {
            title: {
              cy: "title",
              ...e.value.style.title
            },
            subtitle: {
              cy: "subtitle",
              ...e.value.style.title.subtitle
            }
          }
        }, null, 8, ["config"]))
      ], 4)) : v("", !0),
      (l(), r("svg", {
        xmlns: te(re),
        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
        })
      }, [
        M(ce),
        e.value.stopwatch.tracker.gradient.show ? (l(), r("defs", pe, [
          C("radialGradient", {
            id: `tracker_gradient_${i.value}`,
            cx: "50%",
            cy: "50%",
            r: "50%",
            fx: "50%",
            fy: "50%"
          }, [
            C("stop", {
              offset: "0%",
              "stop-color": e.value.stopwatch.tracker.gradient.color
            }, null, 8, we),
            C("stop", {
              offset: "100%",
              "stop-color": e.value.stopwatch.tracker.fill
            }, null, 8, ke)
          ], 8, fe)
        ])) : v("", !0),
        C("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, ge),
        e.value.stopwatch.cycleTrack.show ? (l(), r("path", {
          key: 1,
          d: `M ${t.value.width / 2},${t.value.height / 2 - g.value} A ${g.value},${g.value} 0 ${_.value.largeArcFlag},${_.value.sweepFlag} ${_.value.cx},${_.value.cy}`,
          stroke: e.value.stopwatch.cycleTrack.stroke,
          "stroke-width": e.value.stopwatch.cycleTrack.strokeWidth,
          "stroke-linecap": "round",
          fill: "none"
        }, null, 8, me)) : v("", !0),
        C("circle", V(_.value, {
          r: t.value.tracker.core,
          fill: e.value.stopwatch.tracker.gradient.show ? `url(#tracker_gradient_${i.value})` : e.value.stopwatch.tracker.fill,
          stroke: e.value.stopwatch.tracker.stroke,
          "stroke-width": e.value.stopwatch.tracker.strokeWidth
        }), null, 16, ye),
        e.value.stopwatch.tracker.aura.show ? (l(), r("circle", V({ key: 2 }, _.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, be)) : v("", !0),
        a.$slots.time ? (l(), r("foreignObject", {
          key: 3,
          x: t.value.width / 2,
          y: t.value.height / 2,
          height: "0.1",
          width: "0.1",
          style: { overflow: "visible" }
        }, [
          O(a.$slots, "time", U(E({ ...d.value })), void 0, !0)
        ], 8, _e)) : (l(), r("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" }
        }, ae(d.value.formatted || y.value), 9, Me))
      ], 12, ve)),
      C("div", {
        ref_key: "chartLegend",
        ref: R,
        style: f({ width: "100%", backgroundColor: e.value.stopwatch.legend.backgroundColor })
      }, [
        a.$slots.controls ? v("", !0) : (l(), r("div", Ce, [
          e.value.stopwatch.legend.buttons.start ? (l(), r("button", {
            key: 0,
            title: e.value.stopwatch.legend.buttonTitles.start,
            onClick: L,
            class: "vue-ui-timer-button",
            style: f({
              opacity: s.value ? 0.2 : 1,
              cursor: s.value ? "default" : "pointer"
            })
          }, [
            M(P, {
              name: "play",
              stroke: e.value.stopwatch.legend.buttons.iconColor
            }, null, 8, ["stroke"])
          ], 12, Te)) : v("", !0),
          e.value.stopwatch.legend.buttons.pause ? (l(), r("button", {
            key: 1,
            title: p.value ? e.value.stopwatch.legend.buttonTitles.resume : e.value.stopwatch.legend.buttonTitles.pause,
            onClick: B,
            class: "vue-ui-timer-button",
            style: f({
              opacity: s.value ? 1 : 0.2,
              cursor: s.value ? "pointer" : "default"
            })
          }, [
            M(P, {
              name: "pause",
              stroke: e.value.stopwatch.legend.buttons.iconColor
            }, null, 8, ["stroke"])
          ], 12, xe)) : v("", !0),
          e.value.stopwatch.legend.buttons.reset ? (l(), r("button", {
            key: 2,
            title: e.value.stopwatch.legend.buttonTitles.reset,
            onClick: N,
            class: "vue-ui-timer-button",
            style: f({
              opacity: s.value ? 1 : 0.2,
              cursor: s.value ? "pointer" : "default"
            })
          }, [
            M(P, {
              name: "stop",
              stroke: e.value.stopwatch.legend.buttons.iconColor
            }, null, 8, ["stroke"])
          ], 12, Ie)) : v("", !0),
          e.value.stopwatch.legend.buttons.restart ? (l(), r("button", {
            key: 3,
            title: e.value.stopwatch.legend.buttonTitles.restart,
            onClick: j,
            class: "vue-ui-timer-button",
            style: f({
              opacity: s.value ? 1 : 0.2,
              cursor: s.value ? "pointer" : "default"
            })
          }, [
            M(P, {
              name: "restart",
              stroke: e.value.stopwatch.legend.buttons.iconColor
            }, null, 8, ["stroke"])
          ], 12, $e)) : v("", !0),
          e.value.stopwatch.legend.buttons.lap ? (l(), r("button", {
            key: 4,
            title: e.value.stopwatch.legend.buttonTitles.lap,
            onClick: F,
            class: "vue-ui-timer-button",
            style: f({
              opacity: s.value && !p.value ? 1 : 0.2,
              cursor: s.value && !p.value ? "pointer" : "default"
            })
          }, [
            M(P, {
              name: "lap",
              stroke: e.value.stopwatch.legend.buttons.iconColor
            }, null, 8, ["stroke"])
          ], 12, Pe)) : v("", !0)
        ])),
        O(a.$slots, "controls", U(E({
          start: L,
          pause: B,
          reset: N,
          restart: j,
          lap: F,
          laps: b.value,
          isRunning: s.value,
          isPaused: p.value,
          ...d.value
        })), void 0, !0),
        O(a.$slots, "laps", U(E({
          laps: b.value,
          lap: F,
          isRunning: s.value,
          isPaused: p.value,
          ...d.value
        })), void 0, !0)
      ], 4)
    ], 4));
  }
}, Be = /* @__PURE__ */ de(Se, [["__scopeId", "data-v-8609e7e7"]]);
export {
  Be as default
};
