import { jsx as W } from "react/jsx-runtime";
import { useState as S, useRef as J, Children as U, useCallback as l, cloneElement as Q, useEffect as p } from "react";
import A from "lodash/isEqual";
import r from "lodash/isNil";
import Z from "lodash/isNull";
import g from "./usePrevValue.js";
import { withDeprecated as P } from "../Util/deprecate.js";
const I = {
  TOUCHMOVE: "touchmove",
  KEYDOWN: "keydown"
}, b = 500, ee = "ease-in-out", te = "100vh", ne = "100vw", K = 0, k = 0, D = 100, se = 38, oe = 40, y = "rps-scroll--disabled";
let M = null, d = !1, a = !1, w = !0, Y = !1;
const T = [];
function re({
  animationTimer: f = b,
  blockScrollDown: F = !1,
  blockScrollUp: H = !1,
  children: B = void 0,
  height: V = te,
  width: G = ne,
  customPageNumber: s = void 0,
  onScrollUnavailable: E = void 0,
  onChange: R = void 0,
  renderAllPagesOnFirstRender: $ = !1,
  transitionTimingFunction: j = ee
}) {
  const [n, v] = S(K), [c, C] = S(k), m = g(n), i = J(null), o = U.toArray(B), L = l(
    (e) => {
      let t = 0;
      r(e) || (t = e), t = Math.max(t, c), t <= n + 1 && (r(o[n + 1]) || t++), C(t);
    },
    [o, n, c]
  ), q = l(() => {
    $ ? C(U.count(o)) : r(o[K + 1]) || L(k + 1);
  }, [L, o, $]), O = l(() => {
    w && (w = !1, window.scrollTo({
      left: 0,
      top: 0,
      behavior: "smooth"
    }), document.body.classList.add(y), document.documentElement.classList.add(y));
  }, []), _ = l(() => {
    w || (w = !0, document.body.classList.remove(y), document.documentElement.classList.remove(y));
  }, []), X = l(() => {
    const e = [];
    let t = 0;
    for (; t < c && !r(o[t]); )
      T[t] = !0, e.push(
        /* @__PURE__ */ W("div", { style: { height: "100%", width: "100%" }, children: Q(o[t], { ...o[t].props }) }, t)
      ), t++;
    return e;
  }, [o, c]), h = l(() => {
    !d && !F && (r(T[n + 1]) ? (_(), E && E()) : (O(), d = !0, i.current.style.transform = `translate3d(0, ${(n + 1) * -100}%, 0)`, setTimeout(() => {
      a && v((e) => e + 1);
    }, f + D)));
  }, [f, F, n, O, _, E]), u = l(() => {
    !d && !H && (r(T[n - 1]) ? (_(), E && E()) : (O(), d = !0, i.current.style.transform = `translate3d(0, ${(n - 1) * -100}%, 0)`, setTimeout(() => {
      a && v((e) => e - 1);
    }, f + D)));
  }, [f, H, n, O, _, E]), N = l(
    (e) => {
      Z(M) ? M = e.touches[0].clientY : e.touches[0].clientY > M ? u() : h();
    },
    [h, u]
  ), z = l(
    (e) => {
      e.deltaY < 0 ? u() : h();
    },
    [h, u]
  ), x = l(
    (e) => {
      A(e.keyCode, se) && u(), A(e.keyCode, oe) && h();
    },
    [h, u]
  );
  return p(() => (i.current?.addEventListener(I.TOUCHMOVE, N), i.current?.addEventListener(I.KEYDOWN, x), () => {
    i.current?.removeEventListener && (i.current.removeEventListener(I.TOUCHMOVE, N), i.current.removeEventListener(I.KEYDOWN, x));
  }), [N, x]), p(() => (a = !0, q(), () => {
    a = !1;
  }), []), p(() => {
    d = !1, M = null, m !== void 0 && n > m && L();
  }, [L, n, m]), p(() => {
    R && R(n);
  }, [R, n]), p(() => {
    if (!r(s) && !A(s, n)) {
      let e = c;
      if (!A(n, s)) {
        if (!r(T[s]) && !d)
          d = !0, i.current.style.transform = `translate3d(0, ${s * -100}%, 0)`, r(T[s]) && !r(o[s]) && e++, setTimeout(() => {
            v(s), C(e);
          }, f + D);
        else if (!d && !r(o[s])) {
          for (let t = c; t <= s; t++)
            e++;
          r(o[s]) || e++, d = !0, Y = !0, C(e);
        }
      }
    }
  }, [s]), p(() => {
    Y && (Y = !1, i.current.style.transform = `translate3d(0, ${s * -100}%, 0)`, setTimeout(() => {
      v(s);
    }, f + D));
  }, [f, c, s]), /* @__PURE__ */ W("div", { style: { height: V, width: G, overflow: "hidden" }, children: /* @__PURE__ */ W(
    "div",
    {
      ref: i,
      onWheel: z,
      style: {
        height: "100%",
        width: "100%",
        transition: `transform ${f}ms ${j}`,
        outline: "none"
      },
      tabIndex: 0,
      children: X()
    }
  ) });
}
const ue = P(re, { name: "PageScroller" });
export {
  ue as default
};
