import * as a from "react";
import { Timeout as o } from "./index-useTimeout.js";
let n = !0, s = !1;
const c = new o(), f = {
  text: !0,
  search: !0,
  url: !0,
  tel: !0,
  email: !0,
  password: !0,
  number: !0,
  date: !0,
  month: !0,
  week: !0,
  time: !0,
  datetime: !0,
  "datetime-local": !0
};
function d(e) {
  const {
    type: t,
    tagName: r
  } = e;
  return !!(r === "INPUT" && f[t] && !e.readOnly || r === "TEXTAREA" && !e.readOnly || e.isContentEditable);
}
function h(e) {
  e.metaKey || e.altKey || e.ctrlKey || (n = !0);
}
function i() {
  n = !1;
}
function y() {
  this.visibilityState === "hidden" && s && (n = !0);
}
function m(e) {
  e.addEventListener("keydown", h, !0), e.addEventListener("mousedown", i, !0), e.addEventListener("pointerdown", i, !0), e.addEventListener("touchstart", i, !0), e.addEventListener("visibilitychange", y, !0);
}
function b(e) {
  const {
    target: t
  } = e;
  try {
    return t.matches(":focus-visible");
  } catch {
  }
  return n || d(t);
}
function w() {
  const e = a.useCallback((u) => {
    u != null && m(u.ownerDocument);
  }, []), t = a.useRef(!1);
  function r() {
    return t.current ? (s = !0, c.start(100, () => {
      s = !1;
    }), t.current = !1, !0) : !1;
  }
  function l(u) {
    return b(u) ? (t.current = !0, !0) : !1;
  }
  return {
    isFocusVisibleRef: t,
    onFocus: l,
    onBlur: r,
    ref: e
  };
}
export {
  w as default
};
