import A from "../../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import E from "../../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import { isDOM as nv } from "../../../../rc-util/es/Dom/findDOMNode.mjs";
import tv from "../../../../rc-util/es/Dom/isVisible.mjs";
import ev from "../../../../rc-util/es/hooks/useEvent.mjs";
import Yr from "../../../../rc-util/es/hooks/useLayoutEffect.mjs";
import * as rr from "react";
import { collectScroller as pv, getWin as ra, getVisibleArea as gr, toNum as Ar } from "../util.mjs";
function Er(v) {
  var C = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0, M = "".concat(C), l = M.match(/^(.*)\%$/);
  return l ? v * (parseFloat(l[1]) / 100) : parseFloat(M);
}
function Rr(v, C) {
  var M = C || [], l = E(M, 2), D = l[0], J = l[1];
  return [Er(v.width, D), Er(v.height, J)];
}
function or() {
  var v = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : "";
  return [v[0], v[1]];
}
function S(v, C) {
  var M = C[0], l = C[1], D, J;
  return M === "t" ? J = v.y : M === "b" ? J = v.y + v.height : J = v.y + v.height / 2, l === "l" ? D = v.x : l === "r" ? D = v.x + v.width : D = v.x + v.width / 2, {
    x: D,
    y: J
  };
}
function Q(v, C) {
  var M = {
    t: "b",
    b: "t",
    l: "r",
    r: "l"
  };
  return v.map(function(l, D) {
    return D === C ? M[l] || "c" : l;
  }).join("");
}
function Ca(v, C, M, l, D, J, vr) {
  var Or = rr.useState({
    ready: !1,
    offsetX: 0,
    offsetY: 0,
    offsetR: 0,
    offsetB: 0,
    arrowX: 0,
    arrowY: 0,
    scaleX: 1,
    scaleY: 1,
    align: D[l] || {}
  }), Mr = E(Or, 2), q = Mr[0], dr = Mr[1], ar = rr.useRef(0), Cr = rr.useMemo(function() {
    return C ? pv(C) : [];
  }, [C]), F = rr.useRef({}), nr = function() {
    F.current = {};
  };
  v || nr();
  var tr = ev(function() {
    if (C && M && v) {
      let z = function(xr, g) {
        var p = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : Br, mr = d.x + xr, Xr = d.y + g, gv = mr + W, Av = Xr + T, Ev = Math.max(mr, p.left), Rv = Math.max(Xr, p.top), ov = Math.min(gv, p.right), Ov = Math.min(Av, p.bottom);
        return Math.max(0, (ov - Ev) * (Ov - Rv));
      }, Pr = function() {
        _ = d.y + w, t = _ + T, i = d.x + h, e = i + W;
      };
      var b, Z, r = C, Fr = r.ownerDocument, pr = ra(r), yr = pr.getComputedStyle(r), rv = yr.width, vv = yr.height, av = yr.position, yv = r.style.left, hv = r.style.top, wv = r.style.right, Mv = r.style.bottom, dv = r.style.overflow, f = A(A({}, D[l]), J), j = Fr.createElement("div");
      (b = r.parentElement) === null || b === void 0 || b.appendChild(j), j.style.left = "".concat(r.offsetLeft, "px"), j.style.top = "".concat(r.offsetTop, "px"), j.style.position = av, j.style.height = "".concat(r.offsetHeight, "px"), j.style.width = "".concat(r.offsetWidth, "px"), r.style.left = "0", r.style.top = "0", r.style.right = "auto", r.style.bottom = "auto", r.style.overflow = "hidden";
      var y;
      if (Array.isArray(M))
        y = {
          x: M[0],
          y: M[1],
          width: 0,
          height: 0
        };
      else {
        var R = M.getBoundingClientRect();
        y = {
          x: R.x,
          y: R.y,
          width: R.width,
          height: R.height
        };
      }
      var d = r.getBoundingClientRect(), L = Fr.documentElement, Cv = L.clientWidth, lv = L.clientHeight, Fv = L.scrollWidth, Dv = L.scrollHeight, Dr = L.scrollTop, Ur = L.scrollLeft, T = d.height, W = d.width, $ = y.height, N = y.width, Uv = {
        left: 0,
        top: 0,
        right: Cv,
        bottom: lv
      }, qv = {
        left: -Ur,
        top: -Dr,
        right: Fv - Ur,
        bottom: Dv - Dr
      }, s = f.htmlRegion, qr = "visible", zr = "visibleFirst";
      s !== "scroll" && s !== zr && (s = qr);
      var V = s === zr, zv = gr(qv, Cr), a = gr(Uv, Cr), Br = s === qr ? a : zv, o = V ? a : Br;
      r.style.left = "auto", r.style.top = "auto", r.style.right = "0", r.style.bottom = "0";
      var Gr = r.getBoundingClientRect();
      r.style.left = yv, r.style.top = hv, r.style.right = wv, r.style.bottom = Mv, r.style.overflow = dv, (Z = r.parentElement) === null || Z === void 0 || Z.removeChild(j);
      var c = Ar(Math.round(W / parseFloat(rv) * 1e3) / 1e3), I = Ar(Math.round(T / parseFloat(vv) * 1e3) / 1e3);
      if (c === 0 || I === 0 || nv(M) && !tv(M))
        return;
      var Bv = f.offset, Gv = f.targetOffset, Jv = Rr(d, Bv), Jr = E(Jv, 2), B = Jr[0], G = Jr[1], Kv = Rr(y, Gv), Kr = E(Kv, 2), Qv = Kr[0], Zv = Kr[1];
      y.x -= Qv, y.y -= Zv;
      var jv = f.points || [], Qr = E(jv, 2), kv = Qr[0], bv = Qr[1], k = or(bv), U = or(kv), Zr = S(y, k), jr = S(d, U), H = A({}, f), h = Zr.x - jr.x + B, w = Zr.y - jr.y + G, K = z(h, w), O = z(h, w, a), kr = S(y, ["t", "l"]), br = S(d, ["t", "l"]), fr = S(y, ["b", "r"]), Lr = S(d, ["b", "r"]), n = f.overflow || {}, fv = n.adjustX, Lv = n.adjustY, Tr = n.shiftX, Wr = n.shiftY, Hr = function(g) {
        return typeof g == "boolean" ? g : g >= 0;
      }, _, t, i, e;
      Pr();
      var Sr = Hr(Lv), $r = U[0] === k[0];
      if (Sr && U[0] === "t" && (t > o.bottom || F.current.bt)) {
        var u = w;
        $r ? u -= T - $ : u = kr.y - Lr.y - G;
        var Nr = z(h, u), Tv = z(h, u, a);
        // Of course use larger one
        Nr > K || Nr === K && (!V || // Choose recommend one
        Tv >= O) ? (F.current.bt = !0, w = u, G = -G, H.points = [Q(U, 0), Q(k, 0)]) : F.current.bt = !1;
      }
      if (Sr && U[0] === "b" && (_ < o.top || F.current.tb)) {
        var P = w;
        $r ? P += T - $ : P = fr.y - br.y - G;
        var sr = z(h, P), Wv = z(h, P, a);
        // Of course use larger one
        sr > K || sr === K && (!V || // Choose recommend one
        Wv >= O) ? (F.current.tb = !0, w = P, G = -G, H.points = [Q(U, 0), Q(k, 0)]) : F.current.tb = !1;
      }
      var Vr = Hr(fv), cr = U[1] === k[1];
      if (Vr && U[1] === "l" && (e > o.right || F.current.rl)) {
        var x = h;
        cr ? x -= W - N : x = kr.x - Lr.x - B;
        var Ir = z(x, w), Hv = z(x, w, a);
        // Of course use larger one
        Ir > K || Ir === K && (!V || // Choose recommend one
        Hv >= O) ? (F.current.rl = !0, h = x, B = -B, H.points = [Q(U, 1), Q(k, 1)]) : F.current.rl = !1;
      }
      if (Vr && U[1] === "r" && (i < o.left || F.current.lr)) {
        var m = h;
        cr ? m += W - N : m = fr.x - br.x - B;
        var _r = z(m, w), Sv = z(m, w, a);
        // Of course use larger one
        _r > K || _r === K && (!V || // Choose recommend one
        Sv >= O) ? (F.current.lr = !0, h = m, B = -B, H.points = [Q(U, 1), Q(k, 1)]) : F.current.lr = !1;
      }
      Pr();
      var X = Tr === !0 ? 0 : Tr;
      typeof X == "number" && (i < a.left && (h -= i - a.left - B, y.x + N < a.left + X && (h += y.x - a.left + N - X)), e > a.right && (h -= e - a.right - B, y.x > a.right - X && (h += y.x - a.right + X)));
      var Y = Wr === !0 ? 0 : Wr;
      typeof Y == "number" && (_ < a.top && (w -= _ - a.top - G, y.y + $ < a.top + Y && (w += y.y - a.top + $ - Y)), t > a.bottom && (w -= t - a.bottom - G, y.y > a.bottom - Y && (w += y.y - a.bottom + Y)));
      var hr = d.x + h, $v = hr + W, wr = d.y + w, Nv = wr + T, ir = y.x, sv = ir + N, ur = y.y, Vv = ur + $, cv = Math.max(hr, ir), Iv = Math.min($v, sv), _v = (cv + Iv) / 2, iv = _v - hr, uv = Math.max(wr, ur), Pv = Math.min(Nv, Vv), xv = (uv + Pv) / 2, mv = xv - wr;
      vr == null || vr(C, H);
      var Xv = Gr.right - d.x - (h + d.width), Yv = Gr.bottom - d.y - (w + d.height);
      dr({
        ready: !0,
        offsetX: h / c,
        offsetY: w / I,
        offsetR: Xv / c,
        offsetB: Yv / I,
        arrowX: iv / c,
        arrowY: mv / I,
        scaleX: c,
        scaleY: I,
        align: H
      });
    }
  }), er = function() {
    ar.current += 1;
    var Z = ar.current;
    Promise.resolve().then(function() {
      ar.current === Z && tr();
    });
  }, lr = function() {
    dr(function(Z) {
      return A(A({}, Z), {}, {
        ready: !1
      });
    });
  };
  return Yr(lr, [l]), Yr(function() {
    v || lr();
  }, [v]), [q.ready, q.offsetX, q.offsetY, q.offsetR, q.offsetB, q.arrowX, q.arrowY, q.scaleX, q.scaleY, q.align, er];
}
export {
  Ca as default
};
