import { useGesture as K } from "./index-use-gesture-react.esm.js";
import { useState as N, useEffect as g, useMemo as O, useRef as P } from "react";
import "./index-react-spring_web.modern.js";
import { useSpring as Q } from "./index-react-spring_core.modern.js";
const I = (s, n, p) => Math.min(Math.max(s, n), p), U = (s, n) => Math.max(0, s - n), L = (s) => {
  const n = P(s);
  return n.current = s, n;
}, tt = ({
  activeIndex: s,
  auto: n,
  initialIndex: p,
  length: c,
  onSlideChange: w,
  setSlideCustom: M,
  slideGap: h,
  slidesAtOnce: u,
  slidesToSlide: l,
  viewportRef: x
}) => {
  const o = U(c, u), k = () => {
    var a;
    const t = ((a = x.current) == null ? void 0 : a.getBoundingClientRect().width) ?? 0, r = h * Math.max(u - 1, 0);
    return (t - r) / u;
  }, v = (t) => {
    const r = M ? M(t) : t;
    return I(r, 0, o);
  }, [e, B] = N(() => v(p)), f = L(e), E = L(w), [T, W] = Q(() => ({
    x: 0,
    config: {
      tension: 280,
      friction: 30,
      mass: 1
    }
  })), i = (t) => {
    B(v(t));
  }, S = (t, r = !1) => {
    const a = k(), d = a + h;
    a <= 0 || W.start({
      x: -t * d,
      immediate: r
    });
  }, F = (t) => {
    W.start({
      x: t,
      immediate: !0
    });
  }, X = (t, r) => I(t + (r > 0 ? -1 : 1), 0, o), $ = K({
    onDrag: ({
      down: t,
      movement: [r],
      direction: [a],
      cancel: d,
      active: y,
      last: H
    }) => {
      const D = k(), z = D + h;
      if (D <= 0) return;
      const m = f.current, J = -m * z;
      if (t && Math.abs(r) > z / 2) {
        const C = X(m, a);
        d == null || d(), y && (C === m ? S(m) : i(C));
        return;
      }
      if (y && t) {
        F(J + r);
        return;
      }
      (H || !t) && S(m);
    }
  }, {
    drag: {
      delay: 200,
      filterTaps: !0
    }
  });
  g(() => {
    S(e), E.current(e);
  }, [e, u]), g(() => {
    let t;
    return n > 0 && c > 0 && (t = window.setInterval(() => {
      const r = f.current >= o ? 0 : f.current + 1;
      i(r);
    }, n)), () => {
      t && clearInterval(t);
    };
  }, [n, c, o]), g(() => {
    const t = c > 0 ? v(s % c) : 0;
    t !== f.current && i(t);
  }, [s, c, o]), g(() => {
    if (!x.current || typeof ResizeObserver > "u") return;
    const t = new ResizeObserver(() => {
      S(f.current, !0);
    });
    return t.observe(x.current), () => t.disconnect();
  }, [u]);
  const b = () => {
    i(0);
  }, R = () => {
    i(o);
  }, j = () => {
    const t = e >= o, r = e + u - 1 + l < c - 1;
    if (t) {
      b();
      return;
    }
    if (!r) {
      R();
      return;
    }
    i(e + l);
  }, q = () => {
    if (e === 0) {
      R();
      return;
    }
    if (e - l <= 0) {
      b();
      return;
    }
    i(e - l);
  }, G = O(() => ({
    transform: T.x.to((t) => `translate3d(${t}px, 0, 0)`),
    willChange: "transform",
    touchAction: "pan-y"
  }), [T.x]);
  return {
    gestureBinds: $,
    maxSlide: o,
    nextSlide: j,
    previousSlide: q,
    slide: e,
    trackStyle: G,
    updateSlide: i
  };
};
export {
  tt as useCarouselMovement
};
