"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.useRipple = void 0;
const react_1 = __importStar(require("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] = (0, react_1.useState)([]);
    const idCounter = (0, react_1.useRef)(0);
    const addRippleAt = (0, react_1.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 = (0, react_1.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 = (0, react_1.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 = (0, react_1.useCallback)((evt) => {
        const id = Number(evt.currentTarget.dataset.rippleId);
        setRipples((arr) => arr.filter((r) => r.id !== id));
    }, []);
    const elements = ripples.map((r) => (react_1.default.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,
    };
};
exports.useRipple = useRipple;
//# sourceMappingURL=useRipple.js.map