import z from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import k from "../../@babel/runtime/helpers/esm/extends.mjs";
import l from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import Ne from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import C from "../../classnames/index.mjs";
import H from "../../rc-motion/es/CSSMotion.mjs";
import "../../rc-motion/es/CSSMotionList.mjs";
import * as a from "react";
import R from "../../rc-util/es/KeyCode.mjs";
import be from "../../rc-util/es/pickAttrs.mjs";
import T from "./context.mjs";
import Se from "./DrawerPanel.mjs";
import { parseWidthHeight as U } from "./util.mjs";
var V = {
  width: 0,
  height: 0,
  overflow: "hidden",
  outline: "none",
  position: "absolute"
};
function Ee(e, W) {
  var w, N, b, y, s = e.prefixCls, c = e.open, f = e.placement, B = e.inline, S = e.push, F = e.forceRender, X = e.autoFocus, Y = e.keyboard, i = e.classNames, j = e.rootClassName, q = e.rootStyle, I = e.zIndex, G = e.className, J = e.id, Q = e.style, E = e.motion, Z = e.width, _ = e.height, $ = e.children, ee = e.contentWrapperStyle, te = e.mask, ae = e.maskClosable, ne = e.maskMotion, oe = e.maskClassName, le = e.maskStyle, x = e.afterOpenChange, g = e.onClose, ie = e.onMouseEnter, re = e.onMouseOver, se = e.onMouseLeave, ce = e.onClick, de = e.onKeyDown, ue = e.onKeyUp, r = e.styles, D = a.useRef(), M = a.useRef(), p = a.useRef();
  a.useImperativeHandle(W, function() {
    return D.current;
  });
  var me = function(o) {
    var m = o.keyCode, v = o.shiftKey;
    switch (m) {
      case R.TAB: {
        if (m === R.TAB) {
          if (!v && document.activeElement === p.current) {
            var K;
            (K = M.current) === null || K === void 0 || K.focus({
              preventScroll: !0
            });
          } else if (v && document.activeElement === M.current) {
            var P;
            (P = p.current) === null || P === void 0 || P.focus({
              preventScroll: !0
            });
          }
        }
        break;
      }
      case R.ESC: {
        g && Y && (o.stopPropagation(), g(o));
        break;
      }
    }
  };
  a.useEffect(function() {
    if (c && X) {
      var t;
      (t = D.current) === null || t === void 0 || t.focus({
        preventScroll: !0
      });
    }
  }, [c]);
  var ve = a.useState(!1), O = Ne(ve, 2), fe = O[0], A = O[1], n = a.useContext(T), h;
  S === !1 ? h = {
    distance: 0
  } : S === !0 ? h = {} : h = S || {};
  var d = (w = (N = (b = h) === null || b === void 0 ? void 0 : b.distance) !== null && N !== void 0 ? N : n == null ? void 0 : n.pushDistance) !== null && w !== void 0 ? w : 180, ye = a.useMemo(function() {
    return {
      pushDistance: d,
      push: function() {
        A(!0);
      },
      pull: function() {
        A(!1);
      }
    };
  }, [d]);
  a.useEffect(function() {
    if (c) {
      var t;
      n == null || (t = n.push) === null || t === void 0 || t.call(n);
    } else {
      var o;
      n == null || (o = n.pull) === null || o === void 0 || o.call(n);
    }
  }, [c]), a.useEffect(function() {
    return function() {
      var t;
      n == null || (t = n.pull) === null || t === void 0 || t.call(n);
    };
  }, []);
  var he = te && /* @__PURE__ */ a.createElement(H, k({
    key: "mask"
  }, ne, {
    visible: c
  }), function(t, o) {
    var m = t.className, v = t.style;
    return /* @__PURE__ */ a.createElement("div", {
      className: C("".concat(s, "-mask"), m, i == null ? void 0 : i.mask, oe),
      style: l(l(l({}, v), le), r == null ? void 0 : r.mask),
      onClick: ae && c ? g : void 0,
      ref: o
    });
  }), ke = typeof E == "function" ? E(f) : E, u = {};
  if (fe && d)
    switch (f) {
      case "top":
        u.transform = "translateY(".concat(d, "px)");
        break;
      case "bottom":
        u.transform = "translateY(".concat(-d, "px)");
        break;
      case "left":
        u.transform = "translateX(".concat(d, "px)");
        break;
      default:
        u.transform = "translateX(".concat(-d, "px)");
        break;
    }
  f === "left" || f === "right" ? u.width = U(Z) : u.height = U(_);
  var Ce = {
    onMouseEnter: ie,
    onMouseOver: re,
    onMouseLeave: se,
    onClick: ce,
    onKeyDown: de,
    onKeyUp: ue
  }, we = /* @__PURE__ */ a.createElement(H, k({
    key: "panel"
  }, ke, {
    visible: c,
    forceRender: F,
    onVisibleChanged: function(o) {
      x == null || x(o);
    },
    removeOnLeave: !1,
    leavedClassName: "".concat(s, "-content-wrapper-hidden")
  }), function(t, o) {
    var m = t.className, v = t.style;
    return /* @__PURE__ */ a.createElement("div", k({
      className: C("".concat(s, "-content-wrapper"), i == null ? void 0 : i.wrapper, m),
      style: l(l(l(l({}, u), v), ee), r == null ? void 0 : r.wrapper)
    }, be(e, {
      data: !0
    })), /* @__PURE__ */ a.createElement(Se, k({
      id: J,
      containerRef: o,
      prefixCls: s,
      className: C(G, i == null ? void 0 : i.content),
      style: l(l({}, Q), r == null ? void 0 : r.content)
    }, Ce), $));
  }), L = l({}, q);
  return I && (L.zIndex = I), /* @__PURE__ */ a.createElement(T.Provider, {
    value: ye
  }, /* @__PURE__ */ a.createElement("div", {
    className: C(s, "".concat(s, "-").concat(f), j, (y = {}, z(y, "".concat(s, "-open"), c), z(y, "".concat(s, "-inline"), B), y)),
    style: L,
    tabIndex: -1,
    ref: D,
    onKeyDown: me
  }, he, /* @__PURE__ */ a.createElement("div", {
    tabIndex: 0,
    ref: M,
    style: V,
    "aria-hidden": "true",
    "data-sentinel": "start"
  }), we, /* @__PURE__ */ a.createElement("div", {
    tabIndex: 0,
    ref: p,
    style: V,
    "aria-hidden": "true",
    "data-sentinel": "end"
  })));
}
var xe = /* @__PURE__ */ a.forwardRef(Ee);
process.env.NODE_ENV !== "production" && (xe.displayName = "DrawerPopup");
export {
  xe as default
};
