import { computed as u, ref as g, onMounted as X, openBlock as n, createElementBlock as i, normalizeStyle as A, createBlock as C, unref as c, createElementVNode as m, createCommentVNode as d, toDisplayString as I } from "vue";
import { u as Z, c as R, t as H, o as J, e as K, s as y, T as Q, X as Y, w as N, P as B, f as V, i as ee } from "./index-CguVaFlt.js";
import { u as D } from "./useNestedProp-BbEbJmgv.js";
import M from "./BaseIcon-BNFf4B_v.js";
import te from "./vue-ui-skeleton-ChHLL5Hw.js";
import { _ as ae } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const le = ["id"], oe = ["xmlns", "viewBox"], ne = ["id"], se = ["stop-color"], ue = ["stop-color"], re = { key: 0 }, ie = ["d", "fill"], ve = ["d", "fill"], de = ["d", "stroke", "stroke-width", "stroke-linecap", "stroke-linejoin"], ce = ["d", "stroke", "stroke-width", "stroke-linecap", "stroke-linejoin"], ye = ["height", "width", "x"], he = { style: { width: "100%" } }, fe = ["x", "y", "fill", "font-size", "font-weight"], pe = ["stroke", "cx", "cy", "fill"], ge = ["x", "y", "font-size", "fill", "font-weight"], me = {
  __name: "vue-ui-spark-trend",
  props: {
    config: {
      type: Object,
      default() {
        return {};
      }
    },
    dataset: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  setup(E) {
    const s = E, { vue_ui_spark_trend: G } = Z(), O = u(() => !!s.dataset && s.dataset.length), k = g(R()), e = u(() => {
      const t = D({
        userConfig: s.config,
        defaultConfig: G
      });
      return t.theme ? {
        ...D({
          userConfig: H.vue_ui_spark_trend[t.theme] || s.config,
          defaultConfig: t
        })
      } : t;
    });
    function L(t) {
      return t.map((l) => y(l));
    }
    const _ = g(s.dataset.map((t) => e.value.style.animation.show || [void 0].includes(t) ? null : t)), p = g(!1), $ = g(null);
    X(() => {
      J(s.dataset) && K({
        componentName: "VueUiSparkTrend",
        type: "dataset"
      });
      let l = 1e3 / e.value.style.animation.animationFrames, v = performance.now();
      if (e.value.style.animation.show && e.value.style.animation.animationFrames && s.dataset.length > 1) {
        let z = function() {
          p.value = !0;
          let F = performance.now(), j = F - v;
          j > l ? (v = F - j % l, f < s.dataset.length ? (_.value.push(s.dataset[f]), f += 1, $.value = requestAnimationFrame(z)) : (cancelAnimationFrame($.value), _.value = L(s.dataset), p.value = !1)) : $.value = requestAnimationFrame(z);
        };
        _.value = [];
        let f = 0;
        z();
      }
    });
    const o = g({
      height: 80,
      width: 300
    }), h = u(() => ({
      top: e.value.style.padding.top,
      left: e.value.style.padding.left,
      right: o.value.width - e.value.style.padding.right,
      bottom: o.value.height - e.value.style.padding.bottom,
      height: o.value.height - (e.value.style.padding.top + e.value.style.padding.bottom) - (e.value.style.dataLabel.show ? e.value.style.dataLabel.fontSize : 0),
      width: o.value.width - (e.value.style.padding.left + e.value.style.padding.right)
    })), T = u(() => {
      const t = L(s.dataset);
      return {
        max: Math.max(...t.map((l) => y(l))),
        min: Math.min(...t.map((l) => y(l)))
      };
    }), b = u(() => {
      const t = T.value.min >= 0 ? 0 : T.value.min;
      return Math.abs(t);
    }), U = u(() => T.value.max + b.value);
    function S(t) {
      return t / U.value;
    }
    const P = u(() => s.dataset.length), a = u(() => _.value.map((t, l) => {
      const v = isNaN(t) || [void 0, null, "NaN", NaN, 1 / 0, -1 / 0].includes(t) ? 0 : t || 0;
      return {
        value: y(t),
        absoluteValue: y(v),
        plotValue: y(v + b.value),
        toMax: S(v + b.value),
        x: h.value.left + y(l * (h.value.width / (P.value - 1))) - e.value.style.padding.right,
        y: h.value.bottom - y(h.value.height * S(v + b.value))
      };
    })), x = u(() => {
      const t = L(s.dataset);
      return e.value.style.trendLabel.trendType === "global" ? Q(t) : e.value.style.trendLabel.trendType === "n-1" && t.length > 1 ? (t.at(-1) / t.at(-2) - 1) * 100 : e.value.style.trendLabel.trendType === "lastToFirst" ? (t.at(-1) / t[0] - 1) * 100 : 0;
    }), w = u(() => p.value || x.value === 0 ? "neutral" : x.value > 0 ? "positive" : "negative"), r = u(() => e.value.style.arrow.colors[w.value]), q = u(() => {
      const t = { x: a.value[0].x, y: o.value.height - 6 }, l = { x: a.value[a.value.length - 1].x, y: o.value.height - 6 }, v = [];
      return a.value.forEach((f) => {
        v.push(`${f.x},${f.y} `);
      }), [t.x, t.y, ...v, l.x, l.y].toString();
    }), W = u(() => {
      let t = [];
      return a.value.forEach((l) => {
        t.push(`${l.x},${l.y} `);
      }), `M ${t.toString()}`;
    });
    return (t, l) => (n(), i("div", {
      class: "vue-ui-spark-trend",
      id: k.value,
      style: A(`width:100%;font-family:${e.value.style.fontFamily};backgroundColor:${e.value.style.backgroundColor}`)
    }, [
      O.value ? (n(), i("svg", {
        key: 1,
        xmlns: c(Y),
        viewBox: `0 0 ${o.value.width} ${o.value.height}`,
        style: A(`width:100%;background:${e.value.style.backgroundColor};overflow:visible`)
      }, [
        m("defs", null, [
          m("linearGradient", {
            x1: "0%",
            y1: "0%",
            x2: "0%",
            y2: "100%",
            id: `pill_gradient_${k.value}`
          }, [
            m("stop", {
              offset: "0%",
              "stop-color": (e.value.style.line.useColorTrend ? r.value : e.value.style.line.stroke) + c(N)[e.value.style.area.opacity]
            }, null, 8, se),
            m("stop", {
              offset: "100%",
              "stop-color": e.value.style.backgroundColor
            }, null, 8, ue)
          ], 8, ne)
        ]),
        e.value.style.area.show && a.value[0] ? (n(), i("g", re, [
          e.value.style.line.smooth ? (n(), i("path", {
            key: 0,
            d: `M ${a.value[0].x},${h.value.bottom} ${c(B)(a.value)} L ${a.value.at(-1).x},${h.value.bottom} Z`,
            fill: e.value.style.area.useGradient ? `url(#pill_gradient_${k.value})` : `${e.value.style.line.useColorTrend ? r.value : e.value.style.line.stroke}${c(N)[e.value.style.area.opacity]}`,
            stroke: "none"
          }, null, 8, ie)) : (n(), i("path", {
            key: 1,
            d: `M${q.value}Z`,
            fill: e.value.style.area.useGradient ? `url(#pill_gradient_${k.value})` : `${e.value.style.line.useColorTrend ? r.value : e.value.style.line.stroke}${c(N)[e.value.style.area.opacity]}`,
            stroke: "none"
          }, null, 8, ve))
        ])) : d("", !0),
        e.value.style.line.smooth && a.value.length ? (n(), i("path", {
          key: 1,
          d: `M ${c(B)(a.value)}`,
          stroke: e.value.style.line.useColorTrend ? r.value : e.value.style.line.stroke,
          fill: "none",
          "stroke-width": e.value.style.line.strokeWidth,
          "stroke-linecap": e.value.style.line.strokeLinecap,
          "stroke-linejoin": e.value.style.line.strokeLinejoin
        }, null, 8, de)) : d("", !0),
        !e.value.style.line.smooth && a.value.length ? (n(), i("path", {
          key: 2,
          d: W.value,
          stroke: e.value.style.line.useColorTrend ? r.value : e.value.style.line.stroke,
          fill: "none",
          "stroke-width": e.value.style.line.strokeWidth,
          "stroke-linecap": e.value.style.line.strokeLinecap,
          "stroke-linejoin": e.value.style.line.strokeLinejoin
        }, null, 8, ce)) : d("", !0),
        p.value ? d("", !0) : (n(), i("foreignObject", {
          key: 3,
          height: o.value.height / 2,
          width: o.value.height / 2,
          x: o.value.height / 5,
          y: 8
        }, [
          m("div", he, [
            w.value === "positive" ? (n(), C(M, {
              key: 0,
              stroke: r.value,
              name: "arrowTop",
              size: o.value.height / 2
            }, null, 8, ["stroke", "size"])) : d("", !0),
            w.value === "negative" ? (n(), C(M, {
              key: 1,
              stroke: r.value,
              name: "arrowBottom",
              size: o.value.height / 2
            }, null, 8, ["stroke", "size"])) : d("", !0),
            w.value === "neutral" ? (n(), C(M, {
              key: 2,
              stroke: r.value,
              name: "arrowRight",
              size: o.value.height / 2
            }, null, 8, ["stroke", "size"])) : d("", !0)
          ])
        ], 8, ye)),
        p.value ? d("", !0) : (n(), i("text", {
          key: 4,
          x: o.value.height / 2 - o.value.height / 20,
          y: h.value.bottom,
          "text-anchor": "middle",
          fill: e.value.style.trendLabel.useColorTrend ? r.value : e.value.style.trendLabel.color,
          "font-size": e.value.style.trendLabel.fontSize,
          "font-weight": e.value.style.trendLabel.bold ? "bold" : "normal"
        }, I(c(V)({
          p: x.value > 0 ? "+" : "",
          v: x.value,
          s: "%",
          r: e.value.style.trendLabel.rounding
        })), 9, fe)),
        a.value.length && a.value.at(-1).x !== void 0 ? (n(), i("circle", {
          key: 5,
          stroke: e.value.style.backgroundColor,
          "stroke-width": 2,
          cx: a.value.at(-1).x,
          cy: a.value.at(-1).y,
          r: 4,
          fill: r.value
        }, null, 8, pe)) : d("", !0),
        a.value.length && a.value.at(-1).x !== void 0 && e.value.style.dataLabel.show ? (n(), i("text", {
          key: 6,
          "text-anchor": "middle",
          x: a.value.at(-1).x,
          y: a.value.at(-1).y - e.value.style.dataLabel.fontSize / 1.5,
          "font-size": e.value.style.dataLabel.fontSize,
          fill: e.value.style.dataLabel.useColorTrend ? r.value : e.value.style.dataLabel.color,
          "font-weight": e.value.style.dataLabel.bold ? "bold" : "normal"
        }, I(c(ee)(
          e.value.style.dataLabel.formatter,
          a.value.at(-1).value,
          c(V)({
            p: e.value.style.dataLabel.prefix,
            v: a.value.at(-1).value,
            s: e.value.style.dataLabel.suffix,
            r: e.value.style.dataLabel.rounding
          }),
          { datapoint: a.value.at(-1) }
        )), 9, ge)) : d("", !0)
      ], 12, oe)) : (n(), C(te, {
        key: 0,
        config: {
          type: "sparkline",
          style: {
            backgroundColor: e.value.style.backgroundColor,
            sparkline: {
              color: "#CCCCCC"
            }
          }
        }
      }, null, 8, ["config"]))
    ], 12, le));
  }
}, Le = /* @__PURE__ */ ae(me, [["__scopeId", "data-v-fa45a70f"]]);
export {
  Le as default
};
