import { computed as v, ref as m, onMounted as ee, openBlock as n, createElementBlock as r, normalizeStyle as N, renderSlot as T, normalizeProps as te, guardReactiveProps as ae, createElementVNode as f, toDisplayString as B, createCommentVNode as y, unref as c, Fragment as P, renderList as A, createBlock as le } from "vue";
import { u as ue, c as se, t as oe, o as ne, e as E, g as re, v as I, U as ie, V as de, X as ve, w as C, P as O, i as j, f as R } from "./index-CguVaFlt.js";
import { u as U } from "./useNestedProp-BbEbJmgv.js";
import ce from "./vue-ui-skeleton-ChHLL5Hw.js";
import { t as he, u as ye } from "./useResponsive-NZB-WLRF.js";
import { _ as fe } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const pe = ["id"], ge = ["xmlns", "viewBox"], ke = ["id"], me = ["stop-color"], be = ["stop-color"], xe = ["id"], _e = ["stop-color"], we = ["stop-color"], $e = ["id"], Le = ["stop-color"], Ne = ["stop-color"], Ie = { key: 0 }, Ce = ["d", "fill"], Me = ["d", "fill"], Ve = ["d", "stroke", "stroke-width"], ze = ["x1", "x2", "y1", "y2", "stroke", "stroke-width"], Se = ["x", "y", "width", "height", "fill", "rx"], Pe = ["x1", "x2", "y1", "y2", "stroke", "stroke-width", "stroke-dasharray"], Ae = ["x1", "x2", "y1", "y2", "stroke", "stroke-dasharray", "stroke-width"], We = ["cx", "cy", "r", "fill", "stroke", "stroke-width"], De = ["x", "y", "font-size", "font-weight", "fill"], Fe = ["x", "y", "height", "width", "onMouseenter", "onClick"], Ge = {
  __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
    }
  },
  emits: ["hoverIndex", "selectDatapoint"],
  setup(x, { emit: M }) {
    const i = x, { vue_ui_sparkline: X } = ue(), $ = v(() => !!i.dataset && i.dataset.length), p = m(se()), V = m(null), W = m(null), e = v(() => {
      const t = U({
        userConfig: i.config,
        defaultConfig: X
      });
      return t.theme ? {
        ...U({
          userConfig: oe.vue_ui_sparkline[t.theme] || i.config,
          defaultConfig: t
        })
      } : t;
    }), b = m(i.dataset.map((t) => e.value.style.animation.show ? {
      ...t,
      value: null
    } : {
      ...t,
      value: [void 0].includes(t.value) ? null : t.value
    })), D = m(null);
    ee(() => {
      if (ne(i.dataset) ? E({
        componentName: "VueUiSparkline",
        type: "dataset"
      }) : i.dataset.forEach((t, u) => {
        re({
          datasetObject: t,
          requiredAttributes: ["period", "value"]
        }).forEach((a) => {
          E({
            componentName: "VueUiSparkline",
            type: "datasetSerieAttribute",
            property: a,
            index: u
          });
        });
      }), e.value.style.animation.show && i.dataset.length > 1) {
        let a = function() {
          u < i.dataset.length ? (b.value.push(i.dataset[u]), setTimeout(() => {
            requestAnimationFrame(a);
          }, t)) : b.value = i.dataset, u += 1;
        };
        b.value = [];
        const t = e.value.style.animation.animationFrames / i.dataset.length;
        let u = 0;
        a();
      }
      if (e.value.responsive) {
        const t = he(() => {
          const { width: u, height: a } = ye({
            chart: V.value,
            title: e.value.style.title.show && i.showInfo ? W.value : null
          });
          l.value.width = u, l.value.height = a, l.value.chartWidth = e.value.style.chartWidth / 500 * u, l.value.padding = 0.06 * u;
        });
        D.value = new ResizeObserver(t), D.value.observe(V.value.parentNode);
      }
    });
    const l = m({
      height: 80,
      width: 500,
      chartWidth: e.value.style.chartWidth,
      padding: 30
    }), Z = 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))), q = 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))), _ = v(() => {
      const t = z.value >= 0 ? 0 : z.value;
      return Math.abs(t);
    }), H = v(() => q.value + _.value + Z.value), L = v(() => s.value.bottom - s.value.height * S(_.value));
    function S(t) {
      return t / H.value;
    }
    const g = v(() => i.dataset.length - 1), o = 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 + _.value,
        toMax: S(a + _.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 + _.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)
      };
    })), Y = v(() => {
      const t = { x: o.value[0].x, y: l.value.height - 6 }, u = { x: o.value[o.value.length - 1].x, y: l.value.height - 6 }, a = [];
      return o.value.forEach((d) => {
        a.push(`${d.x},${d.y} `);
      }), [t.x, t.y, ...a, u.x, u.y].toString();
    }), h = m(void 0);
    function J(t, u) {
      h.value = t, M("hoverIndex", { index: u });
    }
    function K() {
      h.value = void 0, M("hoverIndex", { index: void 0 });
    }
    const k = v(() => {
      if ($.value) {
        const t = o.value.map((a) => a.absoluteValue), u = t.reduce((a, d) => a + d, 0);
        return {
          latest: o.value[o.value.length - 1].absoluteValue,
          sum: u,
          average: u / o.value.length,
          median: ie(t),
          trend: de(o.value.map(({ x: a, y: d, absoluteValue: G }) => ({
            x: a,
            y: d,
            value: G
          }))).trend
        };
      } else
        return {
          latest: null,
          sum: null,
          average: null,
          median: null,
          trend: null
        };
    }), F = v(() => $.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 Q(t, u) {
      M("selectDatapoint", { datapoint: t, index: u });
    }
    return (t, u) => (n(), r("div", {
      ref_key: "sparklineChart",
      ref: V,
      class: "vue-ui-sparkline",
      id: p.value,
      style: N(`width:100%;font-family:${e.value.style.fontFamily}`)
    }, [
      T(t.$slots, "before", te(ae({
        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 && x.showInfo ? (n(), r("div", {
        key: 0,
        ref_key: "chartTitle",
        ref: W,
        class: "vue-ui-sparkline-title",
        style: N(`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: N(`padding:${e.value.style.title.textAlign === "left" ? "0 0 0 12px" : e.value.style.title.textAlign === "right" ? "0 12px 0 0" : "0"}`)
        }, B(h.value ? h.value.period : e.value.style.title.text), 5)
      ], 4)) : y("", !0),
      $.value ? (n(), r("svg", {
        key: 1,
        xmlns: c(ve),
        viewBox: `0 0 ${l.value.width} ${l.value.height}`,
        style: N(`background:${e.value.style.backgroundColor};overflow:visible`)
      }, [
        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(I)(e.value.style.area.color, 0.05)}${c(C)[e.value.style.area.opacity]}`
            }, null, 8, me),
            f("stop", {
              offset: "100%",
              "stop-color": e.value.style.area.color + c(C)[e.value.style.area.opacity]
            }, null, 8, be)
          ], 8, ke),
          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, _e),
            f("stop", {
              offset: "100%",
              "stop-color": `${c(I)(e.value.style.bar.color, 0.05)}`
            }, null, 8, we)
          ], 8, xe),
          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, Le),
            f("stop", {
              offset: "100%",
              "stop-color": e.value.style.bar.color
            }, null, 8, Ne)
          ], 8, $e)
        ]),
        e.value.style.area.show && !w.value && o.value[0] ? (n(), r("g", Ie, [
          e.value.style.line.smooth ? (n(), r("path", {
            key: 0,
            d: `M ${o.value[0].x},${s.value.bottom} ${c(O)(o.value)} L ${o.value.at(-1).x},${s.value.bottom} Z`,
            fill: e.value.style.area.useGradient ? `url(#sparkline_gradient_${p.value})` : `${e.value.style.area.color}${c(C)[e.value.style.area.opacity]}`
          }, null, 8, Ce)) : (n(), r("path", {
            key: 1,
            d: `M${Y.value}Z`,
            fill: e.value.style.area.useGradient ? `url(#sparkline_gradient_${p.value})` : `${e.value.style.area.color}${c(C)[e.value.style.area.opacity]}`
          }, null, 8, Me))
        ])) : y("", !0),
        e.value.style.line.smooth && !w.value ? (n(), r("path", {
          key: 1,
          d: `M ${c(O)(o.value)}`,
          stroke: e.value.style.line.color,
          fill: "none",
          "stroke-width": e.value.style.line.strokeWidth,
          "stroke-linecap": "round"
        }, null, 8, Ve)) : y("", !0),
        (n(!0), r(P, null, A(o.value, (a, d) => (n(), r("g", null, [
          d < o.value.length - 1 && !e.value.style.line.smooth && !w.value ? (n(), r("line", {
            key: 0,
            x1: a.x,
            x2: o.value[d + 1].x,
            y1: a.y,
            y2: o.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, ze)) : y("", !0),
          w.value ? (n(), r("rect", {
            key: 1,
            x: a.x - a.width / 2,
            y: a.absoluteValue > 0 ? a.y : L.value,
            width: a.width,
            height: Math.abs(a.y - L.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, Se)) : y("", !0),
          e.value.style.verticalIndicator.show && (h.value && a.id === h.value.id || x.selectedIndex === d) ? (n(), r("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, Pe)) : y("", !0)
        ]))), 256)),
        z.value < 0 ? (n(), r("line", {
          key: 2,
          x1: s.value.start,
          x2: s.value.start + s.value.width - 16,
          y1: L.value,
          y2: L.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, Ae)) : y("", !0),
        e.value.style.plot.show ? (n(!0), r(P, { key: 3 }, A(o.value, (a, d) => (n(), r("g", null, [
          h.value && a.id === h.value.id || x.selectedIndex === d ? (n(), r("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, We)) : y("", !0)
        ]))), 256)) : y("", !0),
        x.showInfo && e.value.style.dataLabel.show ? (n(), r("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
        }, B(h.value ? c(j)(
          e.value.style.dataLabel.formatter,
          h.value.absoluteValue,
          c(R)({ 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(j)(
          e.value.style.dataLabel.formatter,
          F.value,
          c(R)({ p: e.value.style.dataLabel.prefix, v: F.value, s: e.value.style.dataLabel.suffix, r: e.value.style.dataLabel.roundingValue })
        )), 9, De)) : y("", !0),
        (n(!0), r(P, null, A(o.value, (a, d) => (n(), r("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: (G) => J(a, d),
          onMouseleave: K,
          onClick: () => Q(a, d)
        }, null, 40, Fe))), 256)),
        T(t.$slots, "svg", { svg: l.value }, void 0, !0)
      ], 12, ge)) : y("", !0),
      $.value ? y("", !0) : (n(), le(ce, {
        key: 2,
        config: {
          type: "sparkline",
          style: {
            backgroundColor: e.value.style.backgroundColor,
            sparkline: {
              color: "#CCCCCC"
            }
          }
        }
      }, null, 8, ["config"]))
    ], 12, pe));
  }
}, Ue = /* @__PURE__ */ fe(Ge, [["__scopeId", "data-v-a8f8425a"]]);
export {
  Ue as default
};
