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