import { jsx as b } from "react/jsx-runtime";
import "react";
import Y from "./BaseLayer.js";
import q from "../data/DataProvider.js";
import G from "../data/DataConsumer.js";
import * as T from "d3";
import { injectIntl as H } from "react-intl";
import X from "papaparse";
import K from "./BreaksStyles.js";
class Q extends Y {
  constructor() {
    super(), this.createDataLayer = this.createDataLayer.bind(this);
  }
  createDataLayer(t) {
    const {
      format: i,
      path: s,
      tooltip: u,
      markFillColor: n,
      markBorderColor: m,
      markSizeScale: g,
      //circle size
      featureJoinAttribute: e,
      apiJoinAttribute: v,
      projection: o,
      breaks: l,
      markSizeScale2: p,
      //arrow size
      measures: f,
      zoom: k,
      offsetPixels: x = 10,
      waitForFilters: $
    } = this.props, _ = f[0], D = new K({
      breaks: l,
      defaultFillColor: n,
      defaultBorderColor: m,
      defaultSize: p
    });
    i.style === "compacted" || i.style, i.style, i.currency, parseInt(i.minimumFractionDigits), parseInt(i.maximumFractionDigits);
    const I = t.features.filter((r) => r.properties._value != null);
    this.g = T.select(this.gRef.current), this.g.attr("class", "base-layer zoomable flow"), this.props.transform && this.g.attr("transform", this.props.transform), this.g.selectAll(".flow-line").remove(), this.g.selectAll(".start-point").remove(), this.g.selectAll(".end-point").remove(), this.g.select("defs").selectAll("*").remove();
    const B = this.props.transform ? this.props.transform.k : 1, z = [];
    I.forEach((r) => {
      z.push(r), r.properties.destinations.sort((h, y) => h[_] - y[_]).forEach((h) => {
        const y = h[_];
        t.features.filter((a) => a.properties[e] == h.value).forEach((a) => {
          a.properties.meta = h;
          const M = r.properties[e], w = r.properties[e] + "--" + a.properties[e], E = s.centroid(r), A = s.centroid(a), F = A[0] - E[0], j = A[1] - E[1], O = Math.sqrt(F * F + j * j), V = F / O, J = j / O, N = [
            A[0] - V * x,
            A[1] - J * x
          ], R = o.invert(N), U = {
            type: "LineString",
            coordinates: [
              o.invert(E),
              // Start in geo coords
              R
              // New endpoint before d2
            ]
          }, c = this.g;
          this.g.select("defs").append("marker").attr("id", "arrow" + w).attr("markerUnits", "strokeWidth").attr("markerWidth", "6").attr("markerHeight", "6").attr("viewBox", "0 0 24 24").attr("refX", "6").attr("refY", "6").attr("orient", "auto").append("path").attr("d", "M2,2 L10,6 L2,10 L6,6 L2,2").attr("d", "M2,2 L10,6 L2,10 L6,6 L2,2").attr("style", (L) => "fill: " + D.getColor(y) + ";"), c.append("path").attr("d", s(U)).attr("id", w).attr("class", "flow-line").style("fill", "none").style("cursor", "pointer").style("stroke-dasharray", "0").style("stroke", (L) => D.getColor(y)).style("stroke-width", (L) => D.getSize(y)).attr("marker-end", "url(#arrow" + w + ")").on("mouseenter", (L, tt) => {
            if (c.selectAll("marker").transition().duration("200").style("opacity", 0), c.selectAll(".start-point").transition().duration("200").style("opacity", 0), c.selectAll(".flow-line").transition().duration("200").style("opacity", 0), T.select(L.target).transition().duration("200").style("opacity", 1), c.selectAll("#arrow" + w).transition().duration("200").style("opacity", 1), c.selectAll(".start-point.circle_" + M).transition().duration("200").style("opacity", 1), y) {
              const P = {}, C = {};
              Object.keys(r.properties).forEach((d) => {
                P["origin_" + d] = r.properties[d];
              }), Object.keys(a.properties).forEach((d) => {
                C["target_" + d] = a.properties[d];
              }), Object.keys(a.properties.meta).forEach((d) => {
                C["target_" + d] = a.properties.meta[d];
              });
              const W = {
                ...P,
                ...C,
                meta: {
                  [v]: r.properties.meta ? r.properties.meta.value : "",
                  ...r.properties.meta,
                  value: y
                }
              };
              this.showToolTip(u, W, D.getColor(a.properties._value));
            }
          }).on("mouseout", (L) => {
            this.hiddenToolTip(), T.selectAll(".flow-line").transition().duration("100").style("opacity", 1), c.selectAll(".start-point").transition().duration("100").style("opacity", 1), c.selectAll("marker").transition().duration("100").style("opacity", 1);
          }), c.append("text").append("textPath").attr("xlink:href", w).style("text-anchor", "middle").attr("startOffset", "50%").attr("fill", "#fff").text("Yay, my text is on a wavy path");
        });
      });
    }), z.forEach((r) => {
      this.g.append("circle").attr("fill", n).attr("stroke", m).attr("class", "start-point circle_" + r.properties[e]).attr("stroke-width", 2).style("vector-effect", "non-scaling-stroke").attr("cx", s.centroid(r)[0]).attr("cy", s.centroid(r)[1]).attr("r", () => g * 1 / B).on("mouseenter", (h) => {
        this.showToolTip("{name_en}", r.properties, "");
      }).on("mouseout", (h) => {
        this.hiddenToolTip();
      });
    });
  }
  _buildLayer(t) {
    const {
      featureJoinAttribute: i,
      app: s,
      data: u,
      measures: n,
      csv: m
    } = this.props, g = t.features.map((e) => {
      const v = e.properties[i];
      if (s != "csv" && u && u.children) {
        const o = u.children.filter((l) => l.value.indexOf(v) > -1);
        if (o.length > 0) {
          const l = o[0][n[0]];
          e.properties.meta = o[0], e.properties._value = l, e.properties.destinations = o[0].children;
        }
      } else if (s == "csv") {
        const o = X.parse(m, { header: !0, dynamicTyping: !0 }), l = e.properties[i], p = o.data.filter((f) => f.origin == l)[0];
        p != null && (alert("CSV Not implemented Yet, please do it if you have time"), e.properties.meta = p, e.properties._value = p.value, e.properties.destinations = p.destination);
      }
      return e;
    });
    this.createDataLayer({ ...t, features: g });
  }
  create() {
    const { file: t } = this.props;
    t != "none" && (this.state.json ? this._buildLayer(this.state.json) : this.loadJSON(t).then((i) => {
      this.setState({ json: i }), this._buildLayer(i);
    }));
  }
  componentDidUpdate(t, i, s) {
    const { projection: u, editing: n, data: m } = this.props;
    (n || t.data !== m || t.path !== this.props.path) && this.create(), t.visible != this.props.visible && this.g.style("display", this.props.visible ? "" : "none");
  }
  componentDidMount() {
    this.create(), this.props.zoom.current.fullView();
  }
  render() {
    const { id: t } = this.props;
    return /* @__PURE__ */ b("g", { id: "data-" + t, className: "data " + t, ref: this.gRef, children: /* @__PURE__ */ b("defs", {}) });
  }
}
const Z = (S) => {
  const {
    id: t,
    unique: i,
    filters: s,
    csv: u,
    app: n,
    group: m = "default",
    flowOrigin: g,
    editing: e,
    flowDestination: v,
    dvzProxyDatasetId: o,
    waitForFilters: l
  } = S, p = { dvzProxyDatasetId: o }, f = s || {};
  return f && f.forEach && f.forEach((k) => {
    k.value != null && k.value.filter((x) => x != null && x.toString().trim() != "").length > 0 && (p[k.param] = k.value);
  }), /* @__PURE__ */ b(
    q,
    {
      editing: e,
      params: p,
      waitForFilters: l,
      app: n,
      csv: decodeURIComponent(u),
      group: m,
      ignoreErrors: !0,
      isSvg: !0,
      store: [n, i, t],
      source: g + "/" + v,
      children: /* @__PURE__ */ b(G, { children: /* @__PURE__ */ b(Q, { ...S }) })
    }
  );
}, pt = H(Z);
export {
  pt as default
};
