import { useCssVars as ce, ref as d, computed as h, watch as Y, onMounted as fe, openBlock as u, createElementBlock as r, normalizeClass as M, createElementVNode as c, normalizeStyle as i, toDisplayString as b, createCommentVNode as p, Fragment as V, renderList as E, createBlock as G, unref as v, createSlots as pe, withCtx as A, renderSlot as C, normalizeProps as be, guardReactiveProps as me, nextTick as ge } from "vue";
import { u as he, c as ye, t as _e, a as ke, p as $, b as xe, o as Ce, e as Z, g as Ie, d as Se, $ as we, i as P, f as z, q as Ae, r as $e } from "./index-Co2zPDEB.js";
import { u as j } from "./useNestedProp-BkJbU8lx.js";
import Pe from "./vue-ui-sparkline-nLSK___A.js";
import ze from "./BaseIcon-XaP3Hk56.js";
import { u as Fe, U as Oe } from "./usePrinter-B9fZzpeu.js";
import { _ as Ne } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const We = ["id"], De = { style: { "z-index": "1", "padding-right": "24px" } }, Te = ["onClick"], Me = { style: { display: "flex", "flex-direction": "row", gap: "3px", "align-items": "center" } }, Ve = ["data-cell"], Ee = {
  dir: "auto",
  style: { display: "flex", "flex-direction": "row", "align-items": "center", gap: "6px" }
}, Ge = ["data-cell", "onPointerenter"], Le = ["data-cell"], Be = ["data-cell"], Re = ["data-cell"], Ue = ["data-cell"], He = {
  key: 0,
  ref: "source",
  dir: "auto"
}, qe = {
  __name: "vue-ui-table-sparkline",
  props: {
    config: {
      type: Object,
      default() {
        return {};
      }
    },
    dataset: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  setup(ee, { expose: te }) {
    const m = ee;
    ce((t) => ({
      "2e20cf3e": t.tdo
    }));
    const { vue_ui_table_sparkline: ae } = he(), F = d(ye()), L = d(0), I = d(0), O = d(null), e = h({
      get: () => B(),
      set: (t) => t
    });
    function B() {
      const t = j({
        userConfig: m.config,
        defaultConfig: ae
      });
      return t.theme ? {
        ...j({
          userConfig: _e.vue_ui_table_sparkline[t.theme] || m.config,
          defaultConfig: t
        }),
        customPalette: ke[t.theme] || $
      } : t;
    }
    Y(() => m.config, (t) => {
      e.value = B(), X(), I.value += 1;
    }, { deep: !0 }), Y(() => m.dataset, (t) => {
      k.value = y.value, I.value += 1;
    }, { deep: !0 });
    const { isPrinting: R, isImaging: U, generatePdf: H, generateImage: q } = Fe({
      elementId: `table_${F.value}`,
      fileName: e.value.title.text || "vue-ui-table-sparkline"
    }), le = h(() => xe(e.value.customPalette)), S = d(null), N = d(!1), oe = h(() => e.value.responsiveBreakpoint);
    fe(() => {
      X();
    });
    function X() {
      Ce(m.dataset) && Z({
        componentName: "VueUiTableSparkline",
        type: "dataset"
      });
      const t = new ResizeObserver((s) => {
        s.forEach((a) => {
          N.value = a.contentRect.width < oe.value;
        });
      });
      S.value && t.observe(S.value);
    }
    const J = h(() => (m.dataset.forEach((t, s) => {
      Ie({
        datasetObject: t,
        requiredAttributes: ["name", "values"]
      }).forEach((a) => {
        Z({
          componentName: "VueUiTableSparkline",
          type: "datasetSerieAttribute",
          property: a,
          index: s
        });
      });
    }), m.dataset.map((t, s) => {
      const a = (t.values || []).map((n) => isNaN(n) ? 0 : n ?? 0), o = a.reduce((n, l) => n + l, 0);
      return {
        ...t,
        values: t.values || [],
        color: Se(t.color) || le.value[s] || $[s] || $[s % $.length],
        sum: o,
        average: o / a.length,
        median: we(a),
        sparklineDataset: a.map((n, l) => ({
          period: e.value.colNames[l] || `col ${l}`,
          value: n || 0
        }))
      };
    })));
    function ne(t) {
      const a = (t[0].values || []).map(
        (n, l) => t.map((f) => f.values[l] || [])
      ).map(
        (n) => n.map((l, f) => [l, f]).sort((l, f) => f[0] - l[0]).map((l) => l[1])
      );
      return t.map((n, l) => ({
        ...n,
        values: n.values || [],
        orders: a[l]
      }));
    }
    const y = h(() => ne(J.value)), k = d(y.value), se = h(() => Math.max(...k.value.map((t) => (t.values || []).length))), w = d(void 0), W = d(!1), D = d(void 0), T = d(-1);
    function ue() {
      W.value = !1, D.value = void 0, T.value = -1, k.value = y.value;
    }
    function re(t) {
      W.value = !0, D.value = t;
      const s = y.value.map((l) => l.values[t] || []), a = w.value === t ? 1 : -1;
      T.value = a, t === w.value ? w.value = void 0 : w.value = t;
      const n = s.map((l, f) => [f, l]).sort((l, f) => a * (f[1] - l[1])).map((l) => l[0]).map((l) => y.value[l]);
      k.value = n, I.value += 1;
    }
    const ie = h(() => Math.max(...m.dataset.map((t) => (t.values || []).length))), _ = h(() => {
      let t = e.value.colNames;
      if (!t.length)
        for (let a = 0; a < ie.value; a += 1)
          t.push(`col ${a + 1}`);
      e.value.showTotal && (t = [...t, e.value.translations.total]);
      let s;
      return e.value.showAverage && e.value.showMedian ? s = [
        ...t,
        e.value.translations.average,
        e.value.translations.median
      ] : e.value.showAverage && !e.value.showMedian ? s = [...t, e.value.translations.average] : !e.value.showAverage && e.value.showMedian ? s = [...t, e.value.translations.median] : s = t, e.value.showSparklines ? [...s, e.value.translations.chart] : s;
    }), g = d(void 0), x = d(void 0);
    function de({ dataIndex: t, serieIndex: s }) {
      g.value = t, x.value = s, O.value[t] && !N.value && O.value[t].scrollIntoView({ behavior: "smooth", block: "nearest", inline: "center" });
    }
    const K = d(!1);
    function ve(t) {
      K.value = t, L.value += 1;
    }
    function Q() {
      ge(() => {
        const t = [e.value.translations.serie].concat(_.value), s = J.value.map((n, l) => [
          [n.name],
          n.values,
          [n.sum],
          [n.average],
          [n.median]
        ]), a = [t].concat(s), o = Ae(a);
        $e({
          csvContent: o,
          title: e.value.title.text || "vue-ui-table-sparkline"
        });
      });
    }
    return te({
      generatePdf: H,
      generateImage: q,
      generateCsv: Q,
      restoreOrder: ue
    }), (t, s) => (u(), r("div", {
      ref_key: "tableContainer",
      ref: S,
      class: M({ "vue-ui-responsive": N.value }),
      style: { overflow: "hidden" },
      id: `table_${F.value}`
    }, [
      c("div", {
        style: { overflow: "auto" },
        onPointerleave: s[0] || (s[0] = (a) => {
          x.value = void 0, g.value = void 0;
        })
      }, [
        c("table", {
          class: "vue-ui-data-table",
          style: i({ fontFamily: e.value.fontFamily, position: "relative" })
        }, [
          e.value.title.text ? (u(), r("caption", {
            key: 0,
            style: i({ backgroundColor: e.value.title.backgroundColor })
          }, [
            c("div", {
              style: i({
                fontSize: `${e.value.title.fontSize}px`,
                fontWeight: e.value.title.bold ? "bold" : "normal",
                color: e.value.title.color,
                textAlign: e.value.title.textAlign
              })
            }, b(e.value.title.text), 5),
            e.value.title.subtitle.text ? (u(), r("div", {
              key: 0,
              style: i({
                fontSize: `${e.value.title.subtitle.fontSize}px`,
                fontWeight: e.value.title.subtitle.bold ? "bold" : "normal",
                color: e.value.title.subtitle.color,
                textAlign: e.value.title.textAlign
              })
            }, b(e.value.title.subtitle.text), 5)) : p("", !0)
          ], 4)) : p("", !0),
          c("thead", De, [
            c("tr", {
              role: "row",
              class: "vue-ui-data-table__thead-row",
              style: i({
                backgroundColor: e.value.thead.backgroundColor,
                color: e.value.thead.color
              })
            }, [
              c("th", {
                role: "cell",
                style: i({
                  backgroundColor: e.value.thead.backgroundColor,
                  outline: e.value.thead.outline,
                  textAlign: e.value.thead.textAlign,
                  fontWeight: e.value.thead.bold ? "bold" : "normal"
                }),
                class: "sticky-col-first"
              }, b(e.value.translations.serie), 5),
              (u(!0), r(V, null, E(_.value, (a, o) => (u(), r("th", {
                role: "cell",
                style: i({
                  background: e.value.thead.backgroundColor,
                  outline: e.value.thead.outline,
                  textAlign: e.value.thead.textAlign,
                  fontWeight: e.value.thead.bold ? "bold" : "normal",
                  minWidth: o === _.value.length - 1 ? "150px" : "",
                  cursor: y.value[0].values[o] !== void 0 ? "pointer" : "default"
                }),
                onClick: () => re(o),
                class: M({ "sticky-col": o === _.value.length - 1 && e.value.showSparklines })
              }, [
                c("div", Me, [
                  c("span", null, b(a), 1),
                  W.value && o === D.value && y.value[0].values[o] !== void 0 ? (u(), G(ze, {
                    key: 0,
                    size: 18,
                    name: T.value === 1 ? "sort" : "sortReverse",
                    stroke: e.value.thead.color
                  }, null, 8, ["name", "stroke"])) : p("", !0)
                ]),
                e.value.userOptions.show && o === _.value.length - 1 ? (u(), G(Oe, {
                  ref_for: !0,
                  ref: "details",
                  key: `user_option_${L.value}`,
                  backgroundColor: e.value.thead.backgroundColor,
                  color: e.value.thead.color,
                  isPrinting: v(R),
                  isImaging: v(U),
                  uid: F.value,
                  hasPdf: e.value.userOptions.buttons.pdf,
                  hasXls: e.value.userOptions.buttons.csv,
                  hasImg: e.value.userOptions.buttons.img,
                  hasFullscreen: e.value.userOptions.buttons.fullscreen,
                  isFullscreen: K.value,
                  titles: { ...e.value.userOptions.buttonTitles },
                  chartElement: S.value,
                  position: e.value.userOptions.position,
                  onToggleFullscreen: ve,
                  onGeneratePdf: v(H),
                  onGenerateImage: v(q),
                  onGenerateCsv: Q
                }, pe({ _: 2 }, [
                  t.$slots.optionPdf ? {
                    name: "optionPdf",
                    fn: A(() => [
                      C(t.$slots, "optionPdf", {}, void 0, !0)
                    ]),
                    key: "0"
                  } : void 0,
                  t.$slots.optionCsv ? {
                    name: "optionCsv",
                    fn: A(() => [
                      C(t.$slots, "optionCsv", {}, void 0, !0)
                    ]),
                    key: "1"
                  } : void 0,
                  t.$slots.optionImg ? {
                    name: "optionImg",
                    fn: A(() => [
                      C(t.$slots, "optionImg", {}, void 0, !0)
                    ]),
                    key: "2"
                  } : void 0,
                  t.$slots.optionFullscreen ? {
                    name: "optionFullscreen",
                    fn: A(({ toggleFullscreen: n, isFullscreen: l }) => [
                      C(t.$slots, "optionFullscreen", be(me({ toggleFullscreen: n, isFullscreen: l })), void 0, !0)
                    ]),
                    key: "3"
                  } : void 0
                ]), 1032, ["backgroundColor", "color", "isPrinting", "isImaging", "uid", "hasPdf", "hasXls", "hasImg", "hasFullscreen", "isFullscreen", "titles", "chartElement", "position", "onGeneratePdf", "onGenerateImage"])) : p("", !0)
              ], 14, Te))), 256))
            ], 4)
          ]),
          c("tbody", null, [
            (u(!0), r(V, null, E(k.value, (a, o) => (u(), r("tr", {
              role: "row",
              style: i({
                backgroundColor: e.value.tbody.backgroundColor,
                color: e.value.tbody.color
              }),
              class: M({ "vue-ui-data-table__tbody__row": !0, "vue-ui-data-table__tbody__row-even": o % 2 === 0, "vue-ui-data-table__tbody__row-odd": o % 2 !== 0 })
            }, [
              c("td", {
                role: "cell",
                style: i({
                  backgroundColor: e.value.tbody.backgroundColor,
                  border: e.value.tbody.outline,
                  fontSize: `${e.value.tbody.fontSize}px`,
                  fontWeight: e.value.tbody.bold ? "bold" : "normal",
                  textAlign: e.value.tbody.textAlign
                }),
                "data-cell": e.value.translations.serie,
                class: "vue-ui-data-table__tbody__td sticky-col-first"
              }, [
                c("div", Ee, [
                  e.value.tbody.showColorMarker ? (u(), r("span", {
                    key: 0,
                    style: i({ color: a.color })
                  }, "⬤", 4)) : p("", !0),
                  c("span", null, b(a.name ?? "-"), 1)
                ])
              ], 12, Ve),
              (u(!0), r(V, null, E(se.value, (n, l) => (u(), r("td", {
                dir: "auto",
                role: "cell",
                ref_for: !0,
                ref_key: "TD",
                ref: O,
                style: i({
                  border: e.value.tbody.outline,
                  fontSize: `${e.value.tbody.fontSize}px`,
                  fontWeight: e.value.tbody.bold ? "bold" : "normal",
                  textAlign: e.value.tbody.textAlign,
                  background: g.value !== void 0 && l === g.value ? e.value.tbody.selectedColor.useSerieColor ? `${a.color.length > 7 ? a.color.slice(0, -2) : a.color}33` : e.value.tbody.selectedColor.fallback : "",
                  borderRadius: g.value !== void 0 && x.value !== void 0 && l === g.value && x.value === o ? "3px" : ""
                }),
                "data-cell": _.value[l],
                class: "vue-ui-data-table__tbody__td",
                onPointerenter: (f) => {
                  x.value = o, g.value = l;
                }
              }, b([null, void 0].includes(a.values[l]) ? "-" : v(P)(
                e.value.formatter,
                Number(a.values[l]),
                v(z)({
                  p: e.value.prefix,
                  v: Number(a.values[l]),
                  s: e.value.suffix,
                  r: e.value.roundingValues
                }),
                { datapoint: a, seriesIndex: o, datapointIndex: l }
              )), 45, Ge))), 256)),
              e.value.showTotal ? (u(), r("td", {
                key: 0,
                dir: "auto",
                role: "cell",
                style: i({
                  border: e.value.tbody.outline,
                  fontSize: `${e.value.tbody.fontSize}px`,
                  fontWeight: e.value.tbody.bold ? "bold" : "normal",
                  textAlign: e.value.tbody.textAlign
                }),
                "data-cell": e.value.translations.total,
                class: "vue-ui-data-table__tbody__td"
              }, b(v(P)(
                e.value.formatter,
                a.sum,
                v(z)({
                  p: e.value.prefix,
                  v: a.sum,
                  s: e.value.suffix,
                  r: e.value.roundingTotal
                }),
                { datapoint: a.sum, seriesIndex: o }
              )), 13, Le)) : p("", !0),
              e.value.showAverage ? (u(), r("td", {
                key: 1,
                dir: "auto",
                role: "cell",
                style: i({
                  border: e.value.tbody.outline,
                  fontSize: `${e.value.tbody.fontSize}px`,
                  fontWeight: e.value.tbody.bold ? "bold" : "normal",
                  textAlign: e.value.tbody.textAlign
                }),
                "data-cell": e.value.translations.average,
                class: "vue-ui-data-table__tbody__td"
              }, b(v(P)(
                e.value.formatter,
                a.average,
                v(z)({
                  p: e.value.prefix,
                  v: a.average,
                  s: e.value.suffix,
                  r: e.value.roundingAverage
                }),
                { datapoint: a.average, seriesIndex: o }
              )), 13, Be)) : p("", !0),
              e.value.showMedian ? (u(), r("td", {
                key: 2,
                dir: "auto",
                role: "cell",
                style: i({
                  border: e.value.tbody.outline,
                  fontSize: `${e.value.tbody.fontSize}px`,
                  fontWeight: e.value.tbody.bold ? "bold" : "normal",
                  textAlign: e.value.tbody.textAlign
                }),
                "data-cell": e.value.translations.median,
                class: "vue-ui-data-table__tbody__td"
              }, b(v(P)(
                e.value.formatter,
                a.median,
                v(z)({
                  p: e.value.prefix,
                  v: a.median,
                  s: e.value.suffix,
                  r: e.value.roundingMedian
                }),
                { datapoint: a.median, seriesIndex: o }
              )), 13, Re)) : p("", !0),
              e.value.showSparklines ? (u(), r("td", {
                key: 3,
                role: "cell",
                "data-cell": e.value.translations.chart,
                style: i({
                  border: e.value.tbody.outline,
                  fontSize: `${e.value.tbody.fontSize}px`,
                  fontWeight: e.value.tbody.bold ? "bold" : "normal",
                  textAlign: e.value.tbody.textAlign,
                  backgroundColor: e.value.tbody.backgroundColor
                }),
                class: "vue-ui-data-table__tbody__td sticky-col"
              }, [
                (u(), G(Pe, {
                  key: `sparkline_${o}_${I.value}`,
                  onHoverIndex: ({ index: n }) => de({ dataIndex: n, serieIndex: o }),
                  dataset: a.sparklineDataset,
                  showInfo: !1,
                  selectedIndex: g.value,
                  config: {
                    type: e.value.sparkline.type,
                    style: {
                      backgroundColor: e.value.tbody.backgroundColor,
                      animation: {
                        show: e.value.sparkline.animation.show && !v(R) && !v(U),
                        animationFrames: e.value.sparkline.animation.animationFrames
                      },
                      line: {
                        color: a.color,
                        smooth: e.value.sparkline.smooth,
                        strokeWidth: e.value.sparkline.strokeWidth
                      },
                      bar: {
                        color: a.color
                      },
                      area: {
                        color: a.color,
                        opacity: e.value.sparkline.showArea ? 16 : 0,
                        useGradient: e.value.sparkline.useGradient
                      },
                      verticalIndicator: {
                        color: a.color
                      },
                      plot: {
                        radius: 9,
                        stroke: e.value.tbody.backgroundColor,
                        strokeWidth: 3
                      }
                    }
                  }
                }, null, 8, ["onHoverIndex", "dataset", "selectedIndex", "config"]))
              ], 12, Ue)) : p("", !0)
            ], 6))), 256))
          ])
        ], 4)
      ], 32),
      t.$slots.source ? (u(), r("div", He, [
        C(t.$slots, "source", {}, void 0, !0)
      ], 512)) : p("", !0)
    ], 10, We));
  }
}, et = /* @__PURE__ */ Ne(qe, [["__scopeId", "data-v-b0a2f2ee"]]);
export {
  et as default
};
