import * as t from "react";
import l from "./index-_virtual16.js";
import { jsx as w } from "react/jsx-runtime";
import C from "./index-useForkRef.js";
import L from "./index-getReactElementRef.js";
import g from "./index-useEventCallback2.js";
import h from "./index-ownerDocument2.js";
import b from "./index-elementAcceptingRef.js";
import D from "./index-exactProp.js";
function T(c) {
  return c.substring(2).toLowerCase();
}
function O(c, s) {
  return s.documentElement.clientWidth < c.clientX || s.documentElement.clientHeight < c.clientY;
}
function v(c) {
  const {
    children: s,
    disableReactTree: y = !1,
    mouseEvent: i = "onClick",
    onClickAway: k,
    touchEvent: u = "onTouchEnd"
  } = c, a = t.useRef(!1), o = t.useRef(null), d = t.useRef(!1), m = t.useRef(!1);
  t.useEffect(() => (setTimeout(() => {
    d.current = !0;
  }, 0), () => {
    d.current = !1;
  }), []);
  const P = C(L(s), o), f = g((e) => {
    const n = m.current;
    m.current = !1;
    const r = h(o.current);
    if (!d.current || !o.current || "clientX" in e && O(e, r))
      return;
    if (a.current) {
      a.current = !1;
      return;
    }
    let E;
    e.composedPath ? E = e.composedPath().indexOf(o.current) > -1 : E = !r.documentElement.contains(
      // @ts-expect-error returns `false` as intended when not dispatched from a Node
      e.target
    ) || o.current.contains(
      // @ts-expect-error returns `false` as intended when not dispatched from a Node
      e.target
    ), !E && (y || !n) && k(e);
  }), R = (e) => (n) => {
    m.current = !0;
    const r = s.props[e];
    r && r(n);
  }, p = {
    ref: P
  };
  return u !== !1 && (p[u] = R(u)), t.useEffect(() => {
    if (u !== !1) {
      const e = T(u), n = h(o.current), r = () => {
        a.current = !0;
      };
      return n.addEventListener(e, f), n.addEventListener("touchmove", r), () => {
        n.removeEventListener(e, f), n.removeEventListener("touchmove", r);
      };
    }
  }, [f, u]), i !== !1 && (p[i] = R(i)), t.useEffect(() => {
    if (i !== !1) {
      const e = T(i), n = h(o.current);
      return n.addEventListener(e, f), () => {
        n.removeEventListener(e, f);
      };
    }
  }, [f, i]), /* @__PURE__ */ w(t.Fragment, {
    children: /* @__PURE__ */ t.cloneElement(s, p)
  });
}
process.env.NODE_ENV !== "production" && (v.propTypes = {
  // ┌────────────────────────────── Warning ──────────────────────────────┐
  // │ These PropTypes are generated from the TypeScript type definitions. │
  // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
  // └─────────────────────────────────────────────────────────────────────┘
  /**
   * The wrapped element.
   */
  children: b.isRequired,
  /**
   * If `true`, the React tree is ignored and only the DOM tree is considered.
   * This prop changes how portaled elements are handled.
   * @default false
   */
  disableReactTree: l.bool,
  /**
   * The mouse event to listen to. You can disable the listener by providing `false`.
   * @default 'onClick'
   */
  mouseEvent: l.oneOf(["onClick", "onMouseDown", "onMouseUp", "onPointerDown", "onPointerUp", !1]),
  /**
   * Callback fired when a "click away" event is detected.
   */
  onClickAway: l.func.isRequired,
  /**
   * The touch event to listen to. You can disable the listener by providing `false`.
   * @default 'onTouchEnd'
   */
  touchEvent: l.oneOf(["onTouchEnd", "onTouchStart", !1])
});
process.env.NODE_ENV !== "production" && (v.propTypes = D(v.propTypes));
export {
  v as ClickAwayListener
};
