import o from "react";
import L from "../../../classnames/index.mjs";
import B from "../../../rc-resize-observer/es/index.mjs";
import O from "../../../rc-util/es/omit.mjs";
import V from "../_util/throttleByAnimationFrame.mjs";
import "../config-provider/index.mjs";
import ne from "./style/index.mjs";
import { getTargetRect as g, getFixedTop as z, getFixedBottom as b } from "./utils.mjs";
import { ConfigContext as ie } from "../config-provider/context.mjs";
const D = ["resize", "scroll", "touchstart", "touchmove", "touchend", "pageshow", "load"];
function se() {
  return typeof window < "u" ? window : null;
}
var f;
(function(n) {
  n[n.None = 0] = "None", n[n.Prepare = 1] = "Prepare";
})(f || (f = {}));
const F = /* @__PURE__ */ o.forwardRef((n, I) => {
  var x;
  const {
    style: U,
    offsetTop: E,
    offsetBottom: m,
    prefixCls: G,
    className: j,
    rootClassName: H,
    children: M,
    target: l,
    onChange: R
  } = n, {
    getPrefixCls: $,
    getTargetContainer: k
  } = o.useContext(ie), T = $("affix", G), [q, J] = o.useState(!1), [s, K] = o.useState(), [Q, W] = o.useState(), y = o.useRef(f.None), N = o.useRef(null), a = o.useRef(), c = o.useRef(null), S = o.useRef(null), p = o.useRef(null), i = (x = l ?? k) !== null && x !== void 0 ? x : se, C = m === void 0 && E === void 0 ? 0 : E, X = () => {
    if (y.current !== f.Prepare || !S.current || !c.current || !i)
      return;
    const r = i();
    if (r) {
      const t = {
        status: f.None
      }, e = g(c.current);
      if (e.top === 0 && e.left === 0 && e.width === 0 && e.height === 0)
        return;
      const u = g(r), h = z(e, u, C), A = b(e, u, m);
      h !== void 0 ? (t.affixStyle = {
        position: "fixed",
        top: h,
        width: e.width,
        height: e.height
      }, t.placeholderStyle = {
        width: e.width,
        height: e.height
      }) : A !== void 0 && (t.affixStyle = {
        position: "fixed",
        bottom: A,
        width: e.width,
        height: e.height
      }, t.placeholderStyle = {
        width: e.width,
        height: e.height
      }), t.lastAffix = !!t.affixStyle, q !== t.lastAffix && (R == null || R(t.lastAffix)), y.current = t.status, K(t.affixStyle), W(t.placeholderStyle), J(t.lastAffix);
    }
  }, P = () => {
    var r;
    y.current = f.Prepare, X(), process.env.NODE_ENV === "test" && ((r = n == null ? void 0 : n.onTestUpdatePosition) === null || r === void 0 || r.call(n));
  }, d = V(() => {
    P();
  }), v = V(() => {
    if (i && s) {
      const r = i();
      if (r && c.current) {
        const t = g(r), e = g(c.current), u = z(e, t, C), h = b(e, t, m);
        if (u !== void 0 && s.top === u || h !== void 0 && s.bottom === h)
          return;
      }
    }
    P();
  }), _ = () => {
    const r = i == null ? void 0 : i();
    r && (D.forEach((t) => {
      var e;
      a.current && ((e = N.current) === null || e === void 0 || e.removeEventListener(t, a.current)), r == null || r.addEventListener(t, v);
    }), N.current = r, a.current = v);
  }, Y = () => {
    p.current && (clearTimeout(p.current), p.current = null);
    const r = i == null ? void 0 : i();
    D.forEach((t) => {
      var e;
      r == null || r.removeEventListener(t, v), a.current && ((e = N.current) === null || e === void 0 || e.removeEventListener(t, a.current));
    }), d.cancel(), v.cancel();
  };
  o.useImperativeHandle(I, () => ({
    updatePosition: d
  })), o.useEffect(() => (p.current = setTimeout(_), () => Y()), []), o.useEffect(() => {
    _();
  }, [l, s]), o.useEffect(() => {
    d();
  }, [l, E, m]);
  const [Z, ee, te] = ne(T), oe = L(H, ee, T, te), re = L({
    [oe]: s
  });
  let w = O(n, ["prefixCls", "offsetTop", "offsetBottom", "target", "onChange", "rootClassName"]);
  return process.env.NODE_ENV === "test" && (w = O(w, ["onTestUpdatePosition"])), Z(/* @__PURE__ */ o.createElement(B, {
    onResize: d
  }, /* @__PURE__ */ o.createElement("div", Object.assign({
    style: U,
    className: j,
    ref: c
  }, w), s && /* @__PURE__ */ o.createElement("div", {
    style: Q,
    "aria-hidden": "true"
  }), /* @__PURE__ */ o.createElement("div", {
    className: re,
    ref: S,
    style: s
  }, /* @__PURE__ */ o.createElement(B, {
    onResize: d
  }, M)))));
});
process.env.NODE_ENV !== "production" && (F.displayName = "Affix");
const ve = F;
export {
  ve as default
};
