"use client";
import { jsx as D } from "react/jsx-runtime";
import { forwardRef as ee, useRef as y, useState as P, useCallback as v, useEffect as b, useImperativeHandle as te } from "react";
import { cn as x } from "../../utils/cn/cn.es.js";
import { useIsMobile as ne } from "../../utils/useIsMobile.es.js";
import z from "../../components/InfiniteScroll/infiniteScroll.module.css";
function oe({
  items: p,
  speed: R = 50,
  direction: N = "left",
  contentClassName: $,
  itemClassName: B,
  rootClassName: G,
  slowOnTouch: j = !1,
  enableManualScroll: S = !1,
  renderItem: J
}, K) {
  const E = y(null), A = y(null), C = y(null), q = y(!1), X = y(performance.now()), [d, f] = P(0), [s, O] = P(0), [m, Q] = P(4), [k, H] = P(!1), U = ne(), M = v(
    (e) => {
      if (s === 0) return e;
      const t = s;
      let n = e;
      for (; n <= -t; )
        n += t;
      for (; n > 0; )
        n -= t;
      return n;
    },
    [s]
  ), F = v(() => {
    if (!A.current || !E.current || p.length === 0) return;
    const e = A.current, t = e.children.length;
    if (t === 0) return;
    let n = 0;
    for (let r = 0; r < Math.min(p.length, t); r++) {
      const u = e.children[r];
      n += u.offsetWidth;
    }
    if (n === 0 && (n = e.scrollWidth / m), n <= 0)
      return;
    const c = E.current.offsetWidth, i = Math.ceil(c * 2.5 / n), a = Math.max(4, Math.min(20, i % 2 === 0 ? i : i + 1)), l = s, h = m;
    if (Q(a), O(n), l > 0 && h > 0) {
      const r = Math.floor(h / 2), u = Math.floor(a / 2), T = (d + l * r) / l, o = -n * u + T * n;
      f(o);
    } else {
      const r = Math.floor(a / 2);
      f(-n * r);
    }
  }, [p.length, m, s, d]);
  b(() => {
    F();
    const e = new ResizeObserver(() => {
      F();
    });
    return E.current && e.observe(E.current), () => e.disconnect();
  }, [F]);
  const w = v(
    (e) => {
      if (q.current || s === 0) {
        C.current = requestAnimationFrame(w);
        return;
      }
      const t = e != null ? e : performance.now(), n = t - X.current || 16.67;
      X.current = t, f((c) => {
        const i = k ? R * 0.5 : R, a = N === "right" ? -i : i;
        let l = c - a * (n / 1e3);
        return M(l);
      }), C.current = requestAnimationFrame(w);
    },
    [N, R, s, k, M]
  );
  b(() => (X.current = performance.now(), C.current = requestAnimationFrame(w), () => {
    C.current && cancelAnimationFrame(C.current);
  }), [w]);
  const W = v(
    (e, t) => {
      !(t === "mouse") && !(t === "touch" && U) || (e ? j ? H(!0) : q.current = !0 : (q.current = !1, H(!1), X.current = performance.now()));
    },
    [U, j]
  ), V = v(
    (e) => {
      W(!0, e.pointerType);
    },
    [W]
  ), Y = v(
    (e) => {
      W(!1, e.pointerType);
    },
    [W]
  ), _ = v(
    (e, t = 300) => {
      const n = d, c = e - n, i = performance.now(), a = (l) => {
        const h = l - i, r = Math.min(h / t, 1), u = 1 - Math.pow(1 - r, 3);
        f(n + c * u), r < 1 && requestAnimationFrame(a);
      };
      requestAnimationFrame(a);
    },
    [d]
  );
  return b(() => {
    if (!S) return;
    const e = E.current;
    if (!e) return;
    let t = null, n = !1, c = null;
    const i = (o) => {
      if (o.deltaX !== 0) {
        o.preventDefault();
        const L = o.deltaX * 2;
        f((g) => M(g - L));
      }
    }, a = (o) => {
      n = !0, c = o.clientX, o.preventDefault();
    }, l = (o) => {
      if (!n || c === null) return;
      const L = o.clientX, g = L - c;
      c = L, f((I) => M(I + g));
    }, h = () => {
      n = !1, c = null;
    }, r = (o) => {
      t = o.touches[0].clientX;
    }, u = (o) => {
      if (t === null) return;
      o.preventDefault();
      const g = o.touches[0].clientX, I = g - t;
      t = g, f((Z) => M(Z + I));
    }, T = () => {
      t = null;
    };
    return e.addEventListener("wheel", i, { passive: !1 }), e.addEventListener("mousedown", a, { passive: !1 }), e.addEventListener("mousemove", l, { passive: !1 }), e.addEventListener("touchstart", r), e.addEventListener("touchmove", u, { passive: !1 }), e.addEventListener("touchend", T), document.addEventListener("mouseup", h), document.addEventListener("mousemove", l), () => {
      e.removeEventListener("wheel", i), e.removeEventListener("mousedown", a), e.removeEventListener("mousemove", l), e.removeEventListener("touchstart", r), e.removeEventListener("touchmove", u), e.removeEventListener("touchend", T), document.removeEventListener("mouseup", h), document.removeEventListener("mousemove", l);
    };
  }, [S, M]), te(K, () => ({
    scrollLeft: () => {
      const e = s * m, t = -s * Math.floor(m / 2);
      let n = d - 200;
      n < -e + t && (n += s), _(n);
    },
    scrollRight: () => {
      const e = -s * Math.floor(m / 2);
      let t = d + 200;
      t > e && (t -= s), _(t);
    }
  })), /* @__PURE__ */ D(
    "div",
    {
      className: x(z.root, G),
      ref: E,
      "data-manual-scroll": S,
      onPointerEnter: V,
      onPointerLeave: Y,
      children: /* @__PURE__ */ D(
        "div",
        {
          className: x(z.content, $),
          ref: A,
          style: { transform: `translate3d(${d}px, 0, 0)` },
          children: Array.from({ length: p.length * m }, (e, t) => /* @__PURE__ */ D("div", { className: x(z.item, B), children: J(p[t % p.length], t) }, t))
        }
      )
    }
  );
}
const ae = ee(oe);
export {
  ae as InfiniteScroll
};
