import B from "./index-extends.js";
import G from "./index-objectWithoutPropertiesLoose.js";
import * as e from "react";
import _ from "./index-_virtual16.js";
import { clsx as c } from "./index-clsx.js";
import j from "./index-styled.js";
import U from "./index-Ripple.js";
import i from "./index-touchRippleClasses.js";
import { jsx as k } from "react/jsx-runtime";
import { useDefaultProps as F } from "./index-DefaultPropsProvider.js";
import J from "./index-useTimeout.js";
import Q from "./index-TransitionGroup.js";
import { keyframes as E } from "./index-emotion-react.browser.esm.js";
const Z = ["center", "classes", "className"];
let y = (r) => r, O, V, K, S;
const w = 550, v = 80, tt = E(O || (O = y`
  0% {
    transform: scale(0);
    opacity: 0.1;
  }

  100% {
    transform: scale(1);
    opacity: 0.3;
  }
`)), et = E(V || (V = y`
  0% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
`)), it = E(K || (K = y`
  0% {
    transform: scale(1);
  }

  50% {
    transform: scale(0.92);
  }

  100% {
    transform: scale(1);
  }
`)), rt = j("span", {
  name: "MuiTouchRipple",
  slot: "Root"
})({
  overflow: "hidden",
  pointerEvents: "none",
  position: "absolute",
  zIndex: 0,
  top: 0,
  right: 0,
  bottom: 0,
  left: 0,
  borderRadius: "inherit"
}), ot = j(U, {
  name: "MuiTouchRipple",
  slot: "Ripple"
})(S || (S = y`
  opacity: 0;
  position: absolute;

  &.${0} {
    opacity: 0.3;
    transform: scale(1);
    animation-name: ${0};
    animation-duration: ${0}ms;
    animation-timing-function: ${0};
  }

  &.${0} {
    animation-duration: ${0}ms;
  }

  & .${0} {
    opacity: 1;
    display: block;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: currentColor;
  }

  & .${0} {
    opacity: 0;
    animation-name: ${0};
    animation-duration: ${0}ms;
    animation-timing-function: ${0};
  }

  & .${0} {
    position: absolute;
    /* @noflip */
    left: 0px;
    top: 0;
    animation-name: ${0};
    animation-duration: 2500ms;
    animation-timing-function: ${0};
    animation-iteration-count: infinite;
    animation-delay: 200ms;
  }
`), i.rippleVisible, tt, w, ({
  theme: r
}) => r.transitions.easing.easeInOut, i.ripplePulsate, ({
  theme: r
}) => r.transitions.duration.shorter, i.child, i.childLeaving, et, w, ({
  theme: r
}) => r.transitions.easing.easeInOut, i.childPulsate, it, ({
  theme: r
}) => r.transitions.easing.easeInOut), nt = /* @__PURE__ */ e.forwardRef(function(q, A) {
  const I = F({
    props: q,
    name: "MuiTouchRipple"
  }), {
    center: L = !1,
    classes: n = {},
    className: H
  } = I, W = G(I, Z), [N, X] = e.useState([]), Y = e.useRef(0), h = e.useRef(null);
  e.useEffect(() => {
    h.current && (h.current(), h.current = null);
  }, [N]);
  const M = e.useRef(!1), d = J(), o = e.useRef(null), z = e.useRef(null), P = e.useCallback((t) => {
    const {
      pulsate: a,
      rippleX: s,
      rippleY: R,
      rippleSize: b,
      cb: $
    } = t;
    X((l) => [...l, /* @__PURE__ */ k(ot, {
      classes: {
        ripple: c(n.ripple, i.ripple),
        rippleVisible: c(n.rippleVisible, i.rippleVisible),
        ripplePulsate: c(n.ripplePulsate, i.ripplePulsate),
        child: c(n.child, i.child),
        childLeaving: c(n.childLeaving, i.childLeaving),
        childPulsate: c(n.childPulsate, i.childPulsate)
      },
      timeout: w,
      pulsate: a,
      rippleX: s,
      rippleY: R,
      rippleSize: b
    }, Y.current)]), Y.current += 1, h.current = $;
  }, [n]), g = e.useCallback((t = {}, a = {}, s = () => {
  }) => {
    const {
      pulsate: R = !1,
      center: b = L || a.pulsate,
      fakeElement: $ = !1
      // For test purposes
    } = a;
    if ((t == null ? void 0 : t.type) === "mousedown" && M.current) {
      M.current = !1;
      return;
    }
    (t == null ? void 0 : t.type) === "touchstart" && (M.current = !0);
    const l = $ ? null : z.current, p = l ? l.getBoundingClientRect() : {
      width: 0,
      height: 0,
      left: 0,
      top: 0
    };
    let u, m, f;
    if (b || t === void 0 || t.clientX === 0 && t.clientY === 0 || !t.clientX && !t.touches)
      u = Math.round(p.width / 2), m = Math.round(p.height / 2);
    else {
      const {
        clientX: x,
        clientY: C
      } = t.touches && t.touches.length > 0 ? t.touches[0] : t;
      u = Math.round(x - p.left), m = Math.round(C - p.top);
    }
    if (b)
      f = Math.sqrt((2 * p.width ** 2 + p.height ** 2) / 3), f % 2 === 0 && (f += 1);
    else {
      const x = Math.max(Math.abs((l ? l.clientWidth : 0) - u), u) * 2 + 2, C = Math.max(Math.abs((l ? l.clientHeight : 0) - m), m) * 2 + 2;
      f = Math.sqrt(x ** 2 + C ** 2);
    }
    t != null && t.touches ? o.current === null && (o.current = () => {
      P({
        pulsate: R,
        rippleX: u,
        rippleY: m,
        rippleSize: f,
        cb: s
      });
    }, d.start(v, () => {
      o.current && (o.current(), o.current = null);
    })) : P({
      pulsate: R,
      rippleX: u,
      rippleY: m,
      rippleSize: f,
      cb: s
    });
  }, [L, P, d]), D = e.useCallback(() => {
    g({}, {
      pulsate: !0
    });
  }, [g]), T = e.useCallback((t, a) => {
    if (d.clear(), (t == null ? void 0 : t.type) === "touchend" && o.current) {
      o.current(), o.current = null, d.start(0, () => {
        T(t, a);
      });
      return;
    }
    o.current = null, X((s) => s.length > 0 ? s.slice(1) : s), h.current = a;
  }, [d]);
  return e.useImperativeHandle(A, () => ({
    pulsate: D,
    start: g,
    stop: T
  }), [D, g, T]), /* @__PURE__ */ k(rt, B({
    className: c(i.root, n.root, H),
    ref: z
  }, W, {
    children: /* @__PURE__ */ k(Q, {
      component: null,
      exit: !0,
      children: N
    })
  }));
});
process.env.NODE_ENV !== "production" && (nt.propTypes = {
  /**
   * If `true`, the ripple starts at the center of the component
   * rather than at the point of interaction.
   */
  center: _.bool,
  /**
   * Override or extend the styles applied to the component.
   */
  classes: _.object,
  /**
   * @ignore
   */
  className: _.string
});
export {
  v as DELAY_RIPPLE,
  ot as TouchRippleRipple,
  rt as TouchRippleRoot,
  nt as default
};
