import { computed as g, ref as x, onMounted as G, watch as W, nextTick as q, openBlock as r, createElementBlock as v, normalizeStyle as y, unref as s, renderSlot as I, normalizeProps as P, mergeProps as H, createCommentVNode as d, createElementVNode as c, toDisplayString as p, Fragment as z, renderList as X, guardReactiveProps as R, createBlock as J } from "vue";
import { u as K, c as Q, t as Y, a as Z, p as k, b as ee, o as te, e as L, g as le, d as ae, w as _, i as w, f as b, X as oe, v as se } from "./index-CguVaFlt.js";
import { u as T } from "./useNestedProp-BbEbJmgv.js";
import ue from "./vue-ui-skeleton-ChHLL5Hw.js";
const ie = ["onClick"], ne = { key: 1 }, re = ["xmlns", "viewBox"], ve = ["id"], ce = ["stop-color"], de = ["stop-color"], ye = ["height", "width", "fill", "rx"], pe = ["height", "width", "fill", "rx"], fe = ["height", "width", "fill", "rx"], ke = {
  __name: "vue-ui-sparkbar",
  props: {
    config: {
      type: Object,
      default() {
        return {};
      }
    },
    dataset: {
      type: Array,
      default() {
        return [];
      }
    },
    /**
     * Used in VueUiRadar's tooltip exclusively
     */
    backgroundOpacity: {
      type: Number,
      default: 100
    }
  },
  emits: ["selectDatapoint"],
  setup(C, { emit: E }) {
    const a = C, { vue_ui_sparkbar: O } = K(), $ = g(() => !!a.dataset && a.dataset.length), S = x(Q()), e = g(() => {
      const t = T({
        userConfig: a.config,
        defaultConfig: O
      });
      return t.theme ? {
        ...T({
          userConfig: Y.vue_ui_sparkbar[t.theme] || a.config,
          defaultConfig: t
        }),
        customPalette: Z[t.theme] || k
      } : t;
    }), V = g(() => ee(e.value.customPalette)), f = x(a.dataset.map((t) => ({
      ...t,
      value: e.value.style.animation.show ? 0 : t.value || 0,
      formatter: t.formatter || null
    }))), A = x(null);
    G(async () => {
      te(a.dataset) && L({
        componentName: "VueUiSparkbar",
        type: "dataset"
      }), D();
    });
    function D() {
      if (e.value.style.animation.show) {
        let N = function() {
          n += l / t, n < l ? (f.value = f.value.map((u, h) => ({
            ...u,
            value: u.value += o[h]
          })), A.value = requestAnimationFrame(N)) : f.value = a.dataset.map((u) => ({
            ...u,
            value: u.value || 0,
            formatter: u.formatter || null
          }));
        };
        const t = e.value.style.animation.animationFrames, o = a.dataset.map((u, h) => u.value / t), l = a.dataset.map((u) => u.value || 0).reduce((u, h) => u + h, 0);
        let n = 0;
        N();
      }
    }
    W(() => a.dataset, async (t) => {
      cancelAnimationFrame(A.value), f.value = a.dataset.map((o) => ({
        ...o,
        value: e.value.style.animation.show ? 0 : o.value || 0,
        formatter: o.formatter || null
      })), q(D);
    }, { deep: !0 });
    const i = x({
      width: 500,
      height: 16
    }), B = g(() => Math.max(...a.dataset.map((t) => t.value))), M = g(() => (a.dataset.forEach((t, o) => {
      le({
        datasetObject: t,
        requiredAttributes: ["name", "value"]
      }).forEach((l) => {
        L({
          componentName: "VueUiSparkbar",
          type: "datasetSerieAttribute",
          property: l,
          index: o
        });
      });
    }), f.value.map((t, o) => ({
      ...t,
      value: t.value || 0,
      color: ae(t.color) || V.value[o] || k[o] || k[o % k.length]
    }))));
    function U(t) {
      return t / B.value;
    }
    function F(t) {
      return e.value.style.layout.independant ? t.target ? t.value / t.target : e.value.style.layout.percentage ? t.value > 100 ? 1 : t.value / 100 : e.value.style.layout.target === 0 ? 1 : t.value / e.value.style.layout.target : U(t.value);
    }
    function m(t) {
      return e.value.style.layout.independant && t.target || e.value.style.layout.target;
    }
    function j(t, o) {
      E("selectDatapoint", { datapoint: t, index: o });
    }
    return (t, o) => (r(), v("div", {
      style: y({
        width: "100%",
        fontFamily: e.value.style.fontFamily,
        background: e.value.style.backgroundColor + s(_)[a.backgroundOpacity]
      })
    }, [
      t.$slots.title ? I(t.$slots, "title", P(H({ key: 0 }, { title: { ...t.title, title: e.value.style.title.text, subtitle: e.value.style.title.subtitle.text } }))) : d("", !0),
      !t.$slots.title && e.value.style.title.text ? (r(), v("div", {
        key: 1,
        class: "vue-ui-sparkbar-title-container",
        style: y({
          background: e.value.style.title.backgroundColor,
          margin: e.value.style.title.margin,
          textAlign: e.value.style.title.textAlign
        })
      }, [
        c("div", {
          class: "vue-ui-sparkbar-title",
          style: y({
            fontSize: e.value.style.title.fontSize + "px",
            color: e.value.style.title.color,
            fontWeight: e.value.style.title.bold ? "bold" : "normal"
          })
        }, p(e.value.style.title.text), 5),
        e.value.style.title.subtitle.text ? (r(), v("div", {
          key: 0,
          class: "vue-ui-sparkbar-subtitle",
          style: y({
            fontSize: e.value.style.title.subtitle.fontSize + "px",
            color: e.value.style.title.subtitle.color,
            fontWeight: e.value.style.title.subtitle.bold ? "bold" : "normal"
          })
        }, p(e.value.style.title.subtitle.text), 5)) : d("", !0)
      ], 4)) : d("", !0),
      (r(!0), v(z, null, X(M.value, (l, n) => (r(), v(z, null, [
        $.value ? (r(), v("div", {
          key: 0,
          style: y(`display:flex !important;${["left", "right"].includes(e.value.style.labels.name.position) ? `flex-direction: ${e.value.style.labels.name.position === "right" ? "row-reverse" : "row"} !important` : "flex-direction:column !important"};gap:${e.value.style.gap}px !important;align-items:center;${C.dataset.length > 0 && n !== C.dataset.length - 1 ? "margin-bottom:6px" : ""}`),
          onClick: () => j(l, n)
        }, [
          I(t.$slots, "data-label", P(R({ bar: {
            ...l,
            target: m(l),
            valueLabel: s(w)(
              l.formatter,
              l.value,
              s(b)({
                p: l.prefix || "",
                v: l.value,
                s: l.suffix || "",
                r: l.rounding || 0
              }),
              { datapoint: l, seriesIndex: n }
            ),
            targetLabel: s(w)(
              l.formatter,
              m(l),
              s(b)({
                p: l.prefix || "",
                v: m(l),
                s: l.suffix || "",
                r: l.rounding || 0
              }),
              { datapoint: l, seriesIndex: n }
            )
          } }))),
          t.$slots["data-label"] ? d("", !0) : (r(), v("div", {
            key: 0,
            style: y({
              width: e.value.style.labels.name.width,
              color: e.value.style.labels.name.color,
              fontSize: e.value.style.labels.fontSize + "px",
              fontWeight: e.value.style.labels.name.bold ? "bold" : "normal",
              textAlign: ["left", "right"].includes(e.value.style.labels.name.position) || ["top", "top-left"].includes(e.value.style.labels.name.position) ? "left" : e.value.style.labels.name.position === "top-center" ? "center" : "right"
            })
          }, [
            c("span", null, p(l.name), 1),
            e.value.style.labels.value.show ? (r(), v("span", {
              key: 0,
              style: y(`font-weight:${e.value.style.labels.value.bold ? "bold" : "normal"}`)
            }, ": " + p(s(w)(
              l.formatter,
              l.value,
              s(b)({
                p: l.prefix || "",
                v: l.value,
                s: l.suffix || "",
                r: l.rounding || 0
              }),
              { datapoint: l, seriesIndex: n }
            )), 5)) : d("", !0),
            e.value.style.layout.showTargetValue ? (r(), v("span", ne, p(" " + e.value.style.layout.targetValueText) + " " + p(s(w)(
              l.formatter,
              m(l),
              s(b)({
                p: l.prefix || "",
                v: m(l),
                s: l.suffix || "",
                r: l.rounding || 0
              }),
              { datapoint: l, seriesIndex: n }
            )), 1)) : d("", !0)
          ], 4)),
          (r(), v("svg", {
            xmlns: s(oe),
            viewBox: `0 0 ${i.value.width} ${i.value.height}`,
            width: "100%"
          }, [
            c("defs", null, [
              c("linearGradient", {
                x1: "0%",
                y1: "0%",
                x2: "100%",
                y2: "0%",
                id: `sparkbar_gradient_${n}_${S.value}`
              }, [
                c("stop", {
                  offset: "0%",
                  "stop-color": `${s(se)(l.color, 0.03)}${s(_)[100 - e.value.style.bar.gradient.intensity]}`
                }, null, 8, ce),
                c("stop", {
                  offset: "100%",
                  "stop-color": l.color
                }, null, 8, de)
              ], 8, ve)
            ]),
            c("rect", {
              height: i.value.height,
              width: i.value.width,
              x: 0,
              y: 0,
              fill: `${e.value.style.gutter.backgroundColor}${s(_)[e.value.style.gutter.opacity]}`,
              rx: i.value.height / 2
            }, null, 8, ye),
            c("rect", {
              height: i.value.height,
              width: i.value.width * F(l),
              x: 0,
              y: 0,
              fill: e.value.style.bar.gradient.underlayerColor,
              rx: i.value.height / 2
            }, null, 8, pe),
            c("rect", {
              height: i.value.height,
              width: i.value.width * F(l),
              x: 0,
              y: 0,
              fill: e.value.style.bar.gradient.show ? `url(#sparkbar_gradient_${n}_${S.value})` : l.color,
              rx: i.value.height / 2
            }, null, 8, fe)
          ], 8, re))
        ], 12, ie)) : d("", !0)
      ], 64))), 256)),
      $.value ? d("", !0) : (r(), J(ue, {
        key: 2,
        config: {
          type: "sparkbar",
          style: {
            backgroundColor: e.value.style.backgroundColor,
            sparkbar: {
              color: "#CCCCCC"
            }
          }
        }
      }, null, 8, ["config"]))
    ], 4));
  }
};
export {
  ke as default
};
