import { isElement as U } from "../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { Timeout as Z } 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 { getTarget as $, contains as W } from "./utils/element.js";
import { getNodeChildren as y } from "./utils/nodes.js";
function G(u, t) {
  const [_, R] = u;
  let h = !1;
  const a = t.length;
  for (let d = 0, b = a - 1; d < a; b = d++) {
    const [O, E] = t[d] || [0, 0], [S, i] = t[b] || [0, 0];
    E >= R != i >= R && _ <= (S - O) * (R - E) / (i - E) + O && (h = !h);
  }
  return h;
}
function x(u, t) {
  return u[0] >= t.x && u[0] <= t.x + t.width && u[1] >= t.y && u[1] <= t.y + t.height;
}
function rt(u = {}) {
  const {
    buffer: t = 0.5,
    blockPointerEvents: _ = !1,
    requireIntent: R = !0
  } = u, h = new Z();
  let a = !1, d = null, b = null, O = typeof performance < "u" ? performance.now() : 0;
  function E(i, s) {
    const k = performance.now(), c = k - O;
    if (d === null || b === null || c === 0)
      return d = i, b = s, O = k, null;
    const X = i - d, Y = s - b, z = Math.sqrt(X * X + Y * Y) / c;
    return d = i, b = s, O = k, z;
  }
  const S = ({
    x: i,
    y: s,
    placement: k,
    elements: c,
    onClose: X,
    nodeId: Y,
    tree: p
  }) => function(P) {
    function I() {
      h.clear(), X();
    }
    if (h.clear(), !c.domReference || !c.floating || k == null || i == null || s == null)
      return;
    const {
      clientX: N,
      clientY: V
    } = P, H = [N, V], A = $(P), j = P.type === "mouseleave", J = W(c.floating, A), D = W(c.domReference, A), n = c.domReference.getBoundingClientRect(), o = c.floating.getBoundingClientRect(), C = k.split("-")[0], m = i > o.right - o.width / 2, g = s > o.bottom - o.height / 2, K = x(H, n), l = o.width > n.width, f = o.height > n.height, v = (l ? n : o).left, B = (l ? n : o).right, M = (f ? n : o).top, q = (f ? n : o).bottom;
    if (J && (a = !0, !j))
      return;
    if (D && (a = !1), D && !j) {
      a = !0;
      return;
    }
    if (j && U(P.relatedTarget) && W(c.floating, P.relatedTarget) || p && y(p.nodesRef.current, Y).some(({
      context: e
    }) => e == null ? void 0 : e.open))
      return;
    if (C === "top" && s >= n.bottom - 1 || C === "bottom" && s <= n.top + 1 || C === "left" && i >= n.right - 1 || C === "right" && i <= n.left + 1)
      return I();
    let L = [];
    switch (C) {
      case "top":
        L = [[v, n.top + 1], [v, o.bottom - 1], [B, o.bottom - 1], [B, n.top + 1]];
        break;
      case "bottom":
        L = [[v, o.top + 1], [v, n.bottom - 1], [B, n.bottom - 1], [B, o.top + 1]];
        break;
      case "left":
        L = [[o.right - 1, q], [o.right - 1, M], [n.left + 1, M], [n.left + 1, q]];
        break;
      case "right":
        L = [[n.right - 1, q], [n.right - 1, M], [o.left + 1, M], [o.left + 1, q]];
        break;
    }
    function Q([e, r]) {
      switch (C) {
        case "top": {
          const T = [l ? e + t / 2 : m ? e + t * 4 : e - t * 4, r + t + 1], w = [l ? e - t / 2 : m ? e + t * 4 : e - t * 4, r + t + 1], F = [[o.left, m || l ? o.bottom - t : o.top], [o.right, m ? l ? o.bottom - t : o.top : o.bottom - t]];
          return [T, w, ...F];
        }
        case "bottom": {
          const T = [l ? e + t / 2 : m ? e + t * 4 : e - t * 4, r - t], w = [l ? e - t / 2 : m ? e + t * 4 : e - t * 4, r - t], F = [[o.left, m || l ? o.top + t : o.bottom], [o.right, m ? l ? o.top + t : o.bottom : o.top + t]];
          return [T, w, ...F];
        }
        case "left": {
          const T = [e + t + 1, f ? r + t / 2 : g ? r + t * 4 : r - t * 4], w = [e + t + 1, f ? r - t / 2 : g ? r + t * 4 : r - t * 4];
          return [...[[g || f ? o.right - t : o.left, o.top], [g ? f ? o.right - t : o.left : o.right - t, o.bottom]], T, w];
        }
        case "right": {
          const T = [e - t, f ? r + t / 2 : g ? r + t * 4 : r - t * 4], w = [e - t, f ? r - t / 2 : g ? r + t * 4 : r - t * 4], F = [[g || f ? o.left + t : o.right, o.top], [g ? f ? o.left + t : o.right : o.left + t, o.bottom]];
          return [T, w, ...F];
        }
        default:
          return [];
      }
    }
    if (!G([N, V], L)) {
      if (a && !K)
        return I();
      if (!j && R) {
        const e = E(P.clientX, P.clientY);
        if (e !== null && e < 0.1)
          return I();
      }
      G([N, V], Q([i, s])) ? !a && R && h.start(40, I) : I();
    }
  };
  return S.__options = {
    blockPointerEvents: _
  }, S;
}
export {
  rt as safePolygon
};
