import { useEffect } from "react";
import { inBrowser } from "../utils";
let supportsPassive = false;
if (inBrowser) {
  try {
    const opts = {};
    Object.defineProperty(opts, "passive", {
      get() {
        supportsPassive = true;
      }
    });
    window.addEventListener("test-passive", null, opts);
  } catch (e) {
  }
}
function useEventListener(type, listener, options = {}) {
  if (!inBrowser) {
    return;
  }
  const { target = window, passive = false, capture = false, depends = [] } = options;
  let attached;
  const add = () => {
    const element = target;
    if (element && !attached) {
      element.addEventListener(type, listener, supportsPassive ? { capture, passive } : capture);
      attached = true;
    }
  };
  const remove = () => {
    const element = target;
    if (element && attached) {
      element.removeEventListener(type, listener, capture);
      attached = false;
    }
  };
  useEffect(() => {
    add();
    return () => remove();
  }, [target, ...depends]);
}
var stdin_default = useEventListener;
export {
  stdin_default as default
};
