import { justify as _ } from "./align.js";
import v from "./constant.js";
import z from "../../../../d3-array@2.12.1/node_modules/d3-array/src/sum.js";
import R from "../../../../d3-array@2.12.1/node_modules/d3-array/src/max.js";
import tt from "../../../../d3-array@2.12.1/node_modules/d3-array/src/min.js";
function D(s, c) {
  return S(s.source, c.source) || s.index - c.index;
}
function H(s, c) {
  return S(s.target, c.target) || s.index - c.index;
}
function S(s, c) {
  return s.y0 - c.y0;
}
function C(s) {
  return s.value;
}
function nt(s) {
  return s.index;
}
function et(s) {
  return s.nodes;
}
function ot(s) {
  return s.links;
}
function P(s, c) {
  const a = s.get(c);
  if (!a) throw new Error("missing: " + c);
  return a;
}
function W({ nodes: s }) {
  for (const c of s) {
    let a = c.y0, g = a;
    for (const d of c.sourceLinks)
      d.y0 = a + d.width / 2, a += d.width;
    for (const d of c.targetLinks)
      d.y1 = g + d.width / 2, g += d.width;
  }
}
function ct() {
  let s = 0, c = 0, a = 1, g = 1, d = 24, B = 8, l, N = nt, T = _, k, h, M = et, b = ot, w = 6;
  function f() {
    const t = { nodes: M.apply(null, arguments), links: b.apply(null, arguments) };
    return q(t), F(t), G(t), J(t), Q(t), W(t), t;
  }
  f.update = function(t) {
    return W(t), t;
  }, f.nodeId = function(t) {
    return arguments.length ? (N = typeof t == "function" ? t : v(t), f) : N;
  }, f.nodeAlign = function(t) {
    return arguments.length ? (T = typeof t == "function" ? t : v(t), f) : T;
  }, f.nodeSort = function(t) {
    return arguments.length ? (k = t, f) : k;
  }, f.nodeWidth = function(t) {
    return arguments.length ? (d = +t, f) : d;
  }, f.nodePadding = function(t) {
    return arguments.length ? (B = l = +t, f) : B;
  }, f.nodes = function(t) {
    return arguments.length ? (M = typeof t == "function" ? t : v(t), f) : M;
  }, f.links = function(t) {
    return arguments.length ? (b = typeof t == "function" ? t : v(t), f) : b;
  }, f.linkSort = function(t) {
    return arguments.length ? (h = t, f) : h;
  }, f.size = function(t) {
    return arguments.length ? (s = c = 0, a = +t[0], g = +t[1], f) : [a - s, g - c];
  }, f.extent = function(t) {
    return arguments.length ? (s = +t[0][0], a = +t[1][0], c = +t[0][1], g = +t[1][1], f) : [[s, c], [a, g]];
  }, f.iterations = function(t) {
    return arguments.length ? (w = +t, f) : w;
  };
  function q({ nodes: t, links: r }) {
    for (const [o, n] of t.entries())
      n.index = o, n.sourceLinks = [], n.targetLinks = [];
    const e = new Map(t.map((o, n) => [N(o, n, t), o]));
    for (const [o, n] of r.entries()) {
      n.index = o;
      let { source: i, target: u } = n;
      typeof i != "object" && (i = n.source = P(e, i)), typeof u != "object" && (u = n.target = P(e, u)), i.sourceLinks.push(n), u.targetLinks.push(n);
    }
    if (h != null)
      for (const { sourceLinks: o, targetLinks: n } of t)
        o.sort(h), n.sort(h);
  }
  function F({ nodes: t }) {
    for (const r of t)
      r.value = r.fixedValue === void 0 ? Math.max(z(r.sourceLinks, C), z(r.targetLinks, C)) : r.fixedValue;
  }
  function G({ nodes: t }) {
    const r = t.length;
    let e = new Set(t), o = /* @__PURE__ */ new Set(), n = 0;
    for (; e.size; ) {
      for (const i of e) {
        i.depth = n;
        for (const { target: u } of i.sourceLinks)
          o.add(u);
      }
      if (++n > r) throw new Error("circular link");
      e = o, o = /* @__PURE__ */ new Set();
    }
  }
  function J({ nodes: t }) {
    const r = t.length;
    let e = new Set(t), o = /* @__PURE__ */ new Set(), n = 0;
    for (; e.size; ) {
      for (const i of e) {
        i.height = n;
        for (const { source: u } of i.targetLinks)
          o.add(u);
      }
      if (++n > r) throw new Error("circular link");
      e = o, o = /* @__PURE__ */ new Set();
    }
  }
  function K({ nodes: t }) {
    const r = R(t, (n) => n.depth) + 1, e = (a - s - d) / (r - 1), o = new Array(r);
    for (const n of t) {
      const i = Math.max(0, Math.min(r - 1, Math.floor(T.call(null, n, r))));
      n.layer = i, n.x0 = s + i * e, n.x1 = n.x0 + d, o[i] ? o[i].push(n) : o[i] = [n];
    }
    if (k) for (const n of o)
      n.sort(k);
    return o;
  }
  function O(t) {
    const r = tt(t, (e) => (g - c - (e.length - 1) * l) / z(e, C));
    for (const e of t) {
      let o = c;
      for (const n of e) {
        n.y0 = o, n.y1 = o + n.value * r, o = n.y1 + l;
        for (const i of n.sourceLinks)
          i.width = i.value * r;
      }
      o = (g - o + l) / (e.length + 1);
      for (let n = 0; n < e.length; ++n) {
        const i = e[n];
        i.y0 += o * (n + 1), i.y1 += o * (n + 1);
      }
      Y(e);
    }
  }
  function Q(t) {
    const r = K(t);
    l = Math.min(B, (g - c) / (R(r, (e) => e.length) - 1)), O(r);
    for (let e = 0; e < w; ++e) {
      const o = Math.pow(0.99, e), n = Math.max(1 - o, (e + 1) / w);
      X(r, o, n), U(r, o, n);
    }
  }
  function U(t, r, e) {
    for (let o = 1, n = t.length; o < n; ++o) {
      const i = t[o];
      for (const u of i) {
        let m = 0, y = 0;
        for (const { source: L, value: j } of u.targetLinks) {
          let x = j * (u.layer - L.layer);
          m += Z(L, u) * x, y += x;
        }
        if (!(y > 0)) continue;
        let p = (m / y - u.y0) * r;
        u.y0 += p, u.y1 += p, A(u);
      }
      k === void 0 && i.sort(S), E(i, e);
    }
  }
  function X(t, r, e) {
    for (let o = t.length, n = o - 2; n >= 0; --n) {
      const i = t[n];
      for (const u of i) {
        let m = 0, y = 0;
        for (const { target: L, value: j } of u.sourceLinks) {
          let x = j * (L.layer - u.layer);
          m += $(u, L) * x, y += x;
        }
        if (!(y > 0)) continue;
        let p = (m / y - u.y0) * r;
        u.y0 += p, u.y1 += p, A(u);
      }
      k === void 0 && i.sort(S), E(i, e);
    }
  }
  function E(t, r) {
    const e = t.length >> 1, o = t[e];
    V(t, o.y0 - l, e - 1, r), I(t, o.y1 + l, e + 1, r), V(t, g, t.length - 1, r), I(t, c, 0, r);
  }
  function I(t, r, e, o) {
    for (; e < t.length; ++e) {
      const n = t[e], i = (r - n.y0) * o;
      i > 1e-6 && (n.y0 += i, n.y1 += i), r = n.y1 + l;
    }
  }
  function V(t, r, e, o) {
    for (; e >= 0; --e) {
      const n = t[e], i = (n.y1 - r) * o;
      i > 1e-6 && (n.y0 -= i, n.y1 -= i), r = n.y0 - l;
    }
  }
  function A({ sourceLinks: t, targetLinks: r }) {
    if (h === void 0) {
      for (const { source: { sourceLinks: e } } of r)
        e.sort(H);
      for (const { target: { targetLinks: e } } of t)
        e.sort(D);
    }
  }
  function Y(t) {
    if (h === void 0)
      for (const { sourceLinks: r, targetLinks: e } of t)
        r.sort(H), e.sort(D);
  }
  function Z(t, r) {
    let e = t.y0 - (t.sourceLinks.length - 1) * l / 2;
    for (const { target: o, width: n } of t.sourceLinks) {
      if (o === r) break;
      e += n + l;
    }
    for (const { source: o, width: n } of r.targetLinks) {
      if (o === t) break;
      e -= n;
    }
    return e;
  }
  function $(t, r) {
    let e = r.y0 - (r.targetLinks.length - 1) * l / 2;
    for (const { source: o, width: n } of r.targetLinks) {
      if (o === t) break;
      e += n + l;
    }
    for (const { target: o, width: n } of t.sourceLinks) {
      if (o === r) break;
      e -= n;
    }
    return e;
  }
  return f;
}
export {
  ct as default
};
