import { computed as v, ref as m, watch as U, onMounted as re, openBlock as o, createElementBlock as n, normalizeStyle as C, renderSlot as W, normalizeProps as ne, guardReactiveProps as ie, createElementVNode as f, toDisplayString as X, createCommentVNode as y, unref as c, createVNode as de, Fragment as A, renderList as D, createBlock as ve } from "vue";
import { u as ce, c as he, t as ye, _ as T, o as fe, e as Z, g as pe, w as I, $ as ge, a0 as me, X as ke, s as M, R as q, i as H, f as Y } from "./index-Cm2_6s9o.js";
import { u as J } from "./useNestedProp-BYPZJNhi.js";
import be from "./vue-ui-skeleton-BUS6A44x.js";
import { t as _e, u as xe } from "./useResponsive-CoxXLe23.js";
import { _ as we } from "./PackageVersion-CTMY4zeY.js";
import { _ as Le } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const $e = ["id"], Ne = ["xmlns", "viewBox"], Ce = ["id"], Ie = ["stop-color"], Me = ["stop-color"], Pe = ["id"], Ve = ["stop-color"], ze = ["stop-color"], Se = ["id"], We = ["stop-color"], Ae = ["stop-color"], De = { key: 0 }, Te = ["d", "fill"], Be = ["d", "fill"], Fe = ["d", "stroke", "stroke-width"], Ge = ["x1", "x2", "y1", "y2", "stroke", "stroke-width"], Oe = ["x", "y", "width", "height", "fill", "rx"], Re = ["x1", "x2", "y1", "y2", "stroke", "stroke-width", "stroke-dasharray"], Ee = ["x1", "x2", "y1", "y2", "stroke", "stroke-dasharray", "stroke-width"], je = ["cx", "cy", "r", "fill", "stroke", "stroke-width"], Ue = ["x", "y", "font-size", "font-weight", "fill"], Xe = ["x", "y", "height", "width", "onMouseenter", "onClick"], Ze = {
  __name: "vue-ui-sparkline",
  props: {
    config: {
      type: Object,
      default() {
        return {};
      }
    },
    dataset: {
      type: Array,
      default() {
        return [];
      }
    },
    showInfo: {
      type: Boolean,
      default: !0
    },
    selectedIndex: {
      type: Number,
      default: void 0
    },
    heightRatio: {
      type: Number,
      default: 1
    },
    forcedPadding: {
      type: Number,
      default: 30
    }
  },
  emits: ["hoverIndex", "selectDatapoint"],
  setup(_, { emit: P }) {
    const i = _, { vue_ui_sparkline: K } = ce(), L = v(() => !!i.dataset && i.dataset.length), p = m(he()), V = m(null), B = m(null), F = m(null), e = v({
      get: () => G(),
      set: (t) => t
    });
    function G() {
      const t = J({
        userConfig: i.config,
        defaultConfig: K
      });
      return t.theme ? {
        ...J({
          userConfig: ye.vue_ui_sparkline[t.theme] || i.config,
          defaultConfig: t
        })
      } : t;
    }
    const $ = v(() => T({
      data: i.dataset,
      threshold: e.value.downsample.threshold
    }));
    U(() => i.config, (t) => {
      e.value = G(), R(), l.value.chartWidth = e.value.style.chartWidth;
    }, { deep: !0 }), U(() => i.dataset, (t) => {
      b.value = T({
        data: i.dataset.map((u) => ({
          ...u,
          value: [void 0].includes(u.value) ? null : u.value
        })),
        threshold: e.value.downsample.threshold
      });
    }, { deep: !0 });
    const b = m(Q());
    function Q() {
      return T({
        data: i.dataset.map((t) => e.value.style.animation.show ? {
          ...t,
          value: null
        } : {
          ...t,
          value: [void 0].includes(t.value) ? null : t.value
        }),
        threshold: e.value.downsample.threshold
      });
    }
    const O = m(null);
    re(() => {
      if (R(), e.value.style.animation.show && i.dataset.length > 1) {
        let a = function() {
          u < $.value.length ? (b.value.push($.value[u]), setTimeout(() => {
            requestAnimationFrame(a);
          }, t)) : b.value = $.value, u += 1;
        };
        b.value = [];
        const t = e.value.style.animation.animationFrames / i.dataset.length;
        let u = 0;
        a();
      }
    });
    function R() {
      if (fe(i.dataset) ? Z({
        componentName: "VueUiSparkline",
        type: "dataset"
      }) : i.dataset.forEach((t, u) => {
        pe({
          datasetObject: t,
          requiredAttributes: ["period", "value"]
        }).forEach((a) => {
          Z({
            componentName: "VueUiSparkline",
            type: "datasetSerieAttribute",
            property: a,
            index: u
          });
        });
      }), e.value.responsive) {
        const t = _e(() => {
          const { width: u, height: a } = xe({
            chart: V.value,
            title: e.value.style.title.show && i.showInfo ? B.value : null,
            source: F.value
          });
          l.value.width = u, l.value.height = a, l.value.chartWidth = e.value.style.chartWidth / 500 * u, l.value.padding = i.forcedPadding / 500 * u;
        });
        O.value = new ResizeObserver(t), O.value.observe(V.value.parentNode);
      }
    }
    const l = m({
      height: 80 * i.heightRatio,
      width: 500,
      chartWidth: e.value.style.chartWidth,
      padding: i.forcedPadding
    }), ee = m(6), s = v(() => ({
      top: 12,
      left: 0,
      right: l.value.width,
      bottom: l.value.height - 3,
      start: i.showInfo && e.value.style.dataLabel.show && e.value.style.dataLabel.position === "left" ? l.value.width - l.value.chartWidth : l.value.padding,
      width: i.showInfo && e.value.style.dataLabel.show ? l.value.chartWidth : l.value.width - l.value.padding,
      height: l.value.height - 12
    })), z = v(() => Math.min(...b.value.map((t) => isNaN(t.value) || [void 0, null, "NaN", NaN, 1 / 0, -1 / 0].includes(t.value) ? 0 : t.value || 0))), te = v(() => Math.max(...b.value.map((t) => isNaN(t.value) || [void 0, null, "NaN", NaN, 1 / 0, -1 / 0].includes(t.value) ? 0 : t.value || 0))), x = v(() => {
      const t = z.value >= 0 ? 0 : z.value;
      return Math.abs(t);
    }), ae = v(() => te.value + x.value + ee.value), N = v(() => s.value.bottom - s.value.height * S(x.value));
    function S(t) {
      return t / ae.value;
    }
    const g = v(() => $.value.length - 1), r = v(() => b.value.map((t, u) => {
      const a = isNaN(t.value) || [void 0, null, "NaN", NaN, 1 / 0, -1 / 0].includes(t.value) ? 0 : t.value || 0;
      return {
        absoluteValue: a,
        period: t.period,
        plotValue: a + x.value,
        toMax: S(a + x.value),
        x: s.value.start + u * (s.value.width / (g.value + 1) > l.padding ? l.padding : s.value.width / (g.value + 1)),
        y: s.value.bottom - s.value.height * S(a + x.value),
        id: `plot_${p.value}_${u}`,
        color: w.value ? e.value.style.bar.color : e.value.style.area.useGradient ? I(e.value.style.line.color, 0.05 * (1 - u / g.value)) : e.value.style.line.color,
        width: s.value.width / (g.value + 1) > l.padding ? l.padding : s.value.width / (g.value + 1)
      };
    })), le = v(() => {
      const t = { x: r.value[0].x, y: l.value.height - 6 }, u = { x: r.value[r.value.length - 1].x, y: l.value.height - 6 }, a = [];
      return r.value.forEach((d) => {
        a.push(`${d.x},${d.y} `);
      }), [t.x, t.y, ...a, u.x, u.y].toString();
    }), h = m(void 0);
    function ue(t, u) {
      h.value = t, P("hoverIndex", { index: u });
    }
    function se() {
      h.value = void 0, P("hoverIndex", { index: void 0 });
    }
    const k = v(() => {
      if (L.value) {
        const t = r.value.map((a) => a.absoluteValue), u = t.reduce((a, d) => a + d, 0);
        return {
          latest: r.value[r.value.length - 1].absoluteValue,
          sum: u,
          average: u / r.value.length,
          median: ge(t),
          trend: me(r.value.map(({ x: a, y: d, absoluteValue: j }) => ({
            x: a,
            y: d,
            value: j
          }))).trend
        };
      } else
        return {
          latest: null,
          sum: null,
          average: null,
          median: null,
          trend: null
        };
    }), E = v(() => L.value ? e.value.style.dataLabel.valueType === "latest" ? k.value.latest : e.value.style.dataLabel.valueType === "sum" ? k.value.sum : e.value.style.dataLabel.valueType === "average" ? k.value.average : 0 : 0), w = v(() => e.value.type && e.value.type === "bar");
    function oe(t, u) {
      P("selectDatapoint", { datapoint: t, index: u });
    }
    return (t, u) => (o(), n("div", {
      ref_key: "sparklineChart",
      ref: V,
      class: "vue-ui-sparkline",
      id: p.value,
      style: C(`width:100%;font-family:${e.value.style.fontFamily};`)
    }, [
      W(t.$slots, "before", ne(ie({
        selected: h.value,
        latest: k.value.latest,
        sum: k.value.sum,
        average: k.value.average,
        median: k.value.median,
        trend: k.value.trend
      })), void 0, !0),
      e.value.style.title.show && _.showInfo ? (o(), n("div", {
        key: 0,
        ref_key: "chartTitle",
        ref: B,
        class: "vue-ui-sparkline-title",
        style: C(`display:flex;align-items:center;width:100%;color:${e.value.style.title.color};background:${e.value.style.backgroundColor};justify-content:${e.value.style.title.textAlign === "left" ? "flex-start" : e.value.style.title.textAlign === "right" ? "flex-end" : "center"};height:${e.value.style.title.fontSize * 2}px;font-size:${e.value.style.title.fontSize}px;font-weight:${e.value.style.title.bold ? "bold" : "normal"};`)
      }, [
        f("span", {
          style: C(`padding:${e.value.style.title.textAlign === "left" ? "0 0 0 12px" : e.value.style.title.textAlign === "right" ? "0 12px 0 0" : "0"}`)
        }, X(h.value ? h.value.period : e.value.style.title.text), 5)
      ], 4)) : y("", !0),
      L.value ? (o(), n("svg", {
        key: 1,
        xmlns: c(ke),
        viewBox: `0 0 ${l.value.width} ${l.value.height}`,
        style: C(`background:${e.value.style.backgroundColor};overflow:visible`)
      }, [
        de(we),
        f("defs", null, [
          f("linearGradient", {
            x1: "0%",
            y1: "0%",
            x2: "100%",
            y2: "0%",
            id: `sparkline_gradient_${p.value}`
          }, [
            f("stop", {
              offset: "0%",
              "stop-color": c(M)(c(I)(e.value.style.area.color, 0.05), e.value.style.area.opacity)
            }, null, 8, Ie),
            f("stop", {
              offset: "100%",
              "stop-color": c(M)(e.value.style.area.color, e.value.style.area.opacity)
            }, null, 8, Me)
          ], 8, Ce),
          f("linearGradient", {
            x2: "0%",
            y2: "100%",
            id: `sparkline_bar_gradient_pos_${p.value}`
          }, [
            f("stop", {
              offset: "0%",
              "stop-color": e.value.style.bar.color
            }, null, 8, Ve),
            f("stop", {
              offset: "100%",
              "stop-color": c(I)(e.value.style.bar.color, 0.05)
            }, null, 8, ze)
          ], 8, Pe),
          f("linearGradient", {
            x2: "0%",
            y2: "100%",
            id: `sparkline_bar_gradient_neg_${p.value}`
          }, [
            f("stop", {
              offset: "0%",
              "stop-color": c(I)(e.value.style.bar.color, 0.05)
            }, null, 8, We),
            f("stop", {
              offset: "100%",
              "stop-color": e.value.style.bar.color
            }, null, 8, Ae)
          ], 8, Se)
        ]),
        e.value.style.area.show && !w.value && r.value[0] ? (o(), n("g", De, [
          e.value.style.line.smooth ? (o(), n("path", {
            key: 0,
            d: `M ${r.value[0].x},${s.value.bottom} ${c(q)(r.value)} L ${r.value.at(-1).x},${s.value.bottom} Z`,
            fill: e.value.style.area.useGradient ? `url(#sparkline_gradient_${p.value})` : c(M)(e.value.style.area.color, e.value.style.area.opacity)
          }, null, 8, Te)) : (o(), n("path", {
            key: 1,
            d: `M${le.value}Z`,
            fill: e.value.style.area.useGradient ? `url(#sparkline_gradient_${p.value})` : c(M)(e.value.style.area.color, e.value.style.area.opacity)
          }, null, 8, Be))
        ])) : y("", !0),
        e.value.style.line.smooth && !w.value ? (o(), n("path", {
          key: 1,
          d: `M ${c(q)(r.value)}`,
          stroke: e.value.style.line.color,
          fill: "none",
          "stroke-width": e.value.style.line.strokeWidth,
          "stroke-linecap": "round"
        }, null, 8, Fe)) : y("", !0),
        (o(!0), n(A, null, D(r.value, (a, d) => (o(), n("g", null, [
          d < r.value.length - 1 && !e.value.style.line.smooth && !w.value ? (o(), n("line", {
            key: 0,
            x1: a.x,
            x2: r.value[d + 1].x,
            y1: a.y,
            y2: r.value[d + 1].y,
            stroke: a.color,
            "stroke-width": e.value.style.line.strokeWidth,
            "stroke-linecap": "round",
            "stroke-linejoin": "round",
            "shape-rendering": "geometricPrecision"
          }, null, 8, Ge)) : y("", !0),
          w.value ? (o(), n("rect", {
            key: 1,
            x: a.x - a.width / 2,
            y: a.absoluteValue > 0 ? a.y : N.value,
            width: a.width,
            height: Math.abs(a.y - N.value),
            fill: a.absoluteValue > 0 ? `url(#sparkline_bar_gradient_pos_${p.value})` : `url(#sparkline_bar_gradient_neg_${p.value})`,
            rx: e.value.style.bar.borderRadius
          }, null, 8, Oe)) : y("", !0),
          e.value.style.verticalIndicator.show && (h.value && a.id === h.value.id || _.selectedIndex === d) ? (o(), n("line", {
            key: 2,
            x1: a.x,
            x2: a.x,
            y1: s.value.top - 6,
            y2: s.value.bottom,
            stroke: e.value.style.verticalIndicator.color || a.color,
            "stroke-width": e.value.style.verticalIndicator.strokeWidth,
            "stroke-linecap": "round",
            "stroke-dasharray": e.value.style.verticalIndicator.strokeDasharray || 0
          }, null, 8, Re)) : y("", !0)
        ]))), 256)),
        z.value < 0 ? (o(), n("line", {
          key: 2,
          x1: s.value.start,
          x2: s.value.start + s.value.width - 16,
          y1: N.value,
          y2: N.value,
          stroke: e.value.style.zeroLine.color,
          "stroke-dasharray": e.value.style.zeroLine.strokeWidth * 2,
          "stroke-width": e.value.style.zeroLine.strokeWidth,
          "stroke-linecap": "round"
        }, null, 8, Ee)) : y("", !0),
        e.value.style.plot.show ? (o(!0), n(A, { key: 3 }, D(r.value, (a, d) => (o(), n("g", null, [
          h.value && a.id === h.value.id || _.selectedIndex === d ? (o(), n("circle", {
            key: 0,
            cx: a.x,
            cy: a.y,
            r: e.value.style.plot.radius,
            fill: a.color,
            stroke: e.value.style.plot.stroke,
            "stroke-width": e.value.style.plot.strokeWidth
          }, null, 8, je)) : y("", !0)
        ]))), 256)) : y("", !0),
        _.showInfo && e.value.style.dataLabel.show ? (o(), n("text", {
          key: 4,
          x: e.value.style.dataLabel.position === "left" ? 12 + e.value.style.dataLabel.offsetX : s.value.width + 12 + e.value.style.dataLabel.offsetX,
          y: l.value.height / 2 + e.value.style.dataLabel.fontSize / 2.5 + e.value.style.dataLabel.offsetY,
          "font-size": e.value.style.dataLabel.fontSize,
          "font-weight": e.value.style.dataLabel.bold ? "bold" : "normal",
          fill: e.value.style.dataLabel.color
        }, X(h.value ? c(H)(
          e.value.style.dataLabel.formatter,
          h.value.absoluteValue,
          c(Y)({ p: e.value.style.dataLabel.prefix, v: h.value.absoluteValue, s: e.value.style.dataLabel.suffix, r: e.value.style.dataLabel.roundingValue }),
          { datapoint: h.value }
        ) : c(H)(
          e.value.style.dataLabel.formatter,
          E.value,
          c(Y)({ p: e.value.style.dataLabel.prefix, v: E.value, s: e.value.style.dataLabel.suffix, r: e.value.style.dataLabel.roundingValue })
        )), 9, Ue)) : y("", !0),
        (o(!0), n(A, null, D(r.value, (a, d) => (o(), n("rect", {
          x: a.x - (s.value.width / (g.value + 1) > l.value.padding ? l.value.padding : s.value.width / (g.value + 1)) / 2,
          y: s.value.top - 6,
          height: s.value.height + 6,
          width: s.value.width / (g.value + 1) > l.value.padding ? l.value.padding : s.value.width / (g.value + 1),
          fill: "transparent",
          onMouseenter: (j) => ue(a, d),
          onMouseleave: se,
          onClick: () => oe(a, d)
        }, null, 40, Xe))), 256)),
        W(t.$slots, "svg", { svg: l.value }, void 0, !0)
      ], 12, Ne)) : y("", !0),
      t.$slots.source ? (o(), n("div", {
        key: 2,
        ref_key: "source",
        ref: F,
        dir: "auto"
      }, [
        W(t.$slots, "source", {}, void 0, !0)
      ], 512)) : y("", !0),
      L.value ? y("", !0) : (o(), ve(be, {
        key: 3,
        config: {
          type: "sparkline",
          style: {
            backgroundColor: e.value.style.backgroundColor,
            sparkline: {
              color: "#CCCCCC"
            }
          }
        }
      }, null, 8, ["config"]))
    ], 12, $e));
  }
}, tt = /* @__PURE__ */ Le(Ze, [["__scopeId", "data-v-f4a182c7"]]);
export {
  tt as default
};
