import x from "./type.js";
const { log: m, pow: L, floor: _, abs: O } = Math;
function k(u, h = null) {
  const e = {
    min: Number.MAX_VALUE,
    max: Number.MAX_VALUE * -1,
    sum: 0,
    values: [],
    count: 0
  };
  return x(u) === "object" && (u = Object.values(u)), u.forEach((r) => {
    h && x(r) === "object" && (r = r[h]), r != null && !isNaN(r) && (e.values.push(r), e.sum += r, r < e.min && (e.min = r), r > e.max && (e.max = r), e.count += 1);
  }), e.domain = [e.min, e.max], e.limits = (r, n) => U(e, r, n), e;
}
function U(u, h = "equal", e = 7) {
  x(u) == "array" && (u = k(u));
  const { min: r, max: n } = u, c = u.values.sort((i, f) => i - f);
  if (e === 1)
    return [r, n];
  const l = [];
  if (h.substr(0, 1) === "c" && (l.push(r), l.push(n)), h.substr(0, 1) === "e") {
    l.push(r);
    for (let i = 1; i < e; i++)
      l.push(r + i / e * (n - r));
    l.push(n);
  } else if (h.substr(0, 1) === "l") {
    if (r <= 0)
      throw new Error(
        "Logarithmic scales are only possible for values > 0"
      );
    const i = Math.LOG10E * m(r), f = Math.LOG10E * m(n);
    l.push(r);
    for (let o = 1; o < e; o++)
      l.push(L(10, i + o / e * (f - i)));
    l.push(n);
  } else if (h.substr(0, 1) === "q") {
    l.push(r);
    for (let i = 1; i < e; i++) {
      const f = (c.length - 1) * i / e, o = _(f);
      if (o === f)
        l.push(c[o]);
      else {
        const a = f - o;
        l.push(c[o] * (1 - a) + c[o + 1] * a);
      }
    }
    l.push(n);
  } else if (h.substr(0, 1) === "k") {
    let i;
    const f = c.length, o = new Array(f), a = new Array(e);
    let g = !0, E = 0, p = null;
    p = [], p.push(r);
    for (let t = 1; t < e; t++)
      p.push(r + t / e * (n - r));
    for (p.push(n); g; ) {
      for (let s = 0; s < e; s++)
        a[s] = 0;
      for (let s = 0; s < f; s++) {
        const y = c[s];
        let M = Number.MAX_VALUE, w;
        for (let A = 0; A < e; A++) {
          const N = O(p[A] - y);
          N < M && (M = N, w = A), a[w]++, o[s] = w;
        }
      }
      const t = new Array(e);
      for (let s = 0; s < e; s++)
        t[s] = null;
      for (let s = 0; s < f; s++)
        i = o[s], t[i] === null ? t[i] = c[s] : t[i] += c[s];
      for (let s = 0; s < e; s++)
        t[s] *= 1 / a[s];
      g = !1;
      for (let s = 0; s < e; s++)
        if (t[s] !== p[s]) {
          g = !0;
          break;
        }
      p = t, E++, E > 200 && (g = !1);
    }
    const j = {};
    for (let t = 0; t < e; t++)
      j[t] = [];
    for (let t = 0; t < f; t++)
      i = o[t], j[i].push(c[t]);
    let b = [];
    for (let t = 0; t < e; t++)
      b.push(j[t][0]), b.push(j[t][j[t].length - 1]);
    b = b.sort((t, s) => t - s), l.push(b[0]);
    for (let t = 1; t < b.length; t += 2) {
      const s = b[t];
      !isNaN(s) && l.indexOf(s) === -1 && l.push(s);
    }
  }
  return l;
}
export {
  k as analyze,
  U as limits
};
//# sourceMappingURL=analyze.js.map
