function count(e2) {
  var d = 0, f = e2.children, p = f && f.length;
  if (!p) d = 1;
  else for (; --p >= 0; ) d += f[p].value;
  e2.value = d;
}
function count_default() {
  return this.eachAfter(count);
}
function each_default(e2, d) {
  let f = -1;
  for (let p of this) e2.call(d, p, ++f, this);
  return this;
}
function eachBefore_default(e2, d) {
  for (var f = this, p = [f], m, h, g = -1; f = p.pop(); ) if (e2.call(d, f, ++g, this), m = f.children) for (h = m.length - 1; h >= 0; --h) p.push(m[h]);
  return this;
}
function eachAfter_default(e2, d) {
  for (var f = this, p = [f], m = [], h, g, _, v = -1; f = p.pop(); ) if (m.push(f), h = f.children) for (g = 0, _ = h.length; g < _; ++g) p.push(h[g]);
  for (; f = m.pop(); ) e2.call(d, f, ++v, this);
  return this;
}
function find_default(e2, d) {
  let f = -1;
  for (let p of this) if (e2.call(d, p, ++f, this)) return p;
}
function sum_default(e2) {
  return this.eachAfter(function(d) {
    for (var f = +e2(d.data) || 0, p = d.children, m = p && p.length; --m >= 0; ) f += p[m].value;
    d.value = f;
  });
}
function sort_default(e2) {
  return this.eachBefore(function(d) {
    d.children && d.children.sort(e2);
  });
}
function path_default(e2) {
  for (var d = this, f = leastCommonAncestor(d, e2), p = [d]; d !== f; ) d = d.parent, p.push(d);
  for (var m = p.length; e2 !== f; ) p.splice(m, 0, e2), e2 = e2.parent;
  return p;
}
function leastCommonAncestor(e2, d) {
  if (e2 === d) return e2;
  var f = e2.ancestors(), p = d.ancestors(), m = null;
  for (e2 = f.pop(), d = p.pop(); e2 === d; ) m = e2, e2 = f.pop(), d = p.pop();
  return m;
}
function ancestors_default() {
  for (var e2 = this, d = [e2]; e2 = e2.parent; ) d.push(e2);
  return d;
}
function descendants_default() {
  return Array.from(this);
}
function leaves_default() {
  var e2 = [];
  return this.eachBefore(function(d) {
    d.children || e2.push(d);
  }), e2;
}
function links_default() {
  var e2 = this, d = [];
  return e2.each(function(f) {
    f !== e2 && d.push({
      source: f.parent,
      target: f
    });
  }), d;
}
function* iterator_default() {
  var e2 = this, d, f = [e2], p, m, h;
  do
    for (d = f.reverse(), f = []; e2 = d.pop(); ) if (yield e2, p = e2.children) for (m = 0, h = p.length; m < h; ++m) f.push(p[m]);
  while (f.length);
}
function hierarchy(e2, d) {
  e2 instanceof Map ? (e2 = [void 0, e2], d === void 0 && (d = mapChildren)) : d === void 0 && (d = objectChildren);
  for (var f = new Node(e2), p, m = [f], h, g, _, v; p = m.pop(); ) if ((g = d(p.data)) && (v = (g = Array.from(g)).length)) for (p.children = g, _ = v - 1; _ >= 0; --_) m.push(h = g[_] = new Node(g[_])), h.parent = p, h.depth = p.depth + 1;
  return f.eachBefore(computeHeight);
}
function node_copy() {
  return hierarchy(this).eachBefore(copyData);
}
function objectChildren(e2) {
  return e2.children;
}
function mapChildren(e2) {
  return Array.isArray(e2) ? e2[1] : null;
}
function copyData(e2) {
  e2.data.value !== void 0 && (e2.value = e2.data.value), e2.data = e2.data.data;
}
function computeHeight(e2) {
  var d = 0;
  do
    e2.height = d;
  while ((e2 = e2.parent) && e2.height < ++d);
}
function Node(e2) {
  this.data = e2, this.depth = this.height = 0, this.parent = null;
}
Node.prototype = hierarchy.prototype = {
  constructor: Node,
  count: count_default,
  each: each_default,
  eachAfter: eachAfter_default,
  eachBefore: eachBefore_default,
  find: find_default,
  sum: sum_default,
  sort: sort_default,
  path: path_default,
  ancestors: ancestors_default,
  descendants: descendants_default,
  leaves: leaves_default,
  links: links_default,
  copy: node_copy,
  [Symbol.iterator]: iterator_default
};
function optional(e2) {
  return e2 == null ? null : required(e2);
}
function required(e2) {
  if (typeof e2 != "function") throw Error();
  return e2;
}
function constantZero() {
  return 0;
}
function constant_default(e2) {
  return function() {
    return e2;
  };
}
function round_default(e2) {
  e2.x0 = Math.round(e2.x0), e2.y0 = Math.round(e2.y0), e2.x1 = Math.round(e2.x1), e2.y1 = Math.round(e2.y1);
}
function dice_default(e2, d, f, p, m) {
  for (var h = e2.children, g, _ = -1, v = h.length, y = e2.value && (p - d) / e2.value; ++_ < v; ) g = h[_], g.y0 = f, g.y1 = m, g.x0 = d, g.x1 = d += g.value * y;
}
function slice_default(e2, d, f, p, m) {
  for (var h = e2.children, g, _ = -1, v = h.length, y = e2.value && (m - f) / e2.value; ++_ < v; ) g = h[_], g.x0 = d, g.x1 = p, g.y0 = f, g.y1 = f += g.value * y;
}
var phi = (1 + Math.sqrt(5)) / 2;
function squarifyRatio(e2, d, f, p, m, h) {
  for (var g = [], _ = d.children, v, y, b = 0, x = 0, S = _.length, C, w, T = d.value, E, D, O, k, A, j, M; b < S; ) {
    C = m - f, w = h - p;
    do
      E = _[x++].value;
    while (!E && x < S);
    for (D = O = E, j = Math.max(w / C, C / w) / (T * e2), M = E * E * j, A = Math.max(O / M, M / D); x < S; ++x) {
      if (E += y = _[x].value, y < D && (D = y), y > O && (O = y), M = E * E * j, k = Math.max(O / M, M / D), k > A) {
        E -= y;
        break;
      }
      A = k;
    }
    g.push(v = {
      value: E,
      dice: C < w,
      children: _.slice(b, x)
    }), v.dice ? dice_default(v, f, p, m, T ? p += w * E / T : h) : slice_default(v, f, p, T ? f += C * E / T : m, h), T -= E, b = x;
  }
  return g;
}
var squarify_default = (function e(d) {
  function f(e2, f2, p, m, h) {
    squarifyRatio(d, e2, f2, p, m, h);
  }
  return f.ratio = function(d2) {
    return e((d2 = +d2) > 1 ? d2 : 1);
  }, f;
})(phi);
function treemap_default() {
  var e2 = squarify_default, d = false, f = 1, p = 1, m = [0], h = constantZero, g = constantZero, _ = constantZero, v = constantZero, y = constantZero;
  function b(e3) {
    return e3.x0 = e3.y0 = 0, e3.x1 = f, e3.y1 = p, e3.eachBefore(x), m = [0], d && e3.eachBefore(round_default), e3;
  }
  function x(d2) {
    var f2 = m[d2.depth], p2 = d2.x0 + f2, b2 = d2.y0 + f2, x2 = d2.x1 - f2, S = d2.y1 - f2;
    x2 < p2 && (p2 = x2 = (p2 + x2) / 2), S < b2 && (b2 = S = (b2 + S) / 2), d2.x0 = p2, d2.y0 = b2, d2.x1 = x2, d2.y1 = S, d2.children && (f2 = m[d2.depth + 1] = h(d2) / 2, p2 += y(d2) - f2, b2 += g(d2) - f2, x2 -= _(d2) - f2, S -= v(d2) - f2, x2 < p2 && (p2 = x2 = (p2 + x2) / 2), S < b2 && (b2 = S = (b2 + S) / 2), e2(d2, p2, b2, x2, S));
  }
  return b.round = function(e3) {
    return arguments.length ? (d = !!e3, b) : d;
  }, b.size = function(e3) {
    return arguments.length ? (f = +e3[0], p = +e3[1], b) : [f, p];
  }, b.tile = function(d2) {
    return arguments.length ? (e2 = required(d2), b) : e2;
  }, b.padding = function(e3) {
    return arguments.length ? b.paddingInner(e3).paddingOuter(e3) : b.paddingInner();
  }, b.paddingInner = function(e3) {
    return arguments.length ? (h = typeof e3 == "function" ? e3 : constant_default(+e3), b) : h;
  }, b.paddingOuter = function(e3) {
    return arguments.length ? b.paddingTop(e3).paddingRight(e3).paddingBottom(e3).paddingLeft(e3) : b.paddingTop();
  }, b.paddingTop = function(e3) {
    return arguments.length ? (g = typeof e3 == "function" ? e3 : constant_default(+e3), b) : g;
  }, b.paddingRight = function(e3) {
    return arguments.length ? (_ = typeof e3 == "function" ? e3 : constant_default(+e3), b) : _;
  }, b.paddingBottom = function(e3) {
    return arguments.length ? (v = typeof e3 == "function" ? e3 : constant_default(+e3), b) : v;
  }, b.paddingLeft = function(e3) {
    return arguments.length ? (y = typeof e3 == "function" ? e3 : constant_default(+e3), b) : y;
  }, b;
}
export {
  slice_default as a,
  constantZero as c,
  Node as d,
  computeHeight as f,
  squarify_default as i,
  constant_default as l,
  phi as n,
  dice_default as o,
  hierarchy as p,
  squarifyRatio as r,
  round_default as s,
  treemap_default as t,
  optional as u
};
