'use strict';

var React = require('react');

/**
 * useGamepads — polls connected gamepads every frame.
 * Returns an array of GamepadSnapshot objects (0-8 pads).
 * Designed for couch mini-games (higher level than InputManager).
 */
const AXIS_THRESHOLD = 0.5;
const TRIGGER_THRESHOLD = 0.3;
function snapshot(gp) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
    const btns = gp.buttons;
    const axes = gp.axes;
    const dpUp = !!((_a = btns[12]) === null || _a === void 0 ? void 0 : _a.pressed);
    const dpDown = !!((_b = btns[13]) === null || _b === void 0 ? void 0 : _b.pressed);
    const dpLeft = !!((_c = btns[14]) === null || _c === void 0 ? void 0 : _c.pressed);
    const dpRight = !!((_d = btns[15]) === null || _d === void 0 ? void 0 : _d.pressed);
    const lt = ((_f = (_e = btns[6]) === null || _e === void 0 ? void 0 : _e.value) !== null && _f !== void 0 ? _f : 0) > TRIGGER_THRESHOLD || !!((_g = btns[6]) === null || _g === void 0 ? void 0 : _g.pressed);
    const rt = ((_j = (_h = btns[7]) === null || _h === void 0 ? void 0 : _h.value) !== null && _j !== void 0 ? _j : 0) > TRIGGER_THRESHOLD || !!((_k = btns[7]) === null || _k === void 0 ? void 0 : _k.pressed);
    return {
        index: gp.index,
        up: dpUp || (axes[1] != null && axes[1] < -AXIS_THRESHOLD),
        down: dpDown || (axes[1] != null && axes[1] > AXIS_THRESHOLD),
        left: dpLeft || (axes[0] != null && axes[0] < -AXIS_THRESHOLD),
        right: dpRight || (axes[0] != null && axes[0] > AXIS_THRESHOLD),
        dpUp, dpDown, dpLeft, dpRight,
        lx: (_l = axes[0]) !== null && _l !== void 0 ? _l : 0,
        ly: (_m = axes[1]) !== null && _m !== void 0 ? _m : 0,
        rx: (_o = axes[2]) !== null && _o !== void 0 ? _o : 0,
        ry: (_p = axes[3]) !== null && _p !== void 0 ? _p : 0,
        a: !!((_q = btns[0]) === null || _q === void 0 ? void 0 : _q.pressed),
        b: !!((_r = btns[1]) === null || _r === void 0 ? void 0 : _r.pressed),
        x: !!((_s = btns[2]) === null || _s === void 0 ? void 0 : _s.pressed),
        y: !!((_t = btns[3]) === null || _t === void 0 ? void 0 : _t.pressed),
        lb: !!((_u = btns[4]) === null || _u === void 0 ? void 0 : _u.pressed),
        rb: !!((_v = btns[5]) === null || _v === void 0 ? void 0 : _v.pressed),
        lt, rt,
        start: !!((_w = btns[9]) === null || _w === void 0 ? void 0 : _w.pressed),
        select: !!((_x = btns[8]) === null || _x === void 0 ? void 0 : _x.pressed),
    };
}
function useGamepads() {
    const [pads, setPads] = React.useState([]);
    const rafRef = React.useRef(0);
    React.useEffect(() => {
        const poll = () => {
            var _a, _b;
            const raw = (_b = (_a = navigator.getGamepads) === null || _a === void 0 ? void 0 : _a.call(navigator)) !== null && _b !== void 0 ? _b : [];
            const snaps = [];
            for (const gp of raw) {
                if (gp)
                    snaps.push(snapshot(gp));
            }
            setPads(snaps);
            rafRef.current = requestAnimationFrame(poll);
        };
        rafRef.current = requestAnimationFrame(poll);
        return () => cancelAnimationFrame(rafRef.current);
    }, []);
    return pads;
}
/**
 * useGamepadEdges — returns snapshots + edge detection (justPressed).
 * Compares current frame to previous frame for each button.
 */
function useGamepadEdges() {
    const pads = useGamepads();
    const prevRef = React.useRef([]);
    const justPressed = (padIndex, button) => {
        const cur = pads.find(p => p.index === padIndex);
        const prev = prevRef.current.find(p => p.index === padIndex);
        if (!cur)
            return false;
        const cv = cur[button];
        const pv = prev === null || prev === void 0 ? void 0 : prev[button];
        return cv === true && pv !== true;
    };
    React.useEffect(() => { prevRef.current = pads; });
    return { pads, justPressed };
}

exports.useGamepadEdges = useGamepadEdges;
exports.useGamepads = useGamepads;
//# sourceMappingURL=useGamepads.js.map
