import x from "./index-extends.js";
import W from "./index-objectWithoutPropertiesLoose.js";
import * as n from "react";
import e from "./index-_virtual16.js";
import { getPopperUtilityClass as z } from "./index-popperClasses.js";
import { jsx as C } from "react/jsx-runtime";
import D from "./index-ownerDocument2.js";
import G from "./index-Portal.js";
import V from "./index-useForkRef.js";
import H from "./index-useEnhancedEffect2.js";
import { createPopper as J } from "./index-popper2.js";
import K from "./index-useSlotProps.js";
import Q from "./index-composeClasses.js";
import X from "./index-refType.js";
import L from "./index-HTMLElementType.js";
import Y from "./index-chainPropTypes.js";
const Z = ["anchorEl", "children", "direction", "disablePortal", "modifiers", "open", "placement", "popperOptions", "popperRef", "slotProps", "slots", "TransitionProps", "ownerState"], ee = ["anchorEl", "children", "container", "direction", "disablePortal", "keepMounted", "modifiers", "open", "placement", "popperOptions", "popperRef", "style", "transition", "slotProps", "slots"];
function te(o, t) {
  if (t === "ltr")
    return o;
  switch (o) {
    case "bottom-end":
      return "bottom-start";
    case "bottom-start":
      return "bottom-end";
    case "top-end":
      return "top-start";
    case "top-start":
      return "top-end";
    default:
      return o;
  }
}
function M(o) {
  return typeof o == "function" ? o() : o;
}
function w(o) {
  return o.nodeType !== void 0;
}
function oe(o) {
  return !w(o);
}
const ne = (o) => {
  const {
    classes: t
  } = o;
  return Q({
    root: ["root"]
  }, z, t);
}, re = {}, se = /* @__PURE__ */ n.forwardRef(function(t, r) {
  var c;
  const {
    anchorEl: f,
    children: d,
    direction: j,
    disablePortal: u,
    modifiers: m,
    open: v,
    placement: h,
    popperOptions: l,
    popperRef: O,
    slotProps: N = {},
    slots: U = {},
    TransitionProps: a
    // @ts-ignore internal logic
    // prevent from spreading to DOM, it can come from the parent component e.g. Select.
  } = t, _ = W(t, Z), g = n.useRef(null), S = V(g, r), i = n.useRef(null), P = V(i, O), b = n.useRef(P);
  H(() => {
    b.current = P;
  }, [P]), n.useImperativeHandle(O, () => i.current, []);
  const y = te(h, j), [E, k] = n.useState(y), [s, T] = n.useState(M(f));
  n.useEffect(() => {
    i.current && i.current.forceUpdate();
  }), n.useEffect(() => {
    f && T(M(f));
  }, [f]), H(() => {
    if (!s || !v)
      return;
    const $ = (p) => {
      k(p.placement);
    };
    if (process.env.NODE_ENV !== "production" && s && w(s) && s.nodeType === 1) {
      const p = s.getBoundingClientRect();
      process.env.NODE_ENV !== "test" && p.top === 0 && p.left === 0 && p.right === 0 && p.bottom;
    }
    let R = [{
      name: "preventOverflow",
      options: {
        altBoundary: u
      }
    }, {
      name: "flip",
      options: {
        altBoundary: u
      }
    }, {
      name: "onUpdate",
      enabled: !0,
      phase: "afterWrite",
      fn: ({
        state: p
      }) => {
        $(p);
      }
    }];
    m != null && (R = R.concat(m)), l && l.modifiers != null && (R = R.concat(l.modifiers));
    const B = J(s, g.current, x({
      placement: y
    }, l, {
      modifiers: R
    }));
    return b.current(B), () => {
      B.destroy(), b.current(null);
    };
  }, [s, u, m, v, l, y]);
  const I = {
    placement: E
  };
  a !== null && (I.TransitionProps = a);
  const q = ne(t), A = (c = U.root) != null ? c : "div", F = K({
    elementType: A,
    externalSlotProps: N.root,
    externalForwardedProps: _,
    additionalProps: {
      role: "tooltip",
      ref: S
    },
    ownerState: t,
    className: q.root
  });
  return /* @__PURE__ */ C(A, x({}, F, {
    children: typeof d == "function" ? d(I) : d
  }));
}), ie = /* @__PURE__ */ n.forwardRef(function(t, r) {
  const {
    anchorEl: c,
    children: f,
    container: d,
    direction: j = "ltr",
    disablePortal: u = !1,
    keepMounted: m = !1,
    modifiers: v,
    open: h,
    placement: l = "bottom",
    popperOptions: O = re,
    popperRef: N,
    style: U,
    transition: a = !1,
    slotProps: _ = {},
    slots: g = {}
  } = t, S = W(t, ee), [i, P] = n.useState(!0), b = () => {
    P(!1);
  }, y = () => {
    P(!0);
  };
  if (!m && !h && (!a || i))
    return null;
  let E;
  if (d)
    E = d;
  else if (c) {
    const T = M(c);
    E = T && w(T) ? D(T).body : D(null).body;
  }
  const k = !h && m && (!a || i) ? "none" : void 0, s = a ? {
    in: h,
    onEnter: b,
    onExited: y
  } : void 0;
  return /* @__PURE__ */ C(G, {
    disablePortal: u,
    container: E,
    children: /* @__PURE__ */ C(se, x({
      anchorEl: c,
      direction: j,
      disablePortal: u,
      modifiers: v,
      ref: r,
      open: a ? !i : h,
      placement: l,
      popperOptions: O,
      popperRef: N,
      slotProps: _,
      slots: g
    }, S, {
      style: x({
        // Prevents scroll issue, waiting for Popper.js to add this style once initiated.
        position: "fixed",
        // Fix Popper.js display issue
        top: 0,
        left: 0,
        display: k
      }, U),
      TransitionProps: s,
      children: f
    }))
  });
});
process.env.NODE_ENV !== "production" && (ie.propTypes = {
  // ┌────────────────────────────── Warning ──────────────────────────────┐
  // │ These PropTypes are generated from the TypeScript type definitions. │
  // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
  // └─────────────────────────────────────────────────────────────────────┘
  /**
   * An HTML element, [virtualElement](https://popper.js.org/docs/v2/virtual-elements/),
   * or a function that returns either.
   * It's used to set the position of the popper.
   * The return value will passed as the reference object of the Popper instance.
   */
  anchorEl: Y(e.oneOfType([L, e.object, e.func]), (o) => {
    if (o.open) {
      const t = M(o.anchorEl);
      if (t && w(t) && t.nodeType === 1) {
        const r = t.getBoundingClientRect();
        if (process.env.NODE_ENV !== "test" && r.top === 0 && r.left === 0 && r.right === 0 && r.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 if (!t || typeof t.getBoundingClientRect != "function" || oe(t) && t.contextElement != null && t.contextElement.nodeType !== 1)
        return new Error(["MUI: The `anchorEl` prop provided to the component is invalid.", "It should be an HTML element instance or a virtualElement ", "(https://popper.js.org/docs/v2/virtual-elements/)."].join(`
`));
    }
    return null;
  }),
  /**
   * Popper render function or node.
   */
  children: e.oneOfType([e.node, e.func]),
  /**
   * An HTML element or function that returns one.
   * The `container` will have the portal children appended to it.
   *
   * You can also provide a callback, which is called in a React layout effect.
   * This lets you set the container from a ref, and also makes server-side rendering possible.
   *
   * By default, it uses the body of the top-level document object,
   * so it's simply `document.body` most of the time.
   */
  container: e.oneOfType([L, e.func]),
  /**
   * Direction of the text.
   * @default 'ltr'
   */
  direction: e.oneOf(["ltr", "rtl"]),
  /**
   * The `children` will be under the DOM hierarchy of the parent component.
   * @default false
   */
  disablePortal: e.bool,
  /**
   * Always keep the children in the DOM.
   * This prop can be useful in SEO situation or
   * when you want to maximize the responsiveness of the Popper.
   * @default false
   */
  keepMounted: e.bool,
  /**
   * Popper.js is based on a "plugin-like" architecture,
   * most of its features are fully encapsulated "modifiers".
   *
   * A modifier is a function that is called each time Popper.js needs to
   * compute the position of the popper.
   * For this reason, modifiers should be very performant to avoid bottlenecks.
   * To learn how to create a modifier, [read the modifiers documentation](https://popper.js.org/docs/v2/modifiers/).
   */
  modifiers: e.arrayOf(e.shape({
    data: e.object,
    effect: e.func,
    enabled: e.bool,
    fn: e.func,
    name: e.any,
    options: e.object,
    phase: e.oneOf(["afterMain", "afterRead", "afterWrite", "beforeMain", "beforeRead", "beforeWrite", "main", "read", "write"]),
    requires: e.arrayOf(e.string),
    requiresIfExists: e.arrayOf(e.string)
  })),
  /**
   * If `true`, the component is shown.
   */
  open: e.bool.isRequired,
  /**
   * Popper placement.
   * @default 'bottom'
   */
  placement: e.oneOf(["auto-end", "auto-start", "auto", "bottom-end", "bottom-start", "bottom", "left-end", "left-start", "left", "right-end", "right-start", "right", "top-end", "top-start", "top"]),
  /**
   * Options provided to the [`Popper.js`](https://popper.js.org/docs/v2/constructors/#options) instance.
   * @default {}
   */
  popperOptions: e.shape({
    modifiers: e.array,
    onFirstUpdate: e.func,
    placement: e.oneOf(["auto-end", "auto-start", "auto", "bottom-end", "bottom-start", "bottom", "left-end", "left-start", "left", "right-end", "right-start", "right", "top-end", "top-start", "top"]),
    strategy: e.oneOf(["absolute", "fixed"])
  }),
  /**
   * A ref that points to the used popper instance.
   */
  popperRef: X,
  /**
   * The props used for each slot inside the Popper.
   * @default {}
   */
  slotProps: e.shape({
    root: e.oneOfType([e.func, e.object])
  }),
  /**
   * The components used for each slot inside the Popper.
   * Either a string to use a HTML element or a component.
   * @default {}
   */
  slots: e.shape({
    root: e.elementType
  }),
  /**
   * Help supporting a react-transition-group/Transition component.
   * @default false
   */
  transition: e.bool
});
export {
  ie as default
};
