import W from "./index-objectWithoutPropertiesLoose.js";
import s from "./index-extends.js";
import * as r from "react";
import { getPickersPopperUtilityClass as X } from "./index-pickersPopperClasses.js";
import { getActiveElement as G } from "./index-utils11.js";
import { jsx as E, jsxs as Y } from "react/jsx-runtime";
import $ from "./index-useThemeProps.js";
import q from "./index-Grow.js";
import g from "./index-useForkRef.js";
import { PickersActionBar as J } from "./index-PickersActionBar.js";
import Q from "./index-FocusTrap.js";
import V from "./index-useEventCallback2.js";
import L from "./index-ownerDocument2.js";
import Z from "./index-composeClasses.js";
import x from "./index-styled.js";
import ee from "./index-Popper.js";
import te from "./index-Paper.js";
const oe = ["onClick", "onTouchStart"], re = (t) => {
  const {
    classes: o
  } = t;
  return Z({
    root: ["root"],
    paper: ["paper"]
  }, X, o);
}, ne = x(ee, {
  name: "MuiPickersPopper",
  slot: "Root",
  overridesResolver: (t, o) => o.root
})(({
  theme: t
}) => ({
  zIndex: t.zIndex.modal
})), ce = x(te, {
  name: "MuiPickersPopper",
  slot: "Paper",
  overridesResolver: (t, o) => o.paper
})(({
  ownerState: t
}) => s({
  transformOrigin: "top center",
  outline: 0
}, t.placement === "top" && {
  transformOrigin: "bottom center"
}));
function se(t, o) {
  return o.documentElement.clientWidth < t.clientX || o.documentElement.clientHeight < t.clientY;
}
function ie(t, o) {
  const i = r.useRef(!1), p = r.useRef(!1), n = r.useRef(null), f = r.useRef(!1);
  r.useEffect(() => {
    if (!t)
      return;
    function e() {
      f.current = !0;
    }
    return document.addEventListener("mousedown", e, !0), document.addEventListener("touchstart", e, !0), () => {
      document.removeEventListener("mousedown", e, !0), document.removeEventListener("touchstart", e, !0), f.current = !1;
    };
  }, [t]);
  const c = V((e) => {
    if (!f.current)
      return;
    const m = p.current;
    p.current = !1;
    const k = L(n.current);
    if (!n.current || // is a TouchEvent?
    "clientX" in e && se(e, k))
      return;
    if (i.current) {
      i.current = !1;
      return;
    }
    let d;
    e.composedPath ? d = e.composedPath().indexOf(n.current) > -1 : d = !k.documentElement.contains(e.target) || n.current.contains(e.target), !d && !m && o(e);
  }), u = () => {
    p.current = !0;
  };
  return r.useEffect(() => {
    if (t) {
      const e = L(n.current), m = () => {
        i.current = !0;
      };
      return e.addEventListener("touchstart", c), e.addEventListener("touchmove", m), () => {
        e.removeEventListener("touchstart", c), e.removeEventListener("touchmove", m);
      };
    }
  }, [t, c]), r.useEffect(() => {
    if (t) {
      const e = L(n.current);
      return e.addEventListener("click", c), () => {
        e.removeEventListener("click", c), p.current = !1;
      };
    }
  }, [t, c]), [n, u, u];
}
function Te(t) {
  var o;
  const i = $({
    props: t,
    name: "MuiPickersPopper"
  }), {
    anchorEl: p,
    children: n,
    containerRef: f = null,
    onBlur: c,
    onClose: u,
    onClear: e,
    onAccept: m,
    onCancel: k,
    onSetToday: d,
    open: a,
    PopperProps: F,
    role: R,
    TransitionComponent: M = q,
    TrapFocusProps: B,
    PaperProps: T = {},
    components: C,
    componentsProps: v
  } = i;
  r.useEffect(() => {
    function l(y) {
      a && (y.key === "Escape" || y.key === "Esc") && u();
    }
    return document.addEventListener("keydown", l), () => {
      document.removeEventListener("keydown", l);
    };
  }, [u, a]);
  const P = r.useRef(null);
  r.useEffect(() => {
    R !== "tooltip" && (a ? P.current = G(document) : P.current && P.current instanceof HTMLElement && setTimeout(() => {
      P.current instanceof HTMLElement && P.current.focus();
    }));
  }, [a, R]);
  const [_, D, O] = ie(a, c ?? u), j = r.useRef(null), H = g(j, f), I = g(H, _), w = i, A = re(w), {
    onClick: S,
    onTouchStart: b
  } = T, K = W(T, oe), z = (l) => {
    l.key === "Escape" && (l.stopPropagation(), u());
  }, N = (o = C == null ? void 0 : C.ActionBar) != null ? o : J, U = (C == null ? void 0 : C.PaperContent) || r.Fragment;
  return /* @__PURE__ */ E(ne, s({
    transition: !0,
    role: R,
    open: a,
    anchorEl: p,
    onKeyDown: z,
    className: A.root
  }, F, {
    children: ({
      TransitionProps: l,
      placement: y
    }) => /* @__PURE__ */ E(Q, s({
      open: a,
      disableAutoFocus: !0,
      disableRestoreFocus: !0,
      disableEnforceFocus: R === "tooltip",
      isEnabled: () => !0
    }, B, {
      children: /* @__PURE__ */ E(M, s({}, l, {
        children: /* @__PURE__ */ E(ce, s({
          tabIndex: -1,
          elevation: 8,
          ref: I,
          onClick: (h) => {
            D(h), S && S(h);
          },
          onTouchStart: (h) => {
            O(h), b && b(h);
          },
          ownerState: s({}, w, {
            placement: y
          }),
          className: A.paper
        }, K, {
          children: /* @__PURE__ */ Y(U, s({}, v == null ? void 0 : v.paperContent, {
            children: [n, /* @__PURE__ */ E(N, s({
              onAccept: m,
              onClear: e,
              onCancel: k,
              onSetToday: d,
              actions: []
            }, v == null ? void 0 : v.actionBar))]
          }))
        }))
      }))
    }))
  }));
}
export {
  Te as PickersPopper
};
