function L(t, e, n, s) {
  return parseFloat((e - s * e / t + n).toFixed(2));
}
function q(t) {
  [...t.querySelectorAll("*")].forEach((e) => t.removeChild(e));
}
function H(t) {
  return t.value;
}
function p(t, e) {
  const n = document.createElementNS("http://www.w3.org/2000/svg", t);
  for (const s in e)
    n.setAttribute(s, e[s]);
  return n;
}
function N(t, e = [], n = {}) {
  if (q(t), e.length <= 1)
    return;
  let s = [];
  typeof e[0] == "number" ? s = e.map((o) => ({ value: o })) : s = e;
  const { onmousemove: y } = n, { onmouseout: w } = n, D = "interactive" in n ? n.interactive : !!y, m = n.spotRadius || 2, l = m * 2, E = n.cursorWidth || 2, k = parseFloat(t.attributes["stroke-width"].value), M = n.fetch || H, u = s.map((o) => M(o)), P = parseFloat(t.attributes.width.value) - l * 2, x = parseFloat(t.attributes.height.value), $ = x - k * 2 - l, v = Math.max(...u) === 0 ? 1 : Math.max(...u), i = -1e3, g = u.length - 1, W = P / g, h = [], I = L(v, $, k + m, u[0]);
  let A = `M${l} ${I}`;
  u.forEach((o, c) => {
    const r = c * W + l, a = L(v, $, k + m, o);
    h.push(
      Object.assign({}, s[c], {
        index: c,
        x: r,
        y: a
      })
    ), A += ` L ${r} ${a}`;
  });
  const O = p("path", {
    class: "sparkline--line",
    d: A,
    fill: "none"
  }), R = `${A} V ${x} L ${l} ${x} Z`, X = p("path", {
    class: "sparkline--fill",
    d: R,
    stroke: "none"
  });
  if (t.appendChild(X), t.appendChild(O), !D)
    return;
  const d = p("line", {
    class: "sparkline--cursor",
    x1: i,
    x2: i,
    y1: 0,
    y2: x,
    "stroke-width": E
  }), b = p("circle", {
    class: "sparkline--spot",
    cx: i,
    cy: i,
    r: m
  });
  t.appendChild(d), t.appendChild(b);
  const C = p("rect", {
    // @ts-expect-error
    width: t.attributes.width.value,
    // @ts-expect-error
    height: t.attributes.height.value,
    style: "fill: transparent; stroke: transparent",
    class: "sparkline--interaction-layer"
  });
  t.appendChild(C), C.addEventListener("mouseout", (o) => {
    d.setAttribute("x1", i.toString()), d.setAttribute("x2", i.toString()), b.setAttribute("cx", i.toString()), w && w(o);
  }), C.addEventListener("mousemove", (o) => {
    const c = o.offsetX;
    let r = h.find((j) => j.x >= c);
    r || (r = h[g]);
    const a = h[h.indexOf(r) - 1];
    let f, F;
    a ? (F = a.x + (r.x - a.x) / 2, f = c >= F ? r : a) : f = r;
    const { x: S } = f, { y: Y } = f;
    b.setAttribute("cx", S.toString()), b.setAttribute("cy", Y.toString()), d.setAttribute("x1", S.toString()), d.setAttribute("x2", S.toString()), y && y(o, f);
  });
}
export {
  N as default
};
