import { useCssVars as Q, ref as h, computed as c, watch as Y, onMounted as Z, openBlock as n, createElementBlock as l, normalizeStyle as I, normalizeClass as E, createBlock as j, unref as g, createSlots as ee, withCtx as C, renderSlot as r, normalizeProps as p, guardReactiveProps as f, createCommentVNode as v, createElementVNode as y, Fragment as _, renderList as w, createVNode as te, mergeProps as G, nextTick as ae } from "vue";
import { u as oe, c as M, t as se, o as ne, e as le, $ as re, G as ue, N as ie, q as de, r as ce } from "./index-Cm2_6s9o.js";
import { u as S } from "./useNestedProp-BYPZJNhi.js";
import { u as pe, U as ve } from "./usePrinter-Cx8U1e30.js";
import { _ as me } from "./Shape-C-sY_hlc.js";
import { _ as fe } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const he = ["id"], ge = { role: "cell" }, ye = { role: "row" }, be = ["data-cell"], Ce = {
  key: 0,
  style: { display: "flex", "flex-direction": "row", gap: "2px", "align-items": "center" }
}, _e = ["height", "width"], ke = {
  key: 0,
  role: "cell",
  "data-cell": "sum"
}, $e = {
  key: 1,
  role: "cell",
  "data-cell": "average"
}, Ie = {
  key: 2,
  role: "cell",
  "data-cell": "median"
}, we = {
  key: 1,
  ref: "source",
  dir: "auto"
}, xe = {
  __name: "vue-ui-table-heatmap",
  props: {
    config: {
      type: Object,
      default() {
        return {};
      }
    },
    dataset: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  setup(B, { expose: U }) {
    const m = B;
    Q((e) => ({
      "4e1c0e72": J.value,
      "6ed4bc22": q.value
    }));
    const { vue_ui_table_heatmap: A } = oe(), k = h(M()), u = h(!1), b = h(null), $ = h(!1), x = h(0), t = c({
      get: () => F(),
      set: (e) => e
    });
    function F() {
      const e = S({
        userConfig: m.config,
        defaultConfig: A
      });
      return e.theme ? {
        ...S({
          userConfig: se.vue_ui_table_heatmap[e.theme] || m.config,
          defaultConfig: e
        })
      } : e;
    }
    Y(() => m.config, (e) => {
      t.value = F(), O();
    }, { deep: !0 });
    const { isPrinting: D, isImaging: H, generatePdf: P, generateImage: N } = pe({
      elementId: `table_heatmap_${k.value}`,
      fileName: "vue-ui-table-heatmap"
    }), L = c(() => t.value.table.responsiveBreakpoint), X = c(() => !!m.dataset && m.dataset.length);
    Z(() => {
      O();
    });
    function O() {
      ne(m.dataset) && le({
        componentName: "VueUiTableHeatmap",
        type: "dataset"
      });
      const e = new ResizeObserver((s) => {
        s.forEach((a) => {
          u.value = a.contentRect.width < L.value;
        });
      });
      b.value && e.observe(b.value);
    }
    const R = c(() => m.dataset.map((e) => {
      const s = e.values.map((o) => isNaN(o) ? 0 : o), a = s.reduce((o, d) => o + d, 0);
      return {
        ...e,
        values: s,
        serieExtremes: {
          max: Math.max(...s),
          min: Math.min(...s)
        },
        sum: a,
        average: a / s.length,
        median: re(s),
        displayValues: [e.name, ...e.values],
        id: M()
      };
    })), T = c(() => {
      const e = R.value.flatMap((s) => s.values);
      return {
        min: Math.min(...e),
        max: Math.max(...e)
      };
    });
    function W(e, s) {
      const a = t.value.style.heatmapColors.useIndividualScale;
      return ie(t.value.style.heatmapColors.min, t.value.style.heatmapColors.max, a ? s.min : T.value.min, a ? s.max : T.value.max, e);
    }
    const V = c(() => R.value.map((e) => ({
      ...e,
      colors: e.displayValues.map((s) => isNaN(s) ? t.value.style.backgroundColor : W(s, e.serieExtremes))
    }))), q = c(() => t.value.style.backgroundColor), J = c(() => `${t.value.table.borderWidth}px`);
    function z() {
      ae(() => {
        const e = V.value.map((o) => [
          [o.name],
          o.displayValues,
          [o.sum],
          [o.average],
          [o.median]
        ]), s = [[[""], t.value.table.head.values, ["sum"], ["average"], ["median"]]].concat(e), a = de(s);
        ce({ csvContent: a, title: "vue-ui-table-heatmap" });
      });
    }
    function K(e) {
      $.value = e, x.value += 1;
    }
    return U({
      generatePdf: P,
      generateCsv: z,
      generateImage: N
    }), (e, s) => (n(), l("div", {
      ref_key: "tableContainer",
      ref: b,
      style: I(`width:100%; overflow-x:auto; container-type: inline-size;padding-top:${t.value.userOptions.show ? "36px" : ""}; ${$.value ? "vue-data-ui-wrapper-fullscreen" : ""}; position:relative;`),
      class: E({ "vue-ui-responsive": u.value }),
      id: `table_heatmap_${k.value}`
    }, [
      t.value.userOptions.show && X.value ? (n(), j(ve, {
        ref: "details",
        key: `user_option_${x.value}`,
        backgroundColor: t.value.style.backgroundColor,
        color: t.value.style.color,
        isPrinting: g(D),
        isImaging: g(H),
        uid: k.value,
        hasPdf: t.value.userOptions.buttons.pdf,
        hasXls: t.value.userOptions.buttons.csv,
        hasImg: t.value.userOptions.buttons.img,
        hasFullscreen: t.value.userOptions.buttons.fullscreen,
        isFullscreen: $.value,
        titles: { ...t.value.userOptions.buttonTitles },
        chartElement: b.value,
        position: t.value.userOptions.position,
        onToggleFullscreen: K,
        onGeneratePdf: g(P),
        onGenerateCsv: z,
        onGenerateImage: g(N)
      }, ee({ _: 2 }, [
        e.$slots.optionPdf ? {
          name: "optionPdf",
          fn: C(() => [
            r(e.$slots, "optionPdf", {}, void 0, !0)
          ]),
          key: "0"
        } : void 0,
        e.$slots.optionCsv ? {
          name: "optionCsv",
          fn: C(() => [
            r(e.$slots, "optionCsv", {}, void 0, !0)
          ]),
          key: "1"
        } : void 0,
        e.$slots.optionImg ? {
          name: "optionImg",
          fn: C(() => [
            r(e.$slots, "optionImg", {}, void 0, !0)
          ]),
          key: "2"
        } : void 0,
        e.$slots.optionFullscreen ? {
          name: "optionFullscreen",
          fn: C(({ toggleFullscreen: a, isFullscreen: o }) => [
            r(e.$slots, "optionFullscreen", p(f({ toggleFullscreen: a, isFullscreen: o })), void 0, !0)
          ]),
          key: "3"
        } : void 0
      ]), 1032, ["backgroundColor", "color", "isPrinting", "isImaging", "uid", "hasPdf", "hasXls", "hasImg", "hasFullscreen", "isFullscreen", "titles", "chartElement", "position", "onGeneratePdf", "onGenerateImage"])) : v("", !0),
      y("table", {
        class: E({ "vue-ui-table-heatmap": !0 }),
        style: I(`width:100%;font-family:${t.value.style.fontFamily};background:${t.value.style.backgroundColor};`)
      }, [
        y("caption", null, [
          r(e.$slots, "caption", {}, void 0, !0)
        ]),
        y("thead", null, [
          y("tr", {
            role: "row",
            style: I(`background:${t.value.table.head.backgroundColor};color:${t.value.table.head.color}`)
          }, [
            (n(!0), l(_, null, w(t.value.table.head.values, (a, o) => (n(), l("th", ge, [
              r(e.$slots, "head", p(f({ value: a, rowIndex: o, type: typeof a, isResponsive: u.value })), void 0, !0)
            ]))), 256))
          ], 4)
        ]),
        y("tbody", null, [
          (n(!0), l(_, null, w(V.value, (a, o) => (n(), l("tr", ye, [
            (n(!0), l(_, null, w(a.displayValues, (d, i) => (n(), l("td", {
              role: "cell",
              "data-cell": t.value.table.head.values[i]
            }, [
              a.color && i === 0 ? (n(), l("div", Ce, [
                a.color ? (n(), l("svg", {
                  key: 0,
                  height: t.value.style.shapeSize,
                  width: t.value.style.shapeSize,
                  viewBox: "0 0 20 20",
                  style: { background: "none", overflow: "visible" }
                }, [
                  te(me, {
                    plot: { x: 10, y: 10 },
                    color: a.color,
                    radius: 9,
                    shape: a.shape || "circle"
                  }, null, 8, ["color", "shape"])
                ], 8, _e)) : v("", !0),
                r(e.$slots, "rowTitle", p(f({ value: d, rowIndex: o, colIndex: i, type: typeof d, isResponsive: u.value })), void 0, !0)
              ])) : (n(), l(_, { key: 1 }, [
                i === 0 ? r(e.$slots, "rowTitle", p(G({ key: 0 }, { value: d, rowIndex: o, colIndex: i, type: typeof d, isResponsive: u.value })), void 0, !0) : v("", !0),
                i > 0 ? r(e.$slots, "cell", p(G({ key: 1 }, { value: d, rowIndex: o, colIndex: i, type: typeof d, isResponsive: u.value, color: a.colors[i], textColor: g(ue)(a.colors[i]) })), void 0, !0) : v("", !0)
              ], 64))
            ], 8, be))), 256)),
            t.value.table.showSum ? (n(), l("td", ke, [
              r(e.$slots, "sum", p(f({ value: a.sum, rowIndex: o, isResponsive: u.value })), void 0, !0)
            ])) : v("", !0),
            t.value.table.showAverage ? (n(), l("td", $e, [
              r(e.$slots, "average", p(f({ value: a.average, rowIndex: o, isResponsive: u.value })), void 0, !0)
            ])) : v("", !0),
            t.value.table.showMedian ? (n(), l("td", Ie, [
              r(e.$slots, "median", p(f({ value: a.median, rowIndex: o, isResponsive: u.value })), void 0, !0)
            ])) : v("", !0)
          ]))), 256))
        ])
      ], 4),
      e.$slots.source ? (n(), l("div", we, [
        r(e.$slots, "source", {}, void 0, !0)
      ], 512)) : v("", !0)
    ], 14, he));
  }
}, Ve = /* @__PURE__ */ fe(xe, [["__scopeId", "data-v-393e4e2f"]]);
export {
  Ve as default
};
