import React, { useCallback, useRef, useState } from "react";
const HALF = 2;
const RIPPLE_SPEED_PX_PER_MS = 1.5;
const RIPPLE_MIN_DURATION_MS = 250;
const prefersReducedMotion = () => {
    if (typeof window === "undefined") {
        return false;
    }
    return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
};
const isDisabled = (el) => {
    if ("disabled" in el && el.disabled) {
        return true;
    }
    return el.getAttribute("aria-disabled") === "true";
};
const isRippleDisabledByCssVar = (el) => {
    if (typeof window === "undefined") {
        return false;
    }
    return window.getComputedStyle(el).getPropertyValue("--miui-ripple").trim() === "0";
};
const useRipple = (args) => {
    const { ref, onPointerDown, onKeyDown, fromCenter = false } = args;
    const [ripples, setRipples] = useState([]);
    const idCounter = useRef(0);
    const addRippleAt = useCallback((clientX, clientY) => {
        const el = ref.current;
        if (!el || isDisabled(el) || prefersReducedMotion() || isRippleDisabledByCssVar(el)) {
            return;
        }
        const rect = el.getBoundingClientRect();
        const x = clientX - rect.left;
        const y = clientY - rect.top;
        const maxX = Math.max(x, rect.width - x);
        const maxY = Math.max(y, rect.height - y);
        const size = Math.hypot(maxX, maxY) * HALF;
        const duration = Math.max(RIPPLE_MIN_DURATION_MS, size / RIPPLE_SPEED_PX_PER_MS);
        idCounter.current += 1;
        const id = idCounter.current;
        setRipples((arr) => [...arr, { id, x, y, size, duration }]);
    }, [ref]);
    const handlePointerDown = useCallback((evt) => {
        if (fromCenter) {
            const el = ref.current;
            if (el) {
                const rect = el.getBoundingClientRect();
                addRippleAt(rect.left + (rect.width / HALF), rect.top + (rect.height / HALF));
            }
        }
        else {
            addRippleAt(evt.clientX, evt.clientY);
        }
        onPointerDown === null || onPointerDown === void 0 ? void 0 : onPointerDown(evt);
    }, [addRippleAt, onPointerDown, fromCenter, ref]);
    const handleKeyDown = useCallback((evt) => {
        if (!evt.repeat && (evt.key === "Enter" || evt.key === " ")) {
            const el = ref.current;
            if (el) {
                const rect = el.getBoundingClientRect();
                addRippleAt(rect.left + (rect.width / HALF), rect.top + (rect.height / HALF));
            }
        }
        onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(evt);
    }, [addRippleAt, ref, onKeyDown]);
    const handleAnimationEnd = useCallback((evt) => {
        const id = Number(evt.currentTarget.dataset.rippleId);
        setRipples((arr) => arr.filter((r) => r.id !== id));
    }, []);
    const elements = ripples.map((r) => (React.createElement("span", { key: r.id, "data-miui-ripple": true, "data-ripple-id": r.id, "aria-hidden": true, style: {
            left: `${String(r.x - (r.size / HALF))}px`,
            top: `${String(r.y - (r.size / HALF))}px`,
            width: `${String(r.size)}px`,
            height: `${String(r.size)}px`,
            animationDuration: `${String(r.duration)}ms`,
        }, onAnimationEnd: handleAnimationEnd })));
    return {
        onPointerDown: handlePointerDown,
        onKeyDown: handleKeyDown,
        ripples: elements,
    };
};
export { useRipple };
//# sourceMappingURL=useRipple.js.map