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