import { jsx as S } from "react/jsx-runtime";
import { useRef as _, useState as P, useEffect as R } from "react";
import L from "../data/DataProvider.js";
import E from "../data/DataConsumer.js";
import { connect as O } from "react-redux";
import { formatContent as Z } from "../chart/Tooltip.js";
const $ = (l) => {
  const {
    editing: m = !1,
    unique: p,
    intl: r,
    childContent: s,
    "data-csv": h = "",
    "data-dvz-proxy-dataset-id": f,
    "data-view-mode": H = "info",
    "data-app": b,
    "data-measures": x = "{}",
    "data-format": w = "{}",
    "data-group": u,
    "data-filters": y = "[]",
    "data-number-font-size": n = 20,
    "data-number-color": j = "#000000",
    "data-wait-for-filters": T = "false",
    "data-no-data-text": M = "-",
    "data-text-template": z = ""
  } = l, V = r.locale, A = _(null);
  let v = !1, F = "";
  try {
    const t = new URLSearchParams(window.location.search);
    v = t.get("autoHeight") === "1" || t.get("autoHeight") === "true", F = t.get("frameId") || "";
  } catch {
  }
  const g = (t) => m ? t : decodeURIComponent(t), o = (t) => {
    try {
      return JSON.parse(g(t));
    } catch {
      console.error("error parsing value:" + t);
    }
    return null;
  }, d = o(w), i = d ? {
    style: d.style === "compacted" ? "decimal" : d.style,
    notation: d.style === "compacted" ? "compact" : "standard",
    currency: d.currency,
    minimumFractionDigits: parseInt(d.minimumFractionDigits),
    maximumFractionDigits: parseInt(d.maximumFractionDigits)
  } : {
    notation: "standard",
    currency: "USD",
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  }, [C, N] = P(H), D = Math.max(24, (n || 14) * 1.2), I = {}, c = o(y) || {};
  c && c.forEach && c.forEach((t) => {
    t.value != null && t.value.filter((e) => e != null && e.toString().trim() != "").length > 0 && (I[t.param] = t.value);
  }), f && (I.dvzProxyDatasetId = f);
  const a = [];
  return q(A, v, F), /* @__PURE__ */ S("span", { ref: A, children: /* @__PURE__ */ S(
    L,
    {
      style: { minHeight: `${D}px` },
      params: I,
      app: b,
      group: u,
      csv: h,
      editing: m,
      waitForFilters: T === "true",
      store: [b, p, ...a],
      source: a.join("/"),
      children: /* @__PURE__ */ S(E, { children: /* @__PURE__ */ S(
        k,
        {
          locale: V,
          intl: r,
          app: b,
          format: i,
          measure: o(x)[0] || null,
          numberFontSize: n,
          numberColor: j,
          noDataText: M,
          textTemplate: z
        }
      ) })
    }
  ) });
}, k = (l) => {
  var v, F;
  const {
    app: m,
    measure: p,
    data: r,
    format: s,
    numberColor: h,
    numberFontSize: f,
    noDataText: H = "-",
    textTemplate: b = "",
    intl: x
  } = l;
  let w = p, u = r;
  m === "csv" && ((v = r == null ? void 0 : r.meta) != null && v.fields) && ((F = r == null ? void 0 : r.data) != null && F.length) && (w = r.meta.fields[0], u = r.data[0]);
  const y = (u == null ? void 0 : u[w]) ?? null, n = y ? (s == null ? void 0 : s.style) === "percent" ? y / 100 : y : null, [j, T] = P(n);
  R(() => {
    n != null && T(n);
  }, [n]);
  const M = {
    color: decodeURIComponent(h),
    fontSize: f + "px",
    textAlign: "center",
    whiteSpace: "normal",
    wordBreak: "break-word"
  }, z = (g) => x.formatNumber(g, { ...s }), V = (g) => {
    if (!g) return "";
    let o = String(g);
    return o = o.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, ""), o = o.replace(/\son\w+=(?:"[^"]*"|'[^']*')/gi, ""), o;
  };
  return /* @__PURE__ */ S("span", { style: M, dangerouslySetInnerHTML: { __html: (() => {
    const o = {
      ...u && typeof u == "object" ? u : {},
      measure: w || "",
      rawValue: y,
      value: n,
      formattedValue: n != null ? z(n) : H
    }, d = n !== null ? z(n) : null;
    let i = decodeURIComponent(b) || "";
    i = i.replace(/(\%|\#C|\#)\(\s*([^)]*?)\s*\)/g, (c, a, t) => {
      const e = t.replace(/\s+/g, "");
      return `${a}(${e})`;
    });
    const C = (c, a, t) => x.formatNumber(c, { maximumFractionDigits: a ?? 2, ...t }), N = (c) => {
      const a = o[c];
      return typeof a == "string" ? Number(a) : a;
    };
    i = i.replace(/#C\(\{([a-zA-Z0-9_]+)\}(?:,([0-9]+))?\)/g, (c, a, t) => {
      const e = N(a);
      return e == null || isNaN(e) ? "" : C(e, t ? parseInt(t) : 2, { notation: "compact" });
    }), i = i.replace(/#\(\{([a-zA-Z0-9_]+)\}(?:,([0-9]+))?\)/g, (c, a, t) => {
      const e = N(a);
      return e == null || isNaN(e) ? "" : C(e, t ? parseInt(t) : 2, { style: "decimal" });
    }), i = i.replace(/%\(\{([a-zA-Z0-9_]+)\}(?:,([0-9]+))?\)/g, (c, a, t) => {
      const e = N(a);
      if (e == null || isNaN(e)) return "";
      const U = e / 100;
      return C(U, t ? parseInt(t) : 2, { style: "percent" });
    });
    const D = Z(i, o, x, !1), I = D && D.trim().length ? D : d ?? H;
    return V(I);
  })() } });
}, q = (l, m, p) => {
  R(() => {
    if (!m) return;
    const r = l.current || document.body, s = () => {
      try {
        const f = (l.current ? l.current.scrollHeight : document.body.scrollHeight) || 0;
        window.parent && f && window.parent.postMessage({ type: "dvz-embed-height", id: p, height: f }, "*");
      } catch {
      }
    }, h = new MutationObserver(() => {
      requestAnimationFrame(s);
    });
    return h.observe(r, { childList: !0, subtree: !0, characterData: !0 }), setTimeout(s, 0), () => h.disconnect();
  }, [l, m, p]);
}, B = (l, m) => {
  const { "data-app": p, "data-group": r } = m, s = l.getIn(["data", "measures", p, r]);
  return s ? {
    injectedMeasures: s
  } : {};
}, J = {}, tt = O(B, J)($);
export {
  tt as default
};
