import { jsx as tt } from "react/jsx-runtime";
import "react";
import ct from "./BaseLayer.js";
import dt from "../data/D3MapDataProvider.js";
import ut from "../data/D3MapDataConsumer.js";
import * as W from "d3";
import { injectIntl as ht } from "react-intl";
import Z from "./BreaksStyles.js";
import lt from "./GradientColors.js";
const H = (D) => D ? D.toString().replace(/ /g, "_") : "", k = (D) => D ? "pattern_" + H(D) : "";
class ft extends ct {
  constructor() {
    super(), this.state = { geoJson: null, json: null }, this.getTooltipVariables = this.getTooltipVariables.bind(this), this.resize = this.resize.bind(this), this.createLayer = this.createLayer.bind(this), this.createCentroids = this.createCentroids.bind(this), this.createPatterns = this.createPatterns.bind(this), this.createPaths = this.createPaths.bind(this);
  }
  createLayer(e) {
    const l = this.joinData(e, this.props.app, this.props.featureJoinAttribute, this.props.data, this.props.measures, this.props.patternDiscriminator);
    this.createDataLayer(l), this.props.onReady && this.props.onReady();
  }
  resize() {
    const {
      markerLabelSize: e,
      markFillColor: l,
      markBorderColor: v,
      markSizeScale: i,
      measures: u,
      data: y,
      breaks: n,
      gradientScheme: w,
      gradientReverse: s,
      labelFontSize: m
    } = this.props, o = this.props.transform ? this.props.transform.k : 1;
    super.resize();
    const c = new Z({
      breaks: n,
      defaultFillColor: l,
      defaultBorderColor: v,
      defaultSize: i
    });
    this.g.selectAll(".centroids .point").attr("r", (p) => c.getSize(p.properties._value) * 1 / o), this.g.selectAll(".point-label").attr("font-size", (p) => e * (1 / o) + "px");
  }
  getTooltipVariables(e) {
    const { apiJoinAttribute: l } = this.props;
    return e.properties._value ? {
      ...e.properties,
      meta: {
        [l]: e.properties.meta ? e.properties.meta.value : "",
        ...e.properties.meta,
        value: e.properties._value
      }
    } : {};
  }
  createDataLayer(e) {
    const {
      app: l,
      svg: v,
      format: i,
      id: u,
      file: y,
      path: n,
      onLayerCreated: w,
      labelFilter: s = [],
      labelField: m,
      labelFontSize: o,
      labelColor: c,
      fillColor: p,
      borderColor: g,
      tooltip: x,
      markFillColor: b,
      markLabelColor: J,
      markBorderColor: R,
      markSizeScale: G,
      markerLabelSize: K,
      featureJoinAttribute: et,
      apiJoinAttribute: Q,
      measures: $,
      editing: rt,
      data: C,
      patternDiscriminator: S,
      patternDiscriminatorLabel: X,
      breaks: I,
      gradientScheme: N,
      gradientReverse: U,
      patterns: L,
      projection: at,
      useBreaks: Y,
      useGradients: T,
      useCentroidPoint: j,
      usePattern: q,
      waitForFilters: it,
      intl: M,
      patternsVisible: B = !0,
      togglePatterns: E,
      colorLayerVisible: O = !0,
      visible: st
    } = this.props;
    if (this.gRef && this.gRef.current) {
      this.g = W.select(this.gRef.current), this.g.attr("class", "base-layer");
      const z = e.features.filter((V) => V.properties._value != null);
      this.createPaths(e), j || this.createColors(z), q && this.createPatterns(e), m != "none" && this.createLabels(e), j && this.createCentroids(z);
    }
  }
  createColors(e) {
    const {
      app: l,
      svg: v,
      format: i,
      id: u,
      file: y,
      path: n,
      onLayerCreated: w,
      labelFilter: s = [],
      labelField: m,
      labelFontSize: o,
      labelColor: c,
      fillColor: p,
      borderColor: g,
      tooltip: x,
      markFillColor: b,
      markLabelColor: J,
      markBorderColor: R,
      markSizeScale: G,
      markerLabelSize: K,
      featureJoinAttribute: et,
      apiJoinAttribute: Q,
      measures: $,
      editing: rt,
      data: C,
      patternDiscriminator: S,
      patternDiscriminatorLabel: X,
      breaks: I,
      gradientScheme: N,
      gradientReverse: U,
      patterns: L,
      projection: at,
      useBreaks: Y,
      useGradients: T,
      useCentroidPoint: j,
      usePattern: q,
      waitForFilters: it,
      intl: M,
      patternsVisible: B = !0,
      togglePatterns: E,
      colorLayerVisible: O = !0,
      visible: st
    } = this.props, { gradientStartColor: z, gradientEndColor: V } = this.props, d = new Z({
      breaks: I,
      defaultFillColor: b,
      defaultBorderColor: R,
      defaultSize: G
    }), A = new lt({
      data: C ? C.children : [],
      measure: $[0],
      defaultFillColor: b,
      gradientScheme: N,
      gradientReverse: U,
      gradientStartColor: z,
      gradientEndColor: V
    });
    this.g && (this.g.selectAll("path").attr("fill", (h) => !h || !h.properties || !h.properties._value ? p : T ? A.getColor(h.properties._value) : d.getColor(h.properties._value)).attr("stroke", g).attr("id", "state-borders").attr("d", n).on("mouseenter", (h, r) => {
      r.properties._value && this.showToolTip(x, this.getTooltipVariables(r), T ? A.getColor(r.properties._value) : d.getColor(r.properties._value), r);
    }).on("mouseleave", (h) => {
      this.hiddenToolTip(h);
    }).on("mousemove", (h) => {
      this.moveToolTip(h);
    }), O || this.g.selectAll(".borders").style("fill", this.props.fillColor), this.g.attr("transform", this.props.transform));
  }
  createCentroids(e) {
    const {
      app: l,
      svg: v,
      format: i,
      id: u,
      file: y,
      path: n,
      onLayerCreated: w,
      labelFilter: s = [],
      labelField: m,
      labelFontSize: o,
      labelColor: c,
      fillColor: p,
      borderColor: g,
      tooltip: x,
      markFillColor: b,
      markLabelColor: J,
      markBorderColor: R,
      markSizeScale: G,
      markerLabelSize: K,
      featureJoinAttribute: et,
      apiJoinAttribute: Q,
      measures: $,
      editing: rt,
      data: C,
      patternDiscriminator: S,
      patternDiscriminatorLabel: X,
      breaks: I,
      gradientScheme: N,
      gradientReverse: U,
      patterns: L,
      projection: at,
      useBreaks: Y,
      useGradients: T,
      useCentroidPoint: j,
      usePattern: q,
      waitForFilters: it,
      intl: M,
      patternsVisible: B = !0,
      togglePatterns: E,
      colorLayerVisible: O = !0,
      visible: st
    } = this.props, z = new Z({
      breaks: I,
      defaultFillColor: b,
      defaultBorderColor: R,
      defaultSize: G
    }), V = (d) => new lt({
      data: d.children,
      measure: $[0],
      defaultFillColor: b,
      gradientScheme: N,
      gradientReverse: U
    });
    if (this.g) {
      const d = {
        style: i.style === "compacted" ? "decimal" : i.style,
        notation: i.style === "compacted" ? "compact" : "standard",
        currency: i.currency,
        minimumFractionDigits: parseInt(i.minimumFractionDigits),
        maximumFractionDigits: parseInt(i.maximumFractionDigits)
      }, A = this.props.transform ? this.props.transform.k : 1;
      this.g.selectAll(".centroids").remove();
      const h = this.g.selectAll("centroids").data(e).enter().append("g").attr("class", "centroids");
      h.append("circle").attr("fill", (r) => T === !0 ? V(C).getColor(r.properties._value) : z.getColor(r.properties._value, !0)).attr("stroke", R).attr("class", "point").attr("stroke-width", 2).style("vector-effect", "non-scaling-stroke").attr("cx", (r) => n.centroid(r)[0]).attr("cy", (r) => n.centroid(r)[1]).attr("r", (r) => z.getSize(r.properties._value) * 1 / A).on("mouseenter", (r, f) => {
        if (f.properties._value) {
          const t = {
            ...f.properties,
            meta: {
              [Q]: f.properties.meta ? f.properties.meta.value : "",
              ...f.properties.meta,
              value: f.properties._value
            }
          };
          this.showToolTip(x, t, T === !0 ? V(C).getColor(f.properties._value) : z.getColor(f.properties._value));
        }
      }).on("mouseleave", (r) => {
        this.hiddenToolTip();
      }), h.append("text").attr("class", "point-label").attr("x", (r) => n.centroid(r)[0]).attr("y", (r) => n.centroid(r)[1]).attr("font-size", (r) => K * (1 / A) + "px").attr("text-anchor", "middle").attr("dominant-baseline", "middle").style("pointer-events", "none").attr("fill", J).text((r) => M.formatNumber(i.style === "percent" ? r.properties._value / 100 : r.properties._value, d)).on("mouseover", (r) => {
      }), O || this.g.selectAll(".centroids").style("display", "none");
    }
  }
  createPatterns(e) {
    const {
      app: l,
      svg: v,
      format: i,
      id: u,
      file: y,
      path: n,
      onLayerCreated: w,
      labelFilter: s = [],
      labelField: m,
      labelFontSize: o,
      labelColor: c,
      fillColor: p,
      borderColor: g,
      tooltip: x,
      markFillColor: b,
      markLabelColor: J,
      markBorderColor: R,
      markSizeScale: G,
      markerLabelSize: K,
      featureJoinAttribute: et,
      apiJoinAttribute: Q,
      measures: $,
      editing: rt,
      data: C,
      patternDiscriminator: S,
      patternDiscriminatorLabel: X,
      breaks: I,
      gradientScheme: N,
      gradientReverse: U,
      patterns: L,
      projection: at,
      useBreaks: Y,
      useGradients: T,
      useCentroidPoint: j,
      usePattern: q,
      waitForFilters: it,
      intl: M,
      patternsVisible: B = !0,
      togglePatterns: E,
      colorLayerVisible: O = !0,
      visible: st
    } = this.props, z = new Z({
      breaks: I,
      defaultFillColor: b,
      defaultBorderColor: R,
      defaultSize: G
    }), V = this.props.transform ? this.props.transform.k : 1, d = 10 * 1 / V, A = 10 * 1 / V, h = (t) => new lt({
      data: t.children,
      measure: $[0],
      defaultFillColor: b,
      gradientScheme: N,
      gradientReverse: U,
      gradientStartColor,
      gradientEndColor
    });
    let r = [];
    if (l == "csv" && S != "none")
      r = [...new Set(C.data.map((t) => t[S]))].map((t) => ({
        key: t,
        type: L[t + "_symbol"],
        color: L[t + "_color"],
        rotation: L[t + "_rotation"]
      }));
    else if (S != "none") {
      const t = C && C.metadata ? C.metadata.types.filter((F) => F.dimension == S) : [];
      r = t && t.length > 0 ? t[0].items.map((F) => {
        const _ = F.value;
        return {
          key: _,
          type: L[_ + "_symbol"],
          color: L[_ + "_color"],
          rotation: L[_ + "_rotation"]
        };
      }) : [];
    }
    this.g.selectAll("defs").remove();
    const f = this.g.append("defs");
    if (f.selectAll("pattern").remove(), f.selectAll("pattern").data(r).enter().append("pattern").attr("id", (t) => k(t.key)).attr("patternUnits", "userSpaceOnUse").attr("width", d).attr("height", A).attr("x", 0).attr("y", 0).attr("patternTransform", (t) => `rotate(${t.rotation})`), r.forEach((t) => {
      t.type === "lines" && f.select("#" + k(t.key)).append("rect").attr("x", 0.05).attr("width", d / 2).attr("height", A).attr("opacity", 1).attr("fill", t.color), t.type === "squares" && f.select("#" + k(t.key)).append("rect").attr("width", d / 2).attr("height", A / 2).attr("fill", t.color).attr("opacity", 1).attr("stroke-width", 1), t.type === "dots" && f.select("#" + k(t.key)).append("circle").attr("cx", d / 2).attr("cy", A / 2).attr("r", d / 2.5).attr("fill", t.color).attr("opacity", 1).attr("stroke-width", 1), t.type === "triangle" && f.select("#" + k(t.key)).append("polygon").attr("points", `${d / 2} 0, 0 ${d}, ${d}  ${d} `).attr("fill", t.color).attr("opacity", 1).attr("stroke-width", 1);
    }), r = r.filter((t) => t.type != null).sort((t, F) => new Intl.Collator(M.locale, { caseFirst: "upper", numeric: !0, sensitivity: "variant" }).compare(t.key, F.key)), q && e && e.features) {
      this.g.selectAll(".shape-pattern").remove(), B && e.features.forEach((a) => {
        let P = [];
        a.properties && a.properties.meta && (P = l != "csv" ? a.properties.meta[S] ? a.properties.meta[S] : [] : [a.properties.meta[S]], P && P.length > 0 && P.forEach((nt) => {
          this.g.append("path").attr("d", n(a)).datum(nt).attr("class", "shape-pattern").attr("opacity", (ot) => {
            if (Y)
              return 0.7;
          }).attr("fill", (ot) => "transparent").attr("style", () => "none;fill:url(#" + k(nt) + ");").on("mouseenter", () => {
            this.showToolTip(x, this.getTooltipVariables(a), T === !0 ? h(C).getColor(a.properties._value) : z.getColor(a.properties._value));
          }).on("mousemove", (ot) => {
            this.moveToolTip();
          }).on("mouseleave", (ot) => {
            this.hiddenToolTip();
          });
        }));
      }), W.select(this.gRef.current.parentNode.parentNode).select(`.layer_${H(u)}`).select("svg").remove();
      const t = W.select(this.gRef.current.parentNode.parentNode).select(`.layer_${H(u)}`).append("svg");
      t.attr("height", 30 + r.length * 23 + "px");
      const F = t.append("svg").append("g"), _ = F.append("defs");
      _.selectAll("pattern").remove(), B && (_.selectAll("pattern").data(r).enter().append("pattern").attr("id", (a) => "l_" + k(a.key)).attr("patternUnits", "userSpaceOnUse").attr("width", 5).attr("height", 5).attr("x", 0).attr("y", 0).attr("patternTransform", (a) => `rotate(${a.rotation ? a.rotation : 0})`), r.forEach((a) => {
        a.type === "lines" && _.select("#l_" + k(a.key)).append("rect").attr("x", 0).attr("width", 1).attr("height", 10).attr("opacity", 0.75).attr("fill", a.color), a.type === "squares" && _.select("#l_" + k(a.key)).append("rect").attr("width", 3).attr("height", 3).attr("fill", a.color).attr("opacity", 1).attr("stroke-width", 1), a.type === "dots" && _.select("#l_" + k(a.key)).append("circle").attr("cx", 2).attr("cy", 2).attr("r", 2).attr("fill", a.color).attr("opacity", 1).attr("stroke-width", 1), a.type === "triangle" && _.select("#l_" + k(a.key)).append("polygon").attr("points", "5,0 8,8 0,5").attr("fill", a.color).attr("opacity", 1).attr("stroke-width", 1);
      }));
      let pt = B ? "☑ " : "☐ ";
      F.append("text").attr("class", "patterns-checkbox").attr("x", 10).attr("y", 20).text((a) => pt).attr("font-size", "22px").on("click", () => {
        E && E(u);
      }), F.append("text").attr("class", "patterns-title").attr("x", 25).attr("y", 7).text((a) => l === "csv" ? S : X).on("click", () => {
        E && E(u);
      }), B && (F.selectAll(".legend-squares").data(r).enter().append("rect").attr("width", 15).attr("height", 15).attr("y", (a, P) => P * 22 + 30).attr("x", 15).attr("stroke", g).attr("style", (a) => "none;fill:url(#l_" + k(a.key) + ");"), F.selectAll(".patterns-labels").data(r).enter().append("text").attr("class", "patterns-labels").attr("y", (a, P) => P * 22 + 30).attr("x", 32).text((a) => a.key));
    }
  }
  joinData(e, l, v, i, u, y) {
    const n = e.features.map((s) => {
      const m = s.properties[v];
      if (l != "csv" && i && i.children) {
        const o = i.children.filter((c) => c.value == m);
        if (o.length > 0) {
          const c = o[0][u[0]];
          if (s.properties.meta = o[0], s.properties._value = c, y && y != "none") {
            const p = o[0] && o[0].children ? o[0].children.filter((g) => g.type == y).map((g) => g.value) : [];
            s.properties.meta[y] = p;
          }
        } else
          s.properties._value = null;
      } else if (l == "csv") {
        const o = i.data.filter((c) => c[i.meta.fields[0]] == m);
        o.length > 0 ? (s.properties.meta = o[0], s.properties._value = o[0][i.meta.fields[1]]) : s.properties._value = null;
      } else
        s.properties._value = null;
      return s;
    });
    return { ...e, features: n };
  }
  componentDidUpdate(e, l, v) {
    const { app: i, file: u, featureJoinAttribute: y, data: n, measures: w, patternDiscriminator: s, editing: m, usePattern: o } = this.props;
    if ((m || e.data !== n || e.path !== this.props.path) && this.create(), e.visible != this.props.visible && this.g.style("display", this.props.visible ? "" : "none"), e.patternsVisible != this.props.patternsVisible) {
      const p = W.select(this.gRef.current.parentNode.parentNode).select(`.layer_${H(this.props.id)}`);
      p.select(".patterns-checkbox").text(this.props.patternsVisible ? "☑ " : "☐ "), p.selectAll(".patterns-labels").style("display", this.props.patternsVisible ? "" : "none"), p.selectAll("rect").style("display", this.props.patternsVisible ? "" : "none"), p.select("svg").attr("height", this.props.patternsVisible ? 30 + (p.selectAll("rect").size() - 1) * 23 + "px" : "30px"), this.g.selectAll(".shape-pattern").style("display", this.props.patternsVisible ? "" : "none");
    }
    e.colorLayerVisible != this.props.colorLayerVisible && (this.g.selectAll(".borders").style("fill", (c) => this.props.colorLayerVisible ? null : this.props.fillColor), this.g.selectAll(".centroids").style("display", this.props.colorLayerVisible ? "block" : "none")), e.usePattern != this.props.usePattern && (this.props.usePattern || W.select(this.gRef.current.parentNode.parentNode).select(`.layer_${H(this.props.id)}`).select("svg").remove()), this.g && this.resize();
  }
  componentDidMount() {
    super.componentDidMount();
  }
  render() {
    const {
      id: e,
      file: l,
      path: v,
      zoom: i,
      labelFilter: u = [],
      labelField: y,
      labelFontSize: n,
      labelColor: w,
      fillColor: s,
      borderColor: m,
      featureJoinAttribute: o,
      apiJoinAttribute: c,
      dvzProxyDatasetId: p,
      editing: g
    } = this.props;
    return /* @__PURE__ */ tt("g", { id: "data-" + e, className: "data " + e, ref: this.gRef });
  }
}
const mt = (D) => {
  const {
    id: e,
    unique: l,
    filters: v,
    csv: i,
    app: u,
    group: y = "default",
    apiJoinAttribute: n,
    editing: w,
    patternDiscriminator: s,
    dvzProxyDatasetId: m,
    intl: o,
    settings: c,
    waitForFilters: p
  } = D, g = {}, x = v || {};
  return x && x.forEach && x.forEach((b) => {
    b.value != null && b.value.filter((J) => J != null && J.toString().trim() != "").length > 0 && (g[b.param] = b.value);
  }), m && (g.dvzProxyDatasetId = m), /* @__PURE__ */ tt(
    dt,
    {
      waitForFilters: !0,
      editing: w,
      params: g,
      app: u,
      csv: decodeURIComponent(i),
      group: y,
      ignoreErrors: !0,
      isSvg: !0,
      store: [u, l, e],
      mySelf: "Data layer",
      source: n + (s != "none" ? "/" + s : ""),
      children: /* @__PURE__ */ tt(ut, { children: /* @__PURE__ */ tt(ft, { ...D }) })
    }
  );
}, _t = ht(mt);
export {
  _t as default
};
