import * as k from "react";
import { useAnimationFrame as P } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useAnimationFrame.js";
import { useTimeout as b } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useTimeout.js";
import { createChangeEventDetails as v } from "../../utils/createBaseUIEventDetails.js";
import { isMouseLikePointerType as w, isClickLikeEvent as I } from "../utils/event.js";
import { isTypeableElement as x } from "../utils/element.js";
import { EMPTY_OBJECT as L } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/empty.js";
import { triggerPress as y } from "../../utils/reason-parts.js";
function Y(d, M = {}) {
  const e = "rootStore" in d ? d.rootStore : d, T = e.context.dataRef, {
    enabled: h = !0,
    event: a = "click",
    toggle: g = !0,
    ignoreMouse: O = !1,
    stickIfOpen: E = !0,
    touchOpenDelay: n = 0
  } = M, c = k.useRef(void 0), C = P(), m = b(), R = k.useMemo(() => ({
    onPointerDown(t) {
      c.current = t.pointerType;
    },
    onMouseDown(t) {
      const r = c.current, o = t.nativeEvent, i = e.select("open");
      if (t.button !== 0 || a === "click" || w(r, !0) && O)
        return;
      const s = T.current.openEvent, u = s == null ? void 0 : s.type, f = e.select("domReferenceElement") !== t.currentTarget, l = i && f || !(i && g && (!(s && E) || u === "click" || u === "mousedown"));
      if (x(o.target)) {
        const p = v(y, o, o.target);
        l && r === "touch" && n > 0 ? m.start(n, () => {
          e.setOpen(!0, p);
        }) : e.setOpen(l, p);
        return;
      }
      const D = t.currentTarget;
      C.request(() => {
        const p = v(y, o, D);
        l && r === "touch" && n > 0 ? m.start(n, () => {
          e.setOpen(!0, p);
        }) : e.setOpen(l, p);
      });
    },
    onClick(t) {
      if (a === "mousedown-only")
        return;
      const r = c.current;
      if (a === "mousedown" && r) {
        c.current = void 0;
        return;
      }
      if (w(r, !0) && O)
        return;
      const o = e.select("open"), i = T.current.openEvent, s = e.select("domReferenceElement") !== t.currentTarget, u = o && s || !(o && g && (!(i && E) || I(i))), f = v(y, t.nativeEvent, t.currentTarget);
      u && r === "touch" && n > 0 ? m.start(n, () => {
        e.setOpen(!0, f);
      }) : e.setOpen(u, f);
    },
    onKeyDown() {
      c.current = void 0;
    }
  }), [T, a, O, e, E, g, C, m, n]);
  return k.useMemo(() => h ? {
    reference: R
  } : L, [h, R]);
}
export {
  Y as useClick
};
