import { computed as g, ref as x, watch as z, onMounted as q, nextTick as H, openBlock as n, createElementBlock as v, normalizeStyle as f, unref as s, renderSlot as b, normalizeProps as L, mergeProps as X, createCommentVNode as c, createElementVNode as d, toDisplayString as p, 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 $, i as w, f as _, X as ue, w as ne } from "./index-Cm2_6s9o.js";
import { u as V } from "./useNestedProp-BYPZJNhi.js";
import ie from "./vue-ui-skeleton-BUS6A44x.js";
import { _ as re } from "./PackageVersion-CTMY4zeY.js";
const ve = ["onClick"], ce = { key: 1 }, de = ["xmlns", "viewBox"], fe = ["id"], pe = ["stop-color"], ye = ["stop-color"], me = ["height", "width", "fill", "rx"], ge = ["height", "width", "fill", "rx"], he = ["height", "width", "fill", "rx"], xe = {
  key: 2,
  ref: "source",
  dir: "auto"
}, $e = {
  __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(), S = g(() => !!a.dataset && a.dataset.length), A = x(Z()), e = g({
      get: () => N(),
      set: (t) => t
    });
    function N() {
      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;
    }
    z(() => a.config, (t) => {
      e.value = N();
    }, { 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
    }))), D = x(null);
    q(async () => {
      ae(a.dataset) && T({
        componentName: "VueUiSparkbar",
        type: "dataset"
      }), F();
    });
    function F() {
      if (e.value.style.animation.show) {
        let P = function() {
          r += l / t, r < l ? (y.value = y.value.map((u, h) => ({
            ...u,
            value: u.value += o[h]
          })), D.value = requestAnimationFrame(P)) : 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 r = 0;
        P();
      }
    }
    z(() => a.dataset, async (t) => {
      cancelAnimationFrame(D.value), y.value = a.dataset.map((o) => ({
        ...o,
        value: e.value.style.animation.show ? 0 : o.value || 0,
        formatter: o.formatter || null
      })), H(F);
    }, { deep: !0 });
    const i = 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 I(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) => (n(), v("div", {
      style: f({
        width: "100%",
        fontFamily: e.value.style.fontFamily,
        background: a.backgroundOpacity !== null ? s($)(e.value.style.backgroundColor, a.backgroundOpacity) : e.value.style.backgroundColor
      })
    }, [
      t.$slots.title ? b(t.$slots, "title", L(X({ key: 0 }, { title: { ...t.title, title: e.value.style.title.text, subtitle: e.value.style.title.subtitle.text } }))) : c("", !0),
      !t.$slots.title && e.value.style.title.text ? (n(), v("div", {
        key: 1,
        class: "vue-ui-sparkbar-title-container",
        style: f({
          background: e.value.style.title.backgroundColor,
          margin: e.value.style.title.margin,
          textAlign: e.value.style.title.textAlign
        })
      }, [
        d("div", {
          class: "vue-ui-sparkbar-title",
          style: f({
            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 ? (n(), v("div", {
          key: 0,
          class: "vue-ui-sparkbar-subtitle",
          style: f({
            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)) : c("", !0)
      ], 4)) : c("", !0),
      (n(!0), v(O, null, R(j.value, (l, r) => (n(), v(O, null, [
        S.value ? (n(), v("div", {
          key: 0,
          style: f(`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 && r !== C.dataset.length - 1 ? "margin-bottom:6px" : ""}`),
          onClick: () => W(l, r)
        }, [
          b(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: r }
            ),
            targetLabel: s(w)(
              l.formatter,
              m(l),
              s(_)({
                p: l.prefix || "",
                v: m(l),
                s: l.suffix || "",
                r: l.rounding || 0
              }),
              { datapoint: l, seriesIndex: r }
            )
          } }))),
          t.$slots["data-label"] ? c("", !0) : (n(), v("div", {
            key: 0,
            style: f({
              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"
            })
          }, [
            d("span", null, p(l.name), 1),
            e.value.style.labels.value.show ? (n(), v("span", {
              key: 0,
              style: f(`font-weight:${e.value.style.labels.value.bold ? "bold" : "normal"}`)
            }, ": " + p(s(w)(
              l.formatter,
              l.value,
              s(_)({
                p: l.prefix || "",
                v: l.value,
                s: l.suffix || "",
                r: l.rounding || 0
              }),
              { datapoint: l, seriesIndex: r }
            )), 5)) : c("", !0),
            e.value.style.layout.showTargetValue ? (n(), v("span", ce, p(" " + e.value.style.layout.targetValueText) + " " + p(s(w)(
              l.formatter,
              m(l),
              s(_)({
                p: l.prefix || "",
                v: m(l),
                s: l.suffix || "",
                r: l.rounding || 0
              }),
              { datapoint: l, seriesIndex: r }
            )), 1)) : c("", !0)
          ], 4)),
          (n(), v("svg", {
            xmlns: s(ue),
            viewBox: `0 0 ${i.value.width} ${i.value.height}`,
            width: "100%"
          }, [
            K(re),
            d("defs", null, [
              d("linearGradient", {
                x1: "0%",
                y1: "0%",
                x2: "100%",
                y2: "0%",
                id: `sparkbar_gradient_${r}_${A.value}`
              }, [
                d("stop", {
                  offset: "0%",
                  "stop-color": s($)(s(ne)(l.color, 0.03), 100 - e.value.style.bar.gradient.intensity)
                }, null, 8, pe),
                d("stop", {
                  offset: "100%",
                  "stop-color": l.color
                }, null, 8, ye)
              ], 8, fe)
            ]),
            d("rect", {
              height: i.value.height,
              width: i.value.width,
              x: 0,
              y: 0,
              fill: s($)(e.value.style.gutter.backgroundColor, e.value.style.gutter.opacity),
              rx: i.value.height / 2
            }, null, 8, me),
            d("rect", {
              height: i.value.height,
              width: i.value.width * I(l),
              x: 0,
              y: 0,
              fill: e.value.style.bar.gradient.underlayerColor,
              rx: i.value.height / 2
            }, null, 8, ge),
            d("rect", {
              height: i.value.height,
              width: i.value.width * I(l),
              x: 0,
              y: 0,
              fill: e.value.style.bar.gradient.show ? `url(#sparkbar_gradient_${r}_${A.value})` : l.color,
              rx: i.value.height / 2
            }, null, 8, he)
          ], 8, de))
        ], 12, ve)) : c("", !0)
      ], 64))), 256)),
      t.$slots.source ? (n(), v("div", xe, [
        b(t.$slots, "source")
      ], 512)) : c("", !0),
      S.value ? c("", !0) : (n(), Q(ie, {
        key: 3,
        config: {
          type: "sparkbar",
          style: {
            backgroundColor: e.value.style.backgroundColor,
            sparkbar: {
              color: "#CCCCCC"
            }
          }
        }
      }, null, 8, ["config"]))
    ], 4));
  }
};
export {
  $e as default
};
