"use strict";
"use client";
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.useEventCallback = useEventCallback;
/**
 * Stable event callback: returns a function whose identity never changes but always
 * invokes the latest `callback`. Avoids stale closures without re‑creating handlers.
 *
 * Why not `useCallback`? Its identity depends on a deps array:
 * - omit deps -> stale; include deps -> new function each render.
 * - This hook decouples identity from freshness.
 *
 * How it works: a single stable "trampoline" function delegates to a mutable ref. The current
 * `callback` is promoted from `next` in an insertion/layout phase effect so abandoned concurrent
 * renders cannot leak outdated handlers.
 *
 * Guarantees: stable identity; latest logic executed; no calls from uncommitted renders; dev
 * error if invoked during render; safe when `callback` is undefined (no-op).
 *
 * This hook is a more permissive version of React 19.2's `React.useEffectEvent` in that it can be passed through contexts and called in event handler props, not just effects.
 */
const react_1 = __importStar(require("react"));
const useRefWithInit_1 = require("./useRefWithInit");
/* https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379 */
const useInsertionEffect = react_1.default[`useInsertionEffect${Math.random().toFixed(1)}`.slice(0, -3)];
const useSafeInsertionEffect = 
// React 17 doesn't have useInsertionEffect.
useInsertionEffect &&
    // Preact replaces useInsertionEffect with useLayoutEffect and fires too late.
    useInsertionEffect !== react_1.useLayoutEffect
    ? useInsertionEffect
    : (fn) => fn();
function useEventCallback(callback) {
    const stable = (0, useRefWithInit_1.useRefWithInit)(createStableCallback).current;
    stable.next = callback; // eslint-disable-line react-hooks/immutability
    useSafeInsertionEffect(stable.effect);
    return stable.trampoline;
}
function createStableCallback() {
    const stable = {
        next: undefined,
        callback: assertNotCalled,
        trampoline: (...args) => { var _a; return (_a = stable.callback) === null || _a === void 0 ? void 0 : _a.call(stable, ...args); },
        effect: () => {
            stable.callback = stable.next;
        },
    };
    return stable;
}
function assertNotCalled() {
    if (process.env.NODE_ENV !== "production") {
        throw new Error("Aksel: Cannot call an event handler while rendering.");
    }
}
//# sourceMappingURL=useEventCallback.js.map