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