import { jsx as m, jsxs as A, Fragment as ze } from "react/jsx-runtime";
import { useRef as be, useState as ve } from "react";
import { Container as Ae } from "semantic-ui-react";
import Be from "../data/DataProvider.js";
import Le from "../data/DataConsumer.js";
import { connect as Te } from "react-redux";
import { alphaSort as xe } from "../utils/common.js";
import { formatContent as _e } from "../common/ChartTooltip.js";
const He = "No data matches your selection", Re = "-", Ee = "#5a5d68", Ue = "none", $e = 14, ke = "#3182ce", Ve = "none", de = 24, R = (o, l) => l ? o : decodeURIComponent(o), ne = (o, l) => {
  try {
    return JSON.parse(R(o, l));
  } catch (s) {
    return console.error("Error parsing value:", o, s), null;
  }
}, we = (o) => o ? {
  style: o.style === "compacted" ? "decimal" : o.style,
  notation: o.style === "compacted" ? "compact" : "standard",
  currency: o.currency,
  minimumFractionDigits: parseInt(o.minimumFractionDigits, 10),
  maximumFractionDigits: parseInt(o.maximumFractionDigits, 10),
  prefix: o.prefix || "",
  suffix: o.suffix || ""
} : {
  notation: "standard",
  currency: "USD",
  minimumFractionDigits: 2,
  maximumFractionDigits: 2,
  prefix: "",
  suffix: ""
}, Oe = (o, l, s, b = !0) => {
  const i = [];
  if (!o) return i;
  const f = o[s];
  if (f && typeof f == "object" && Object.entries(f).forEach(([p, w]) => {
    if (w && w.selected) {
      const E = b ? we(w.format || l) : l, I = w && typeof w.customLabel == "string" && w.customLabel.trim().length > 0 ? w.customLabel.trim() : p;
      i.push({ name: p, format: E, label: I });
    }
  }), i.length === 0) {
    const p = (Array.isArray(o), o[0]);
    p && i.push({ name: p, format: l, label: p });
  }
  return i;
}, Ge = (o, l) => {
  const s = {}, b = o || [];
  return b.forEach && b.forEach((i) => {
    var f;
    ((f = i.value) == null ? void 0 : f.filter((p) => p != null && p.toString().trim() !== "").length) > 0 && (s[i.param] = i.value);
  }), l && (s.dvzProxyDatasetId = l), s;
}, Pe = (o) => {
  const l = [];
  return o && o !== "none" && l.push(o), l;
}, Ze = ({
  lineColor: o,
  textColor: l = "#ffffff",
  fontSizePx: s = "14px",
  showLabel: b = !1,
  label: i = "",
  showValue: f = !1,
  valueString: p = ""
}) => /* @__PURE__ */ A("div", { style: { display: "flex", alignItems: "center", height: "100%" }, children: [
  /* @__PURE__ */ m(
    "div",
    {
      className: "grouped-bar-zero-line",
      style: {
        width: "2px",
        backgroundColor: o,
        height: "100%"
      }
    }
  ),
  b && /* @__PURE__ */ m("span", { style: {
    color: l,
    fontSize: s,
    fontWeight: "500",
    whiteSpace: "nowrap",
    marginLeft: "8px"
  }, children: i }),
  f && /* @__PURE__ */ m("span", { style: {
    color: l,
    fontSize: s,
    fontWeight: "500",
    whiteSpace: "nowrap",
    marginLeft: b ? "6px" : "8px"
  }, children: p })
] }), We = ({ textColor: o, message: l = "No data to display" }) => /* @__PURE__ */ m("div", { className: "grouped-bars-data-frame", children: /* @__PURE__ */ m("div", { className: "no-data-text", style: { color: o }, children: l }) }), je = ({
  dimensionValue: o,
  measureEntries: l,
  mainEntry: s,
  barBackgroundColor: b,
  textColor: i,
  measureTextColor: f,
  fontSize: p,
  mainValueFontSize: w,
  labelPosition: E,
  labelWidth: I,
  labelHeight: U,
  labelFormat: $,
  vars: k,
  intl: B,
  valuePosition: L,
  format: y,
  showMeasureLabels: V = !1
}) => {
  var S, j;
  const T = {
    fontSize: p + "px",
    color: i,
    width: `${I}%`,
    flex: `0 0 ${I}%`,
    whiteSpace: "normal",
    wordBreak: "break-word",
    overflow: "hidden",
    lineHeight: "1.2",
    display: "flex",
    alignItems: "center",
    height: U + "px"
  }, W = decodeURIComponent($ || ""), _ = _e(W, k || { value: o }, B), O = Array.isArray(l) && l.length === 1 && !s, G = L === "top" && O ? /* @__PURE__ */ A(
    "div",
    {
      className: "grouped-bar-measure",
      style: {
        fontSize: (w || de) + "px",
        color: i,
        whiteSpace: "nowrap",
        flex: "1",
        textAlign: "right"
      },
      children: [
        y.prefix,
        new Intl.NumberFormat(B.locale, y).format(l[0].value),
        y.suffix
      ]
    }
  ) : null, P = /* @__PURE__ */ m("div", { style: { flex: "1", display: "flex", flexDirection: "column", gap: "6px", minWidth: 0 }, children: l.map((c, Q) => {
    var Z, C, J, N;
    return /* @__PURE__ */ m(
      "div",
      {
        className: "grouped-bar-bar-container",
        style: {
          backgroundColor: b,
          height: "28px",
          borderRadius: "8px",
          overflow: "hidden",
          position: "relative",
          display: "flex",
          alignItems: "center"
        },
        children: c.width > 0 ? /* @__PURE__ */ m(
          "div",
          {
            className: "grouped-bar-bar",
            style: {
              width: c.width + "%",
              backgroundColor: c.color,
              height: "100%",
              display: "flex",
              alignItems: "center",
              paddingLeft: "8px",
              paddingRight: "8px"
            },
            children: /* @__PURE__ */ A("span", { style: { color: f || "#ffffff", fontSize: p + "px", fontWeight: "500", whiteSpace: "nowrap" }, children: [
              V ? /* @__PURE__ */ A(ze, { children: [
                c.label || c.name,
                c.label && c.label !== c.name ? " " : ": "
              ] }) : null,
              L === "bar" ? `${((Z = c.format) == null ? void 0 : Z.prefix) || ""}${new Intl.NumberFormat(B.locale, c.format || y).format(c.value)}${((C = c.format) == null ? void 0 : C.suffix) || ""}` : `${(c.width || 0).toFixed(1)}%`
            ] })
          }
        ) : /* @__PURE__ */ m(
          Ze,
          {
            lineColor: c.color,
            textColor: f || "#ffffff",
            fontSizePx: p + "px",
            showLabel: !!V,
            label: c.label || c.name,
            showValue: !0,
            valueString: `${((J = c.format) == null ? void 0 : J.prefix) || ""}${new Intl.NumberFormat(B.locale, c.format || y).format(c.value)}${((N = c.format) == null ? void 0 : N.suffix) || ""}`
          }
        )
      },
      `${o}-${c.name}-${Q}`
    );
  }) }), H = s ? /* @__PURE__ */ A(
    "div",
    {
      className: "grouped-bar-main",
      style: {
        flex: "0 0 140px",
        backgroundColor: s.color || b,
        borderRadius: "8px",
        padding: "10px",
        display: "flex",
        flexDirection: "column",
        alignItems: "center",
        justifyContent: "center",
        minWidth: 0
      },
      children: [
        /* @__PURE__ */ A("span", { style: {
          color: f || i,
          fontSize: (w || de) + "px",
          fontWeight: 600,
          lineHeight: 1,
          whiteSpace: "nowrap"
        }, children: [
          ((S = s.format) == null ? void 0 : S.prefix) || "",
          new Intl.NumberFormat(B.locale, s.format || y).format(s.value),
          ((j = s.format) == null ? void 0 : j.suffix) || ""
        ] }),
        /* @__PURE__ */ m("span", { style: {
          color: f || i,
          fontSize: p + "px",
          fontWeight: 500,
          lineHeight: 1.2,
          marginTop: 6,
          textAlign: "center",
          whiteSpace: "nowrap"
        }, children: s.label || s.name })
      ]
    }
  ) : null;
  return E === "left" ? /* @__PURE__ */ m("div", { className: "grouped-bar-item", style: { marginBottom: "12px" }, children: /* @__PURE__ */ A("div", { style: { display: "flex", alignItems: "flex-start", gap: "12px" }, children: [
    /* @__PURE__ */ m("div", { className: "grouped-bar-label", style: T, dangerouslySetInnerHTML: { __html: _ } }),
    H,
    P
  ] }) }) : /* @__PURE__ */ A("div", { className: "grouped-bar-item", style: { marginBottom: "12px" }, children: [
    /* @__PURE__ */ A("div", { style: { display: "flex", alignItems: "flex-start", gap: "12px", marginBottom: "6px" }, children: [
      /* @__PURE__ */ m("div", { className: "grouped-bar-label", style: T, dangerouslySetInnerHTML: { __html: _ } }),
      G
    ] }),
    /* @__PURE__ */ A("div", { style: { display: "flex", alignItems: "stretch", gap: "12px" }, children: [
      H,
      P
    ] })
  ] });
}, Je = (o) => {
  const {
    app: l,
    measure: s,
    dimension1: b,
    data: i,
    format: f,
    textColor: p,
    measureTextColor: w,
    fontSize: E,
    mainValueFontSize: I,
    intl: U,
    manualColors: $,
    defaultBarColor: k,
    barBackgroundColor: B,
    labelPosition: L,
    valuePosition: y,
    labelWidth: V,
    labelHeight: T,
    labelFormat: W,
    sorting: _,
    sortDirection: O,
    topN: G,
    barSizeCriteria: P,
    selectedMeasures: H,
    mainMeasureName: S,
    sortMeasureName: j,
    barSizeUseGroup: c = !1,
    showMeasureLabels: Q = !1,
    onHeightChange: Z
  } = o, C = be({ last: null, timer: null }), J = () => {
    if (!i) return { dataItems: [], measureField: null, dimensionField: null };
    if (l === "csv") {
      const { data: e, meta: { fields: t } } = i, r = t[0], a = t[1];
      return { dataItems: i.data.map((h) => ({
        value: h[r],
        [a]: h[a],
        [r]: h[r]
      })), measureField: a, dimensionField: r };
    } else {
      const e = i.children || [], t = s, r = b;
      return { dataItems: e.map((u) => ({
        value: u.value,
        [t]: u[t],
        [r]: u.value,
        vars: { ...u }
      })), measureField: t, dimensionField: r };
    }
  }, { dataItems: N, measureField: K, dimensionField: D } = J(), g = H && H.length > 0 ? H : K ? [{ name: K, format: f }] : s ? [{ name: s, format: f }] : [];
  let x;
  if (_ === "measure") {
    const e = S || j || K || (g[0] ? g[0].name : null);
    x = N.sort((t, r) => {
      const a = e ? (t.vars && t.vars[e]) ?? t[e] ?? 0 : 0, u = e ? (r.vars && r.vars[e]) ?? r[e] ?? 0 : 0;
      return O === "asc" ? a - u : u - a;
    });
  } else _ === "dimension" ? x = N.sort((e, t) => O === "asc" ? xe(!1, U.locale, e.value, t.value) : xe(!0, U.locale, e.value, t.value)) : x = N;
  if (G && !isNaN(parseInt(G))) {
    const e = parseInt(G);
    if (e > 0)
      if (g.length <= 1)
        x = x.slice(0, e);
      else if (S) {
        const t = [...N].sort((a, u) => {
          const h = ((a.vars && a.vars[S]) ?? a[S] ?? 0) || 0;
          return (((u.vars && u.vars[S]) ?? u[S] ?? 0) || 0) - h;
        }), r = new Set(
          t.slice(0, e).map((a) => a[D])
        );
        x = x.filter((a) => r.has(a[D]));
      } else {
        const t = [...N].sort((a, u) => {
          const h = g.reduce((n, v) => {
            const d = ((a.vars && a.vars[v.name]) ?? a[v.name] ?? 0) || 0;
            return n + d;
          }, 0);
          return g.reduce((n, v) => {
            const d = ((u.vars && u.vars[v.name]) ?? u[v.name] ?? 0) || 0;
            return n + d;
          }, 0) - h;
        }), r = new Set(
          t.slice(0, e).map((a) => a[D])
        );
        x = x.filter((a) => r.has(a[D]));
      }
  }
  const Y = {};
  g.forEach((e) => {
    Y[e.name] = x.reduce((t, r) => {
      const a = (r.vars && r.vars[e.name]) ?? r[e.name] ?? 0;
      return t + (a || 0);
    }, 0);
  });
  const X = Math.max(
    0,
    ...g.flatMap((e) => x.map((t) => (t.vars && t.vars[e.name]) ?? t[e.name] ?? 0))
  ), se = Object.values(Y).reduce((e, t) => e + t, 0), ee = {};
  x.forEach((e) => {
    const t = e[D];
    ee[t] = 0, g.forEach((r) => {
      const a = (e.vars && e.vars[r.name]) ?? e[r.name] ?? 0;
      ee[t] += a || 0;
    });
  });
  const oe = {};
  if (x.forEach((e) => {
    const t = e[D], r = g.map((a) => ((e.vars && e.vars[a.name]) ?? e[a.name] ?? 0) || 0);
    oe[t] = r.length > 0 ? Math.max(...r) : 0;
  }), x.length === 0 || !D || g.length === 0 && !K)
    return /* @__PURE__ */ m(We, { textColor: p });
  const ie = (e, t) => {
    const r = o.manualColorsMode || "dimension", a = ($ == null ? void 0 : $[l]) || {};
    return r === "measure" ? a.measures && a.measures[e] ? a.measures[e] : t && a[t] ? a[t] : k : t && a[t] ? a[t] : a.measures && a.measures[e] ? a.measures[e] : k;
  }, me = 28, fe = 6, re = 6 + (L === "left" ? 0 : 6), ce = T && parseInt(T, 10) || 0, le = (e) => {
    if (o.showZeroNullMeasures) return g.length;
    let t = 0;
    return g.forEach((r) => {
      const a = (e.vars && e.vars[r.name]) ?? e[r.name] ?? null, u = typeof a == "number" ? a : a != null ? parseFloat(a) : null;
      u != null && u !== 0 && t++;
    }), t;
  };
  let q = 0;
  x.forEach((e) => {
    const t = Math.max(1, le(e)), r = me * t + fe * Math.max(0, t - 1), a = L === "left" ? Math.max(r, ce) + 12 : r + 12 + re;
    q += a;
  });
  const ue = q + 40;
  if (typeof Z == "function") {
    const e = Math.ceil(ue);
    C.current.timer && (clearTimeout(C.current.timer), C.current.timer = null), C.current.last !== e && (C.current.timer = setTimeout(() => {
      C.current.last !== e && (C.current.last = e, Z(e));
    }, 100));
  }
  return /* @__PURE__ */ m("div", { className: "grouped-bars-data-frame", children: x.map((e, t) => {
    const r = e[D], a = g.map((n) => {
      const v = (e.vars && e.vars[n.name]) ?? e[n.name] ?? null, d = typeof v == "number" ? v : v != null ? parseFloat(v) : null;
      let M = 0;
      if (P === "percentage")
        if (g.length > 1)
          if (c) {
            const F = ee[r] || 0;
            M = F > 0 && d != null ? d / F * 100 : 0;
          } else {
            const F = se || 0;
            M = F > 0 && d != null ? d / F * 100 : 0;
          }
        else {
          const F = Y[n.name] || 0;
          M = F > 0 && d != null ? d / F * 100 : 0;
        }
      else if (P === "relative_max")
        if (g.length > 1 && c) {
          const F = oe[r] || 0;
          M = F > 0 && d != null ? d / F * 100 : 0;
        } else
          M = X > 0 && d != null ? d / X * 100 : 0;
      else
        M = X > 0 && d != null ? d / X * 100 : 0;
      (d == null || d === 0) && (M = 0), M = Math.max(0, Math.min(100, M));
      const ae = ie(n.name, r);
      return { name: n.name, label: n.label || n.name, value: d, width: M, color: ae, format: n.format || f };
    }), u = o.showZeroNullMeasures ? a : a.filter((n) => n.value != null && n.value !== 0);
    let h = null;
    if (S) {
      const n = u.find((v) => v.name === S);
      o.showZeroNullMeasures ? h = n || null : h = n && n.value != null && n.value !== 0 ? n : null;
    }
    let z = h ? u.filter((n) => n.name !== S) : u;
    return _ === "measure" && (z = [...z].sort(
      (n, v) => O === "asc" ? n.value - v.value : v.value - n.value
    )), /* @__PURE__ */ m(
      je,
      {
        dimensionValue: r,
        measureEntries: z,
        mainEntry: h,
        barBackgroundColor: B,
        textColor: p,
        measureTextColor: w,
        fontSize: E,
        mainValueFontSize: I,
        labelPosition: L,
        valuePosition: y,
        format: f,
        labelWidth: V,
        labelHeight: T,
        labelFormat: W,
        vars: e.vars,
        intl: U,
        showMeasureLabels: Q
      },
      `${r}-${t}`
    );
  }) });
}, Ke = (o) => {
  var he;
  const {
    editing: l = !1,
    unique: s,
    intl: b,
    "data-csv": i = "",
    "data-dvz-proxy-dataset-id": f,
    "data-no-data-message": p = He,
    "data-view-mode": w = "info",
    "data-height": E,
    "data-app": I,
    "data-measures": U = "{}",
    "data-format": $ = "{}",
    "data-group": k,
    "data-filters": B = "[]",
    "data-text-color": L = Ee,
    "data-measure-text-color": y = "#ffffff",
    "data-back-ground-color": V = Ue,
    "data-font-size": T = $e,
    "data-dimension1": W,
    "data-wait-for-filters": _ = "false",
    "data-no-data-text": O = Re,
    "data-manual-colors": G = "{}",
    "data-default-bar-color": P = ke,
    "data-bar-background-color": H = Ve,
    "data-label-position": S,
    "data-value-position": j,
    "data-label-width": c,
    "data-label-height": Q,
    "data-label-format": Z,
    "data-sorting": C,
    "data-sort-direction": J,
    "data-sort-measure": N,
    "data-top-n": K,
    "data-bar-size-criteria": D,
    "data-main-measure": g,
    "data-show-measure-labels": x,
    "data-bar-size-use-group": Y,
    "data-enable-manual-colors": X,
    "data-manual-colors-mode": se,
    "data-main-value-font-size": ee,
    "data-show-zero-null-measures": oe
  } = o, ie = be(null), [me, fe] = ve(w), [re, ce] = ve(null), le = parseInt(E || 0, 10) || 0, q = Math.max(le, re || 0), ue = l ? q - 80 : q - 40, e = ne($, l), t = we(e), r = ne(B, l), a = ne(U, l), u = ne(G, l), h = Oe(a, t, I, o["data-enable-custom-measure-formats"] === "true"), z = h.map((te) => te.name), n = typeof g == "string" && g.length > 0 ? R(g) : null, v = n && n.toLowerCase() === "none" ? null : n || null, d = h.length > 1 ? v === null ? null : z.includes(v) ? v : z[0] : null, M = typeof N == "string" && N.length > 0 ? R(N) : null, ae = M && M.toLowerCase() === "none" ? null : M || null, F = h.length > 1 ? ae === null ? null : z.includes(ae) ? ae : z[0] : null, Se = Ge(r, f), pe = Pe(W), Me = D, Fe = Y === "true", Ce = X === "true", Ne = se || "dimension", Ie = parseInt(ee || de, 10), ye = x === "true", De = oe === "true";
  return /* @__PURE__ */ m("div", { ref: ie, children: /* @__PURE__ */ m(
    Ae,
    {
      className: `chart container grouped-bars-container ${l ? "editing" : ""}`,
      style: { height: (q || le) + "px", backgroundColor: V },
      fluid: !0,
      children: /* @__PURE__ */ m(
        Be,
        {
          style: { height: `${ue}px` },
          params: Se,
          app: I,
          group: k,
          csv: i,
          editing: l,
          waitForFilters: _ === "true",
          store: [I, s, ...pe],
          source: pe.join("/"),
          children: /* @__PURE__ */ m(Le, { children: /* @__PURE__ */ m(
            Je,
            {
              editing: l,
              locale: b.locale,
              intl: b,
              app: I,
              format: t,
              dimension1: W,
              manualColors: Ce ? u : {},
              manualColorsMode: Ne,
              measure: h.length === 1 ? (he = h[0]) == null ? void 0 : he.name : null,
              fontSize: T,
              textColor: R(L),
              measureTextColor: R(y),
              backGroundColor: R(V),
              noDataText: O,
              defaultBarColor: R(P),
              barBackgroundColor: R(H),
              labelPosition: S,
              valuePosition: h.length > 1 ? "bar" : j,
              labelWidth: c,
              labelHeight: Q,
              labelFormat: Z,
              sorting: C,
              sortDirection: J,
              topN: K,
              barSizeCriteria: Me,
              barSizeUseGroup: Fe,
              selectedMeasures: h,
              mainMeasureName: d,
              sortMeasureName: F,
              mainValueFontSize: Ie,
              showMeasureLabels: ye,
              showZeroNullMeasures: De,
              onHeightChange: (te) => {
                if (typeof te == "number" && te > 0) {
                  const ge = Math.ceil(te);
                  ge !== re && ce(ge);
                }
              }
            }
          ) })
        }
      )
    }
  ) });
}, Xe = (o, l) => {
  const { "data-app": s, "data-group": b } = l, i = o.getIn(["data", "measures", s, b]);
  return i ? { injectedMeasures: i } : {};
}, qe = {}, na = Te(Xe, qe)(Ke);
export {
  na as default
};
