import { jsxs as c, Fragment as P, jsx as s } from "react/jsx-runtime";
import { useState as i, useRef as V, useEffect as b } from "react";
import { motion as We } from "motion/react";
import { noop as p } from "es-toolkit/function";
import { toNumber as j } from "es-toolkit/compat";
import m from "../../utils/classNames.js";
import Oe from "./SidebarFooter.js";
import ge from "./SidebarBackdrop.js";
import ve from "./SidebarFullscreenToggle.js";
import ze from "./SidebarCloseButton.js";
import E from "../resizer/Resizer.js";
import He from "../../hooks/useWindowResize.js";
import Be from "../../hooks/useEsc.js";
import ke from "../../utils/getWidthInBoundaries.js";
import xe from "../smoothScrollbars/SmoothScrollbars.js";
const Ue = 350, Ae = 100, Ye = 800, X = 0, Ge = 400, Pe = 800, Ve = 0.14, je = 500, C = {
  LEFT: "left",
  RIGHT: "right"
}, D = {
  MODE_FLY: "fly",
  MODE_FLUID: "fluid"
}, Xe = {
  sidebarEnter: ({ fromRight: n }) => ({
    x: n ? "100%" : "-100%",
    opacity: 0
  }),
  sidebarVisible: ({ fromRight: n, closed: r }) => ({
    x: r ? n ? "100%" : "-100%" : 0,
    opacity: r ? 0 : 1,
    display: r ? "none" : "block"
  }),
  sidebarExit: ({ fromRight: n }) => ({
    x: n ? "100%" : "-100%",
    opacity: 0
  })
}, t = (n) => {
  const {
    fly: r = !1,
    closed: o = !1,
    title: F = "",
    footer: Z,
    resizable: q = !1,
    position: R,
    width: l = Ue,
    minWidth: K = Ae,
    maxWidth: M = Ye,
    hasBackdrop: J = !1,
    hasSmoothScrollbar: Q = !1,
    makeBackdropVisible: $ = !1,
    enableFullscreenToggle: T = !1,
    fullscreenToggleTooltip: ee,
    openInFullscreen: se = !1,
    showHeaderBorder: te = !1,
    disableClose: _ = !1,
    bodyRef: w,
    headerButtons: y,
    switchModeBreakpoint: W = X,
    onClose: oe = p,
    onResizeEnd: ie = p,
    onFullScreenChange: O = p,
    onBackdropClick: ne = p,
    disableEsc: re = !1,
    titleClassName: ae = "",
    bodyClassName: le = "",
    headerClassName: de = "",
    footerClassName: ce = "",
    backdropClassName: me = "",
    className: fe = "",
    children: g,
    ...ue
  } = n, [he, v] = i(j(l)), [S, z] = i(r ? t.MODE_FLY : t.MODE_FLUID), [a, H] = i(se), [be, B] = i(!1), [I, pe] = i(!1), [N, Ee] = i(R === C.RIGHT), f = V(null), k = V(null);
  b(() => {
    f.current && S === t.MODE_FLY && f.current.parentNode?.className.includes("right") && Ee(!0);
  }, [f.current, S]);
  const [x, Te] = i(o);
  x !== o && Te(o);
  const [Se, Ie] = i(l);
  Se !== l && (console.log("update internal width"), v(j(l)), Ie(l)), He(() => u(), je), b(() => u(), []), b(() => {
    o || setTimeout(() => u());
  }, [o]), b(() => {
    const e = document.querySelector(".module-content");
    e && (k.current = e);
  }, []), Be(() => {
    !re && T && a && (H(!1), O(!1));
  });
  const u = () => {
    const e = k.current?.clientWidth;
    if (r || W === X)
      return;
    const L = e && e <= Ge, d = e && e > Pe, h = window.innerWidth <= W;
    if (L || h) {
      z(t.MODE_FLY);
      return;
    }
    if (d && !h) {
      z(t.MODE_FLUID);
      return;
    }
  }, Ne = (e) => {
    const L = I, d = window.innerWidth * 0.5, h = M || d;
    v((Y) => {
      const ye = R === t.RIGHT ? Y + e : Y - e, G = ke(K, h, ye);
      return pe(G === d), L && !I ? d : G;
    }), u();
  }, Le = () => {
    document.body?.classList.add("pointer-events-none"), B(!0);
  }, Ce = () => {
    document.body?.classList.remove("pointer-events-none"), B(!1), ie();
  }, De = () => {
    const e = !a;
    H(e), O(e);
  }, Fe = m(
    "Sidebar",
    fe,
    o && "closed",
    a && "width-100vw sidebar-fullscreen",
    I && !a && "max-width-50vw width-50vw",
    S === D.MODE_FLY || a ? "fly" : "fluid"
  ), Re = m("SidebarHeader", de, te && "show-border"), Me = m("SidebarTitle", ae), U = m("SidebarBody", le), _e = m("SidebarResizeLimit", be && "display-block"), A = M || window.innerWidth * 0.5, we = /* @__PURE__ */ c(P, { children: [
    /* @__PURE__ */ s("div", { className: _e, style: N ? { right: A } : { left: A } }),
    /* @__PURE__ */ c("div", { className: "SidebarContent", children: [
      /* @__PURE__ */ c("div", { className: Re, children: [
        /* @__PURE__ */ s("div", { className: Me, children: F }),
        /* @__PURE__ */ c("div", { className: "SidebarButtons non-printable close", children: [
          y,
          T && /* @__PURE__ */ s(
            ve,
            {
              isFullscreen: a,
              onClick: De,
              tooltip: ee
            }
          ),
          !_ && (y || T) && /* @__PURE__ */ s("div", { className: "SidebarButtons-spacer" }),
          !_ && /* @__PURE__ */ s(ze, { title: F, onClick: oe })
        ] })
      ] }),
      Q ? /* @__PURE__ */ s(xe, { slideIn: !0, className: U, ref: w, children: g }) : /* @__PURE__ */ s("div", { className: U, ref: w, children: g }),
      /* @__PURE__ */ s(Oe, { footer: Z, className: ce })
    ] }),
    q && /* @__PURE__ */ s(
      E,
      {
        onResizeStart: Le,
        onResize: Ne,
        onResizeEnd: Ce,
        direction: E.HORIZONTAL,
        position: N ? E.LEFT : E.RIGHT
      }
    )
  ] });
  return /* @__PURE__ */ c(P, { children: [
    /* @__PURE__ */ s(
      We.div,
      {
        ...ue,
        initial: x && "sidebarEnter",
        animate: "sidebarVisible",
        exit: "sidebarExit",
        variants: Xe,
        custom: { fromRight: N, closed: o },
        transition: { duration: Ve },
        className: Fe,
        style: { width: he },
        ref: f,
        children: we
      }
    ),
    J && !o && /* @__PURE__ */ s(
      ge,
      {
        className: me,
        makeBackdropVisible: $,
        onClick: ne
      }
    )
  ] });
};
t.LEFT = C.LEFT;
t.RIGHT = C.RIGHT;
t.MODE_FLY = D.MODE_FLY;
t.MODE_FLUID = D.MODE_FLUID;
export {
  t as default
};
//# sourceMappingURL=Sidebar.js.map
