var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = require("react");
var import__ = require(".");
let passiveEventSupported = false;
try {
  const opts = Object.defineProperty({}, "passive", {
    get() {
      passiveEventSupported = true;
    }
  });
  window.addEventListener("test", null, opts);
} catch (e) {
}
const passiveEvent = passiveEventSupported ? { capture: false, passive: true } : false;
const useEventVisible = (eventOptions) => {
  const {
    listenEvents = import__.DEFAULT_EVENTS,
    offset,
    scrollContainer,
    debounce,
    throttle
  } = eventOptions;
  const [visible, setVisible] = (0, import_react.useState)(false);
  const containerRef = (0, import_react.useRef)(null);
  (0, import_react.useEffect)(() => {
    let checkVisible = () => {
      var _a;
      if (!containerRef.current)
        return;
      const { top, left, width, height } = (_a = containerRef.current) == null ? void 0 : _a.getBoundingClientRect();
      const {
        top: parentTop,
        left: parentLeft,
        width: parentWidth,
        height: parentHeight
      } = scrollContainer.getBoundingClientRect();
      const windowInnerHeight = window.innerHeight || document.documentElement.clientHeight;
      const windowInnerWidth = window.innerWidth || document.documentElement.clientWidth;
      const intersectionTop = Math.min(parentTop, 0);
      const intersectionLeft = Math.min(parentLeft, 0);
      const intersectionHeight = Math.min(windowInnerHeight, parentTop + parentHeight) - intersectionTop;
      const intersectionWidth = Math.min(windowInnerWidth, parentLeft + parentWidth) - intersectionLeft;
      const offsetTop = top - intersectionTop;
      const offsetLeft = left - intersectionLeft;
      const offsets = Array.isArray(offset) ? offset : [offset, offset];
      const isVisible = offsetTop - offsets[0] <= intersectionHeight && offsetTop + height + offsets[0] >= 0 && offsetLeft - offsets[1] <= intersectionWidth && offsetLeft + width + offsets[1] >= 0;
      setVisible(isVisible);
      if (isVisible) {
        listenEvents.forEach((event) => {
          scrollContainer == null ? void 0 : scrollContainer.removeEventListener(event, checkVisible);
        });
      }
    };
    if (debounce) {
      checkVisible = (0, import__.debounce)(checkVisible, debounce);
    } else if (throttle) {
      checkVisible = (0, import__.throttle)(checkVisible, throttle);
    }
    listenEvents.forEach((event) => {
      scrollContainer == null ? void 0 : scrollContainer.addEventListener(event, checkVisible, passiveEvent);
    });
    checkVisible();
    return () => {
      listenEvents.forEach((event) => {
        scrollContainer == null ? void 0 : scrollContainer.removeEventListener(event, checkVisible, passiveEvent);
      });
    };
  }, [scrollContainer]);
  return [containerRef, visible];
};
var stdin_default = useEventVisible;
