import bt from "../../../@babel/runtime/helpers/esm/extends.mjs";
import M from "../../../@babel/runtime/helpers/esm/defineProperty.mjs";
import Ve from "../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import F from "../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import m from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import ie from "../../../classnames/index.mjs";
import re from "../../../rc-resize-observer/es/index.mjs";
import St from "../../../rc-util/es/hooks/useEvent.mjs";
import { useComposeRef as pt } from "../../../rc-util/es/ref.mjs";
import * as n from "react";
import { useRef as S, useState as A, useEffect as I } from "react";
import yt from "../hooks/useIndicator.mjs";
import xt from "../hooks/useOffsets.mjs";
import Be from "../hooks/useSyncState.mjs";
import zt from "../hooks/useTouchMove.mjs";
import Rt, { useUpdateState as Ct } from "../hooks/useUpdate.mjs";
import Tt from "../hooks/useVisibleRange.mjs";
import _t from "../TabContext.mjs";
import { stringify as He, genDataNodeKey as Et } from "../util.mjs";
import Lt from "./AddButton.mjs";
import Oe from "./ExtraContent.mjs";
import kt from "./OperationNode.mjs";
import Nt from "./TabNode.mjs";
var wt = function(o, r) {
  var p = o.offsetWidth, C = o.offsetHeight, T = o.offsetTop, L = o.offsetLeft, c = o.getBoundingClientRect(), f = c.width, _ = c.height, V = c.x, g = c.y;
  return Math.abs(f - p) < 1 ? [f, _, V - r.x, g - r.y] : [p, C, L, T];
}, E = function(o) {
  var r = o.current || {}, p = r.offsetWidth, C = p === void 0 ? 0 : p, T = r.offsetHeight, L = T === void 0 ? 0 : T;
  if (o.current) {
    var c = o.current.getBoundingClientRect(), f = c.width, _ = c.height;
    if (Math.abs(f - C) < 1)
      return [f, _];
  }
  return [C, L];
}, G = function(o, r) {
  return o[r ? 0 : 1];
}, ea = /* @__PURE__ */ n.forwardRef(function(a, o) {
  var r, p = a.className, C = a.style, T = a.id, L = a.animated, c = a.activeKey, f = a.rtl, _ = a.extra, V = a.editable, g = a.locale, B = a.tabPosition, ne = a.tabBarGutter, We = a.children, Ue = a.onTabClick, H = a.onTabScroll, De = a.indicatorSize, oe = n.useContext(_t), u = oe.prefixCls, y = oe.tabs, fe = S(null), le = S(null), se = S(null), k = S(null), O = S(null), ce = S(null), ue = S(null), l = B === "top" || B === "bottom", Ke = Be(0, function(t, e) {
    l && H && H({
      direction: t > e ? "left" : "right"
    });
  }), de = m(Ke, 2), d = de[0], q = de[1], Pe = Be(0, function(t, e) {
    !l && H && H({
      direction: t > e ? "top" : "bottom"
    });
  }), ve = m(Pe, 2), x = ve[0], J = ve[1], $e = A([0, 0]), me = m($e, 2), Fe = me[0], Ie = me[1], Ge = A([0, 0]), ge = m(Ge, 2), he = ge[0], qe = ge[1], Je = A([0, 0]), be = m(Je, 2), Qe = be[0], Xe = be[1], Ye = A([0, 0]), Se = m(Ye, 2), Ze = Se[0], je = Se[1], et = Ct(/* @__PURE__ */ new Map()), pe = m(et, 2), tt = pe[0], at = pe[1], W = xt(y, tt, he[0]), Q = G(Fe, l), N = G(he, l), X = G(Qe, l), ye = G(Ze, l), xe = Q < N + X, v = xe ? Q - ye : Q - X, it = "".concat(u, "-nav-operations-hidden"), h = 0, z = 0;
  l && f ? (h = 0, z = Math.max(0, N - v)) : (h = Math.min(0, v - N), z = 0);
  function Y(t) {
    return t < h ? h : t > z ? z : t;
  }
  var Z = S(null), rt = A(), ze = m(rt, 2), U = ze[0], Re = ze[1];
  function j() {
    Re(Date.now());
  }
  function ee() {
    Z.current && clearTimeout(Z.current);
  }
  zt(k, function(t, e) {
    function i(s, R) {
      s(function(b) {
        var P = Y(b + R);
        return P;
      });
    }
    return xe ? (l ? i(q, t) : i(J, e), ee(), j(), !0) : !1;
  }), I(function() {
    return ee(), U && (Z.current = setTimeout(function() {
      Re(0);
    }, 100)), ee;
  }, [U]);
  var nt = Tt(
    W,
    // Container
    v,
    // Transform
    l ? d : x,
    // Tabs
    N,
    // Add
    X,
    // Operation
    ye,
    F(F({}, a), {}, {
      tabs: y
    })
  ), Ce = m(nt, 2), ot = Ce[0], ft = Ce[1], Te = St(function() {
    var t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : c, e = W.get(t) || {
      width: 0,
      height: 0,
      left: 0,
      right: 0,
      top: 0
    };
    if (l) {
      var i = d;
      f ? e.right < d ? i = e.right : e.right + e.width > d + v && (i = e.right + e.width - v) : e.left < -d ? i = -e.left : e.left + e.width > -d + v && (i = -(e.left + e.width - v)), J(0), q(Y(i));
    } else {
      var s = x;
      e.top < -x ? s = -e.top : e.top + e.height > -x + v && (s = -(e.top + e.height - v)), q(0), J(Y(s));
    }
  }), D = {};
  B === "top" || B === "bottom" ? D[f ? "marginRight" : "marginLeft"] = ne : D.marginTop = ne;
  var _e = y.map(function(t, e) {
    var i = t.key;
    return /* @__PURE__ */ n.createElement(Nt, {
      id: T,
      prefixCls: u,
      key: i,
      tab: t,
      style: e === 0 ? void 0 : D,
      closable: t.closable,
      editable: V,
      active: i === c,
      renderWrapper: We,
      removeAriaLabel: g == null ? void 0 : g.removeAriaLabel,
      onClick: function(R) {
        Ue(i, R);
      },
      onFocus: function() {
        Te(i), j(), k.current && (f || (k.current.scrollLeft = 0), k.current.scrollTop = 0);
      }
    });
  }), Ee = function() {
    return at(function() {
      var e, i = /* @__PURE__ */ new Map(), s = (e = O.current) === null || e === void 0 ? void 0 : e.getBoundingClientRect();
      return y.forEach(function(R) {
        var b, P = R.key, Ae = (b = O.current) === null || b === void 0 ? void 0 : b.querySelector('[data-node-key="'.concat(Et(P), '"]'));
        if (Ae) {
          var dt = wt(Ae, s), $ = m(dt, 4), vt = $[0], mt = $[1], gt = $[2], ht = $[3];
          i.set(P, {
            width: vt,
            height: mt,
            left: gt,
            top: ht
          });
        }
      }), i;
    });
  };
  I(function() {
    Ee();
  }, [y.map(function(t) {
    return t.key;
  }).join("_")]);
  var K = Rt(function() {
    var t = E(fe), e = E(le), i = E(se);
    Ie([t[0] - e[0] - i[0], t[1] - e[1] - i[1]]);
    var s = E(ue);
    Xe(s);
    var R = E(ce);
    je(R);
    var b = E(O);
    qe([b[0] - s[0], b[1] - s[1]]), Ee();
  }), lt = y.slice(0, ot), st = y.slice(ft + 1), Le = [].concat(Ve(lt), Ve(st)), ke = W.get(c), ct = yt({
    activeTabOffset: ke,
    horizontal: l,
    rtl: f,
    indicatorSize: De
  }), ut = ct.style;
  I(function() {
    Te();
  }, [c, h, z, He(ke), He(W), l]), I(function() {
    K();
  }, [f]);
  var Ne = !!Le.length, w = "".concat(u, "-nav-wrap"), te, ae, we, Me;
  return l ? f ? (ae = d > 0, te = d !== z) : (te = d < 0, ae = d !== h) : (we = x < 0, Me = x !== h), /* @__PURE__ */ n.createElement(re, {
    onResize: K
  }, /* @__PURE__ */ n.createElement("div", {
    ref: pt(o, fe),
    role: "tablist",
    className: ie("".concat(u, "-nav"), p),
    style: C,
    onKeyDown: function() {
      j();
    }
  }, /* @__PURE__ */ n.createElement(Oe, {
    ref: le,
    position: "left",
    extra: _,
    prefixCls: u
  }), /* @__PURE__ */ n.createElement(re, {
    onResize: K
  }, /* @__PURE__ */ n.createElement("div", {
    className: ie(w, (r = {}, M(r, "".concat(w, "-ping-left"), te), M(r, "".concat(w, "-ping-right"), ae), M(r, "".concat(w, "-ping-top"), we), M(r, "".concat(w, "-ping-bottom"), Me), r)),
    ref: k
  }, /* @__PURE__ */ n.createElement(re, {
    onResize: K
  }, /* @__PURE__ */ n.createElement("div", {
    ref: O,
    className: "".concat(u, "-nav-list"),
    style: {
      transform: "translate(".concat(d, "px, ").concat(x, "px)"),
      transition: U ? "none" : void 0
    }
  }, _e, /* @__PURE__ */ n.createElement(Lt, {
    ref: ue,
    prefixCls: u,
    locale: g,
    editable: V,
    style: F(F({}, _e.length === 0 ? void 0 : D), {}, {
      visibility: Ne ? "hidden" : null
    })
  }), /* @__PURE__ */ n.createElement("div", {
    className: ie("".concat(u, "-ink-bar"), M({}, "".concat(u, "-ink-bar-animated"), L.inkBar)),
    style: ut
  }))))), /* @__PURE__ */ n.createElement(kt, bt({}, a, {
    removeAriaLabel: g == null ? void 0 : g.removeAriaLabel,
    ref: ce,
    prefixCls: u,
    tabs: Le,
    className: !Ne && it,
    tabMoving: !!U
  })), /* @__PURE__ */ n.createElement(Oe, {
    ref: se,
    position: "right",
    extra: _,
    prefixCls: u
  })));
});
export {
  ea as default
};
