import { jsxs as a, jsx as e, Fragment as $ } from "react/jsx-runtime";
import { useRef as W, useState as K } from "react";
import "prop-types";
import Q from "papaparse";
import { injectIntl as X } from "react-intl";
import Y from "../data/D3MapDataProvider.js";
import Z from "../data/D3MapDataConsumer.js";
import ee from "./GradientColors.js";
import { Icon as G } from "semantic-ui-react";
const U = ({ breaks: l, isPoint: o, numberFormat: d, intl: v }) => l.length > 0 && /* @__PURE__ */ e("div", { className: "legend-breaks", children: l.map((n, c) => n.type !== "graterThan" ? /* @__PURE__ */ a("div", { className: "break", children: [
  /* @__PURE__ */ e(
    "div",
    {
      className: `break-item ${o ? "point" : ""}`,
      style: {
        backgroundColor: n.color,
        border: `1px solid ${n.borderColor}`
      }
    }
  ),
  /* @__PURE__ */ a("div", { className: "break-label", children: [
    " < ",
    v.formatNumber(n.end, d)
  ] })
] }) : /* @__PURE__ */ a("div", { className: "break", children: [
  /* @__PURE__ */ e(
    "div",
    {
      className: `break-item ${o ? "point" : ""}`,
      style: {
        backgroundColor: n.color,
        border: `1px solid ${n.borderColor}`
      }
    }
  ),
  /* @__PURE__ */ a("div", { className: "break-label", children: [
    " > ",
    v.formatNumber(n.end, d)
  ] })
] })) }), re = (l) => {
  const {
    name: o,
    breaks: d,
    pointStyleBy: v,
    dimension2: n,
    pointDimensionStyles: c = {},
    markFillColor: s,
    markBorderColor: m,
    measures: k,
    visible: h,
    format: g,
    intl: D,
    id: r,
    onItemClick: y,
    customMeasuresLabels: f
  } = l, u = {
    style: g.style === "compacted" ? "decimal" : g.style,
    notation: g.style === "compacted" ? "compact" : "standard",
    currency: g.currency,
    minimumFractionDigits: parseInt(g.minimumFractionDigits),
    maximumFractionDigits: parseInt(g.maximumFractionDigits)
  };
  let p = k[0];
  return f && (p = f[k[0]]), [...new Set(Object.keys(c).map((b) => b.split("_")[0]))], /* @__PURE__ */ e("div", { className: "legend", children: /* @__PURE__ */ a("div", { children: [
    /* @__PURE__ */ a("div", { className: "legend-item", children: [
      /* @__PURE__ */ e(
        "div",
        {
          className: "legend-color legend-check",
          onClick: (b) => y(r),
          style: {
            backgroundColor: s,
            borderColor: m
          },
          children: h != !1 && /* @__PURE__ */ e($, { children: "✓" })
        }
      ),
      /* @__PURE__ */ a("div", { className: "legend-label", children: [
        o,
        " (",
        p,
        ")"
      ] })
    ] }),
    h != !1 && /* @__PURE__ */ e(U, { breaks: d, symbol: "arrow", numberFormat: u, intl: D })
  ] }) });
}, ae = (l) => {
  const {
    id: o,
    name: d,
    breaks: v,
    intl: n,
    pointStyleBy: c,
    format: s,
    dimension2: m,
    pointDimensionStyles: k = {},
    markFillColor: h,
    markBorderColor: g,
    measures: D,
    visible: r,
    onItemClick: y,
    customMeasuresLabels: f,
    allCategories: u,
    showDim2OnLegends: p,
    dim2LegendLabel: b,
    type: L,
    d2Click: x,
    selectedItem: F
  } = l, C = {
    style: s.style === "compacted" ? "decimal" : s.style,
    notation: s.style === "compacted" ? "compact" : "standard",
    currency: s.currency,
    minimumFractionDigits: parseInt(s.minimumFractionDigits),
    maximumFractionDigits: parseInt(s.maximumFractionDigits)
  };
  let S = D[0];
  f && (S = f[D[0]]);
  const w = m && u ? u.filter((i) => i.type.toUpperCase() == m.toUpperCase()) : [], N = (w.length > 0 ? w[0].items : []).map((i) => i.value), P = c === "dimension" || p ? m : S;
  return /* @__PURE__ */ e("div", { className: "legend", children: /* @__PURE__ */ a("div", { children: [
    /* @__PURE__ */ a("div", { className: "legend-item", children: [
      /* @__PURE__ */ e(
        "div",
        {
          className: "legend-color legend-check",
          onClick: (i) => y(o),
          style: c === "measure" || p ? {} : {
            backgroundColor: h,
            borderColor: g
          },
          children: r != !1 && /* @__PURE__ */ e($, { children: "✓" })
        }
      ),
      /* @__PURE__ */ a("div", { className: "legend-label", children: [
        d,
        " "
      ] })
    ] }),
    c === "measure" && r != !1 && /* @__PURE__ */ e("div", { className: "legend-breaks", children: v.map((i, z) => /* @__PURE__ */ a("div", { className: "break", children: [
      /* @__PURE__ */ e("div", { className: "break-item", style: {
        backgroundColor: i.color,
        border: `1px solid ${i.borderColor}`,
        borderRadius: L == "dataPoints" ? "50%" : "0"
      } }),
      /* @__PURE__ */ a("div", { className: "break-label", children: [
        " < ",
        n.formatNumber(i.end, C)
      ] })
    ] })) }),
    r != !1 && p != !1 && /* @__PURE__ */ e("div", { className: "legend", children: /* @__PURE__ */ e("div", { className: "legend-item", children: /* @__PURE__ */ e("div", { className: "legend-label", children: b || P }) }) }),
    r != !1 && p != !1 && /* @__PURE__ */ e("div", { className: "legend-breaks", children: N.map((i) => /* @__PURE__ */ a("div", { className: "break", children: [
      c === "measure" ? /* @__PURE__ */ e(
        "span",
        {
          className: "break-item dim2-item"
        }
      ) : /* @__PURE__ */ e(
        "div",
        {
          className: "break-item",
          style: {
            backgroundColor: k[i + "_color"] || h,
            border: `1px solid ${k[i + "_border"] || g}`,
            borderRadius: L == "dataPoints" ? "50%" : "0"
          }
        }
      ),
      /* @__PURE__ */ e("div", { className: "break-label", onClick: (z) => x(i), children: F == i ? /* @__PURE__ */ e("b", { children: i }) : i })
    ] })) })
  ] }) });
}, ie = (l) => {
  const { fillColor: o, borderColor: d, name: v, visible: n, id: c, onItemClick: s } = l;
  return /* @__PURE__ */ e("div", { className: "legend", children: /* @__PURE__ */ a("div", { className: "legend-item", children: [
    /* @__PURE__ */ e(
      "div",
      {
        className: "legend-color legend-check",
        onClick: (m) => s(c),
        style: { backgroundColor: o, borderColor: d },
        children: n != !1 && /* @__PURE__ */ e($, { children: "✓" })
      }
    ),
    /* @__PURE__ */ e("div", { className: "legend-label", children: v })
  ] }) });
}, le = (l) => {
  const {
    markFillColor: o,
    fillColor: d,
    markSizeScale: v,
    markBorderColor: n,
    useCentroidPoint: c,
    name: s,
    useBreaks: m,
    useGradients: k,
    breaks: h,
    intl: g,
    usePattern: D,
    patternsData: r,
    patternDiscriminator: y,
    patternDiscriminatorLabel: f,
    measures: u,
    borderColor: p,
    data: b,
    app: L,
    customMeasuresLabels: x,
    divRef: F,
    id: C,
    patternWidth: S = 0.35,
    patternHeight: w = 0.25,
    group: E,
    format: N,
    csv: P,
    visible: i,
    onItemClick: z,
    gradientScheme: q,
    gradientReverse: A,
    toggleColorLayer: j,
    colorLayerVisible: B = !0,
    gradientStartColor: H,
    gradientEndColor: J
  } = l;
  let I = "";
  const R = {
    style: N.style === "compacted" ? "decimal" : N.style,
    notation: N.style === "compacted" ? "compact" : "standard",
    currency: N.currency,
    minimumFractionDigits: parseInt(N.minimumFractionDigits),
    maximumFractionDigits: parseInt(N.maximumFractionDigits)
  }, V = (t) => new ee({
    data: t.children,
    measure: u[0],
    defaultFillColor: o,
    gradientScheme: q,
    gradientReverse: A,
    gradientStartColor: H,
    gradientEndColor: J
  });
  if (L != "csv" && x)
    I = x[u[0]];
  else {
    const t = Q.parse(P, { header: !0, dynamicTyping: !0 });
    I = t.meta.fields.length > 0 ? t.meta.fields[1] : "";
  }
  const M = (t) => t ? t.toString().replace(/ /g, "_").toLocaleLowerCase() : "";
  if (F.current) {
    const t = F.current.querySelector(
      `.legend.layer_${M(C)} svg`
    );
    t && (t.style.display = i === !1 ? "none" : "block");
  }
  const O = (t) => Math.min(...t.children.map((_) => _[u[0]])), T = (t) => Math.max(...t.children.map((_) => _[u[0]]));
  return /* @__PURE__ */ e("div", { className: `legend layer_${M(C)}`, id: M(`${E} ${s} ${C}`), children: /* @__PURE__ */ a("div", { children: [
    /* @__PURE__ */ a("div", { className: "legend-item", children: [
      /* @__PURE__ */ e(
        "div",
        {
          className: "legend-color legend-check",
          onClick: (t) => z(C),
          style: { backgroundColor: d, borderColor: p },
          children: i != !1 && /* @__PURE__ */ e($, { children: "✓" })
        }
      ),
      /* @__PURE__ */ e("div", { className: "legend-label", children: s })
    ] }),
    c && !m && i != !1 && /* @__PURE__ */ e("div", { className: "legend-breaks", children: /* @__PURE__ */ a("div", { className: "break", children: [
      /* @__PURE__ */ e(
        "div",
        {
          className: `break-item point ${B ? "checked" : ""}`,
          onClick: (t) => j(C),
          style: {
            backgroundColor: o,
            border: `1px solid ${n}`
          }
        }
      ),
      I
    ] }) }),
    m && i != !1 && /* @__PURE__ */ a("div", { children: [
      /* @__PURE__ */ e("div", { className: "legend-breaks", children: /* @__PURE__ */ a(
        "div",
        {
          className: "legend-section-title",
          onClick: (t) => j(C),
          children: [
            /* @__PURE__ */ e("div", { className: `legend-section-title-checkbox ${B ? "checked" : ""}` }),
            /* @__PURE__ */ e("span", { className: "measure-label", children: I })
          ]
        }
      ) }),
      B && /* @__PURE__ */ e(
        U,
        {
          isPoint: c,
          numberFormat: R,
          breaks: h,
          visible: i,
          intl: g
        }
      )
    ] }),
    k && b && b.children && b.children.length > 0 && i != !1 && /* @__PURE__ */ a("div", { className: "gradient-container", children: [
      /* @__PURE__ */ e("div", { className: "gradient-label", style: { float: "right" }, children: g.formatNumber(T(b), R) }),
      /* @__PURE__ */ e("div", { className: "gradient-label", style: { float: "left" }, children: g.formatNumber(O(b), R) }),
      /* @__PURE__ */ e(
        "div",
        {
          className: "gradient-bar",
          style: {
            background: `linear-gradient(to right, ${V(b).getStartColor()}, ${V(b).getEndColor()})`,
            width: "120px",
            height: "10px"
          }
        }
      )
    ] })
  ] }) });
}, te = (l) => {
  const o = W(null), [d, v] = K(!1), n = ({ size: r = 16, color: y = "currentColor" }) => /* @__PURE__ */ e(G, { name: "chevron down" }), c = ({ size: r = 16, color: y = "currentColor" }) => /* @__PURE__ */ e(G, { name: "chevron right" }), { layers: s = [], onItemClick: m, patternsData: k, group: h, intl: g, toggleColorLayer: D } = l;
  return /* @__PURE__ */ a("div", { className: `legends ${d ? "collapsed" : ""}`, ref: o, children: [
    /* @__PURE__ */ e("div", { className: "legend-collapsible-control", onClick: () => v(!d), children: d ? /* @__PURE__ */ a("div", { children: [
      " ",
      /* @__PURE__ */ e(c, { size: 14 }),
      "   Show Legend"
    ] }) : /* @__PURE__ */ a("div", { children: [
      /* @__PURE__ */ e(n, { size: 14 }),
      "Hide Legend"
    ] }) }),
    /* @__PURE__ */ e("div", { className: "legends-content", children: s && s.map((r) => {
      const y = {}, f = r.filters || {};
      return f && f.forEach && f.forEach((u) => {
        u.value != null && u.value.filter((p) => p != null && p.toString().trim() != "").length > 0 && (y[u.param] = u.value);
      }), r.dvzProxyDatasetId && (y.dvzProxyDatasetId = r.dvzProxyDatasetId), /* @__PURE__ */ a("div", { children: [
        r.type == "base" && /* @__PURE__ */ e(ie, { ...r, group: h, onItemClick: m }),
        r.type == "data" && r.apiJoinAttribute && /* @__PURE__ */ e(
          Y,
          {
            waitForFilters: !0,
            editing: r.editing,
            params: y,
            app: r.app,
            verbose: !1,
            csv: decodeURIComponent(r.csv),
            group: h,
            ignoreErrors: !0,
            isSvg: !0,
            mySelf: "Legends",
            store: [r.app, l.unique, r.id],
            source: r.apiJoinAttribute + (r.patternDiscriminator != "none" ? "/" + r.patternDiscriminator : ""),
            children: /* @__PURE__ */ e(Z, { children: /* @__PURE__ */ e(le, { group: h, patternsData: k ? k[r.id] : null, divRef: o, ...r, intl: l.intl, onItemClick: m, toggleColorLayer: D }) })
          }
        ),
        r.type == "dataPoints" && /* @__PURE__ */ e(ae, { selectedItem: l.selectedItem, d2Click: l.d2Click, intl: l.intl, group: h, ...r, onItemClick: m }),
        r.type == "flow" && /* @__PURE__ */ e(re, { group: h, ...r, onItemClick: m, intl: l.intl })
      ] }, r.id);
    }) })
  ] });
}, pe = X(te);
export {
  pe as default
};
