import w from "./index-extends.js";
import * as i from "react";
import M from "./index-useTimeout.js";
import E from "./index-useEventCallback2.js";
import k from "./index-extractEventHandlers.js";
function R(f = {}) {
  const {
    autoHideDuration: r = null,
    disableWindowBlurListener: m = !1,
    onClose: t,
    open: l,
    resumeHideDuration: d
  } = f, c = M();
  i.useEffect(() => {
    if (!l)
      return;
    function e(n) {
      n.defaultPrevented || (n.key === "Escape" || n.key === "Esc") && (t == null || t(n, "escapeKeyDown"));
    }
    return document.addEventListener("keydown", e), () => {
      document.removeEventListener("keydown", e);
    };
  }, [l, t]);
  const v = E((e, n) => {
    t == null || t(e, n);
  }), a = E((e) => {
    !t || e == null || c.start(e, () => {
      v(null, "timeout");
    });
  });
  i.useEffect(() => (l && a(r), c.clear), [l, r, a, c]);
  const L = (e) => {
    t == null || t(e, "clickaway");
  }, u = c.clear, s = i.useCallback(() => {
    r != null && a(d ?? r * 0.5);
  }, [r, d, a]), b = (e) => (n) => {
    const o = e.onBlur;
    o == null || o(n), s();
  }, p = (e) => (n) => {
    const o = e.onFocus;
    o == null || o(n), u();
  }, C = (e) => (n) => {
    const o = e.onMouseEnter;
    o == null || o(n), u();
  }, y = (e) => (n) => {
    const o = e.onMouseLeave;
    o == null || o(n), s();
  };
  return i.useEffect(() => {
    if (!m && l)
      return window.addEventListener("focus", s), window.addEventListener("blur", u), () => {
        window.removeEventListener("focus", s), window.removeEventListener("blur", u);
      };
  }, [m, l, s, u]), {
    getRootProps: (e = {}) => {
      const n = w({}, k(f), k(e));
      return w({
        // ClickAwayListener adds an `onClick` prop which results in the alert not being announced.
        // See https://github.com/mui/material-ui/issues/29080
        role: "presentation"
      }, e, n, {
        onBlur: b(n),
        onFocus: p(n),
        onMouseEnter: C(n),
        onMouseLeave: y(n)
      });
    },
    onClickAway: L
  };
}
export {
  R as default
};
