import v from "./index-extends.js";
import j from "./index-objectWithoutPropertiesLoose.js";
import * as p from "react";
import e from "./index-_virtual16.js";
import { clsx as Z } from "./index-clsx.js";
import le from "./index-styled.js";
import { getPopoverUtilityClass as Ee } from "./index-popoverClasses.js";
import { jsx as z } from "react/jsx-runtime";
import Oe from "./index-Paper.js";
import { useDefaultProps as Re } from "./index-DefaultPropsProvider.js";
import xe from "./index-Grow.js";
import we from "./index-useForkRef.js";
import ee from "./index-ownerDocument2.js";
import oe from "./index-ownerWindow2.js";
import Ne from "./index-debounce3.js";
import te from "./index-useSlotProps.js";
import Se from "./index-isHostComponent.js";
import Ce from "./index-composeClasses.js";
import _e from "./index-Modal2.js";
import De from "./index-integerPropType.js";
import Me from "./index-refType.js";
import je from "./index-elementTypeAcceptingRef.js";
import ne from "./index-HTMLElementType.js";
import ze from "./index-chainPropTypes.js";
const ke = ["onEntering"], Le = ["action", "anchorEl", "anchorOrigin", "anchorPosition", "anchorReference", "children", "className", "container", "elevation", "marginThreshold", "open", "PaperProps", "slots", "slotProps", "transformOrigin", "TransitionComponent", "transitionDuration", "TransitionProps", "disableScrollLock"], Ie = ["slotProps"];
function re(t, o) {
  let i = 0;
  return typeof o == "number" ? i = o : o === "center" ? i = t.height / 2 : o === "bottom" && (i = t.height), i;
}
function ie(t, o) {
  let i = 0;
  return typeof o == "number" ? i = o : o === "center" ? i = t.width / 2 : o === "right" && (i = t.width), i;
}
function se(t) {
  return [t.horizontal, t.vertical].map((o) => typeof o == "number" ? `${o}px` : o).join(" ");
}
function x(t) {
  return typeof t == "function" ? t() : t;
}
const Ve = (t) => {
  const {
    classes: o
  } = t;
  return Ce({
    root: ["root"],
    paper: ["paper"]
  }, Ee, o);
}, We = le(_e, {
  name: "MuiPopover",
  slot: "Root",
  overridesResolver: (t, o) => o.root
})({}), $e = le(Oe, {
  name: "MuiPopover",
  slot: "Paper",
  overridesResolver: (t, o) => o.paper
})({
  position: "absolute",
  overflowY: "auto",
  overflowX: "hidden",
  // So we see the popover when it's empty.
  // It's most likely on issue on userland.
  minWidth: 16,
  minHeight: 16,
  maxWidth: "calc(100% - 32px)",
  maxHeight: "calc(100% - 32px)",
  // We disable the focus ring for mouse, touch and keyboard users.
  outline: 0
}), qe = /* @__PURE__ */ p.forwardRef(function(o, i) {
  var k, L, I;
  const y = Re({
    props: o,
    name: "MuiPopover"
  }), {
    action: ae,
    anchorEl: c,
    anchorOrigin: g = {
      vertical: "top",
      horizontal: "left"
    },
    anchorPosition: w,
    anchorReference: b = "anchorEl",
    children: pe,
    className: ce,
    container: fe,
    elevation: V = 8,
    marginThreshold: l = 16,
    open: u,
    PaperProps: ue = {},
    slots: E,
    slotProps: O,
    transformOrigin: T = {
      vertical: "top",
      horizontal: "left"
    },
    TransitionComponent: N = xe,
    transitionDuration: S = "auto",
    TransitionProps: {
      onEntering: W
    } = {},
    disableScrollLock: C = !1
  } = y, $ = j(y.TransitionProps, ke), he = j(y, Le), h = (k = O == null ? void 0 : O.paper) != null ? k : ue, _ = p.useRef(), me = we(_, h.ref), D = v({}, y, {
    anchorOrigin: g,
    anchorReference: b,
    elevation: V,
    marginThreshold: l,
    externalPaperSlotProps: h,
    transformOrigin: T,
    TransitionComponent: N,
    transitionDuration: S,
    TransitionProps: $
  }), q = Ve(D), H = p.useCallback(() => {
    if (b === "anchorPosition")
      return process.env.NODE_ENV, w;
    const n = x(c), r = n && n.nodeType === 1 ? n : ee(_.current).body, s = r.getBoundingClientRect();
    if (process.env.NODE_ENV !== "production") {
      const m = r.getBoundingClientRect();
      process.env.NODE_ENV !== "test" && m.top === 0 && m.left === 0 && m.right === 0 && m.bottom;
    }
    return {
      top: s.top + re(s, g.vertical),
      left: s.left + ie(s, g.horizontal)
    };
  }, [c, g.horizontal, g.vertical, w, b]), U = p.useCallback((n) => ({
    vertical: re(n, T.vertical),
    horizontal: ie(n, T.horizontal)
  }), [T.horizontal, T.vertical]), A = p.useCallback((n) => {
    const r = {
      width: n.offsetWidth,
      height: n.offsetHeight
    }, s = U(r);
    if (b === "none")
      return {
        top: null,
        left: null,
        transformOrigin: se(s)
      };
    const m = H();
    let d = m.top - s.vertical, P = m.left - s.horizontal;
    const X = d + r.height, J = P + r.width, K = oe(x(c)), R = K.innerHeight - l, Q = K.innerWidth - l;
    if (l !== null && d < l) {
      const a = d - l;
      d -= a, s.vertical += a;
    } else if (l !== null && X > R) {
      const a = X - R;
      d -= a, s.vertical += a;
    }
    if (process.env.NODE_ENV !== "production" && r.height > R && r.height, l !== null && P < l) {
      const a = P - l;
      P -= a, s.horizontal += a;
    } else if (J > Q) {
      const a = J - Q;
      P -= a, s.horizontal += a;
    }
    return {
      top: `${Math.round(d)}px`,
      left: `${Math.round(P)}px`,
      transformOrigin: se(s)
    };
  }, [c, b, H, U, l]), [de, B] = p.useState(u), f = p.useCallback(() => {
    const n = _.current;
    if (!n)
      return;
    const r = A(n);
    r.top !== null && (n.style.top = r.top), r.left !== null && (n.style.left = r.left), n.style.transformOrigin = r.transformOrigin, B(!0);
  }, [A]);
  p.useEffect(() => (C && window.addEventListener("scroll", f), () => window.removeEventListener("scroll", f)), [c, C, f]);
  const Pe = (n, r) => {
    W && W(n, r), f();
  }, ve = () => {
    B(!1);
  };
  p.useEffect(() => {
    u && f();
  }), p.useImperativeHandle(ae, () => u ? {
    updatePosition: () => {
      f();
    }
  } : null, [u, f]), p.useEffect(() => {
    if (!u)
      return;
    const n = Ne(() => {
      f();
    }), r = oe(c);
    return r.addEventListener("resize", n), () => {
      n.clear(), r.removeEventListener("resize", n);
    };
  }, [c, u, f]);
  let F = S;
  S === "auto" && !N.muiSupportAuto && (F = void 0);
  const ge = fe || (c ? ee(x(c)).body : void 0), M = (L = E == null ? void 0 : E.root) != null ? L : We, Y = (I = E == null ? void 0 : E.paper) != null ? I : $e, be = te({
    elementType: Y,
    externalSlotProps: v({}, h, {
      style: de ? h.style : v({}, h.style, {
        opacity: 0
      })
    }),
    additionalProps: {
      elevation: V,
      ref: me
    },
    ownerState: D,
    className: Z(q.paper, h == null ? void 0 : h.className)
  }), G = te({
    elementType: M,
    externalSlotProps: (O == null ? void 0 : O.root) || {},
    externalForwardedProps: he,
    additionalProps: {
      ref: i,
      slotProps: {
        backdrop: {
          invisible: !0
        }
      },
      container: ge,
      open: u
    },
    ownerState: D,
    className: Z(q.root, ce)
  }), {
    slotProps: Te
  } = G, ye = j(G, Ie);
  return /* @__PURE__ */ z(M, v({}, ye, !Se(M) && {
    slotProps: Te,
    disableScrollLock: C
  }, {
    children: /* @__PURE__ */ z(N, v({
      appear: !0,
      in: u,
      onEntering: Pe,
      onExited: ve,
      timeout: F
    }, $, {
      children: /* @__PURE__ */ z(Y, v({}, be, {
        children: pe
      }))
    }))
  }));
});
process.env.NODE_ENV !== "production" && (qe.propTypes = {
  // ┌────────────────────────────── Warning ──────────────────────────────┐
  // │ These PropTypes are generated from the TypeScript type definitions. │
  // │    To update them, edit the d.ts file and run `pnpm proptypes`.     │
  // └─────────────────────────────────────────────────────────────────────┘
  /**
   * A ref for imperative actions.
   * It currently only supports updatePosition() action.
   */
  action: Me,
  /**
   * An HTML element, [PopoverVirtualElement](/material-ui/react-popover/#virtual-element),
   * or a function that returns either.
   * It's used to set the position of the popover.
   */
  anchorEl: ze(e.oneOfType([ne, e.func]), (t) => {
    if (t.open && (!t.anchorReference || t.anchorReference === "anchorEl")) {
      const o = x(t.anchorEl);
      if (o && o.nodeType === 1) {
        const i = o.getBoundingClientRect();
        if (process.env.NODE_ENV !== "test" && i.top === 0 && i.left === 0 && i.right === 0 && i.bottom === 0)
          return new Error(["MUI: The `anchorEl` prop provided to the component is invalid.", "The anchor element should be part of the document layout.", "Make sure the element is present in the document or that it's not display none."].join(`
`));
      } else
        return new Error(["MUI: The `anchorEl` prop provided to the component is invalid.", `It should be an Element or PopoverVirtualElement instance but it's \`${o}\` instead.`].join(`
`));
    }
    return null;
  }),
  /**
   * This is the point on the anchor where the popover's
   * `anchorEl` will attach to. This is not used when the
   * anchorReference is 'anchorPosition'.
   *
   * Options:
   * vertical: [top, center, bottom];
   * horizontal: [left, center, right].
   * @default {
   *   vertical: 'top',
   *   horizontal: 'left',
   * }
   */
  anchorOrigin: e.shape({
    horizontal: e.oneOfType([e.oneOf(["center", "left", "right"]), e.number]).isRequired,
    vertical: e.oneOfType([e.oneOf(["bottom", "center", "top"]), e.number]).isRequired
  }),
  /**
   * This is the position that may be used to set the position of the popover.
   * The coordinates are relative to the application's client area.
   */
  anchorPosition: e.shape({
    left: e.number.isRequired,
    top: e.number.isRequired
  }),
  /**
   * This determines which anchor prop to refer to when setting
   * the position of the popover.
   * @default 'anchorEl'
   */
  anchorReference: e.oneOf(["anchorEl", "anchorPosition", "none"]),
  /**
   * The content of the component.
   */
  children: e.node,
  /**
   * Override or extend the styles applied to the component.
   */
  classes: e.object,
  /**
   * @ignore
   */
  className: e.string,
  /**
   * An HTML element, component instance, or function that returns either.
   * The `container` will passed to the Modal component.
   *
   * By default, it uses the body of the anchorEl's top-level document object,
   * so it's simply `document.body` most of the time.
   */
  container: e.oneOfType([ne, e.func]),
  /**
   * Disable the scroll lock behavior.
   * @default false
   */
  disableScrollLock: e.bool,
  /**
   * The elevation of the popover.
   * @default 8
   */
  elevation: De,
  /**
   * Specifies how close to the edge of the window the popover can appear.
   * If null, the popover will not be constrained by the window.
   * @default 16
   */
  marginThreshold: e.number,
  /**
   * Callback fired when the component requests to be closed.
   * The `reason` parameter can optionally be used to control the response to `onClose`.
   */
  onClose: e.func,
  /**
   * If `true`, the component is shown.
   */
  open: e.bool.isRequired,
  /**
   * Props applied to the [`Paper`](/material-ui/api/paper/) element.
   *
   * This prop is an alias for `slotProps.paper` and will be overriden by it if both are used.
   * @deprecated Use `slotProps.paper` instead.
   *
   * @default {}
   */
  PaperProps: e.shape({
    component: je
  }),
  /**
   * The extra props for the slot components.
   * You can override the existing props or add new ones.
   *
   * @default {}
   */
  slotProps: e.shape({
    paper: e.oneOfType([e.func, e.object]),
    root: e.oneOfType([e.func, e.object])
  }),
  /**
   * The components used for each slot inside.
   *
   * @default {}
   */
  slots: e.shape({
    paper: e.elementType,
    root: e.elementType
  }),
  /**
   * The system prop that allows defining system overrides as well as additional CSS styles.
   */
  sx: e.oneOfType([e.arrayOf(e.oneOfType([e.func, e.object, e.bool])), e.func, e.object]),
  /**
   * This is the point on the popover which
   * will attach to the anchor's origin.
   *
   * Options:
   * vertical: [top, center, bottom, x(px)];
   * horizontal: [left, center, right, x(px)].
   * @default {
   *   vertical: 'top',
   *   horizontal: 'left',
   * }
   */
  transformOrigin: e.shape({
    horizontal: e.oneOfType([e.oneOf(["center", "left", "right"]), e.number]).isRequired,
    vertical: e.oneOfType([e.oneOf(["bottom", "center", "top"]), e.number]).isRequired
  }),
  /**
   * The component used for the transition.
   * [Follow this guide](/material-ui/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
   * @default Grow
   */
  TransitionComponent: e.elementType,
  /**
   * Set to 'auto' to automatically calculate transition time based on height.
   * @default 'auto'
   */
  transitionDuration: e.oneOfType([e.oneOf(["auto"]), e.number, e.shape({
    appear: e.number,
    enter: e.number,
    exit: e.number
  })]),
  /**
   * Props applied to the transition element.
   * By default, the element is based on this [`Transition`](https://reactcommunity.org/react-transition-group/transition/) component.
   * @default {}
   */
  TransitionProps: e.object
});
export {
  $e as PopoverPaper,
  We as PopoverRoot,
  qe as default,
  ie as getOffsetLeft,
  re as getOffsetTop
};
