"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Parallax_exports = {};
__export(Parallax_exports, {
  default: () => Parallax
});
module.exports = __toCommonJS(Parallax_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_clamp = __toESM(require("../../../../../utils/clamp"));
var import_useIsomorphicLayoutEffect = require("../../../../hooks/useIsomorphicLayoutEffect");
function isScrollable(element) {
  const { overflow, overflowY, overflowX } = element.ownerDocument.defaultView.getComputedStyle(element);
  return /(auto|scroll)/.test(overflow + overflowX + overflowY);
}
function getScrollParent(element) {
  const { parentElement } = element;
  if (!element || !parentElement)
    return element;
  if (isScrollable(element))
    return element;
  return getScrollParent(parentElement);
}
function Parallax({ strength, children, ...rest }) {
  const container = (0, import_react.useRef)(null);
  const [containerScrollTop, setContainerScrollTop] = (0, import_react.useState)(strength == null ? 0 : strength);
  const lastScrollParentScrollTop = (0, import_react.useRef)(0);
  (0, import_useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => {
    if (!container.current || strength == null || strength === 0)
      return void 0;
    const containerDocument = container.current.ownerDocument;
    const scrollParent = getScrollParent(container.current);
    const eventTarget = containerDocument.documentElement === scrollParent ? containerDocument.defaultView : scrollParent;
    lastScrollParentScrollTop.current = scrollParent.scrollTop;
    setContainerScrollTop(strength);
    function handleScroll() {
      containerDocument.defaultView.requestAnimationFrame(() => {
        if (!container.current)
          return;
        const { top: containerTop, bottom: containerBottom } = container.current.getBoundingClientRect();
        const { top: scrollParentTop, bottom: scrollParentBottom } = scrollParent === containerDocument.documentElement ? { top: 0, bottom: containerDocument.defaultView.innerHeight } : scrollParent.getBoundingClientRect();
        const { scrollTop: scrollParentScrollTop } = scrollParent === containerDocument.documentElement ? { scrollTop: containerDocument.defaultView.pageYOffset } : scrollParent;
        const scrollParentHeight = scrollParentBottom - scrollParentTop;
        const scrollParentScrollDelta = scrollParentScrollTop - lastScrollParentScrollTop.current;
        lastScrollParentScrollTop.current = scrollParentScrollTop;
        setContainerScrollTop((scrollTop) => {
          const isContainerVisible = containerTop < scrollParentTop + scrollParentHeight && containerBottom > scrollParentTop;
          const containerScrollRemaining = scrollParentScrollDelta > 0 ? 2 * strength - scrollTop : scrollTop;
          const scrollParentScrollRemaining = scrollParentScrollDelta > 0 ? scrollParentTop + containerBottom : scrollParentHeight - containerTop;
          const parallaxRatio = containerScrollRemaining / scrollParentScrollRemaining;
          const containerScrollDelta = isContainerVisible ? parallaxRatio * scrollParentScrollDelta : 0;
          return (0, import_clamp.default)(0, scrollTop + containerScrollDelta, strength * 2);
        });
      });
    }
    eventTarget.addEventListener("scroll", handleScroll);
    return () => eventTarget.removeEventListener("scroll", handleScroll);
  }, [strength]);
  const getProps = (0, import_react.useCallback)(
    ({ style, ...restOfChildrenProps }) => ({
      ...restOfChildrenProps,
      style: {
        ...style,
        position: "absolute",
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
        ...strength == null || strength === 0 ? {} : {
          top: -strength,
          bottom: -strength,
          transform: `translate3d(0, ${containerScrollTop - strength}px, 0)`
        }
      }
    }),
    [strength, containerScrollTop]
  );
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    "div",
    {
      ...rest,
      ref: container,
      style: { position: "absolute", top: 0, left: 0, right: 0, bottom: 0 },
      children: children(getProps)
    }
  );
}
//# sourceMappingURL=index.js.map