// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { useCallback, useContext, useDebugValue, useErrorBoundary, useId, useImperativeHandle, useMemo, useEffect as usePreactEffect, useState as usePreactState, useReducer, useRef, } from 'preact/hooks';
import { isProfiling, profileEnd, profileFlowId, profileStart } from '../debug/profile.js';
// Cache profiled wrappers by the original preact setter to preserve stable
// identity without introducing extra hooks in component render flow.
const stateSetterTraceCache = /* @__PURE__ */ isProfiling
    ? new WeakMap()
    : undefined;
function buildTraceOption(flowId, stack) {
    if (!stack) {
        return { flowId };
    }
    return {
        flowId,
        args: {
            stack,
        },
    };
}
function withEffectProfile(effect, traceName, flowId, stack) {
    const traceOption = buildTraceOption(flowId, stack);
    return () => {
        profileStart(traceName, traceOption);
        try {
            const cleanup = effect();
            if (typeof cleanup !== 'function') {
                return cleanup;
            }
            return () => {
                profileStart(`${traceName}::cleanup`, traceOption);
                try {
                    cleanup();
                }
                finally {
                    profileEnd();
                }
            };
        }
        finally {
            profileEnd();
        }
    };
}
function useEffectWithProfile(effect, deps, traceName) {
    const flowId = profileFlowId();
    const stack = new Error().stack;
    const traceOption = buildTraceOption(flowId, stack);
    profileStart(traceName, traceOption);
    try {
        return usePreactEffect(withEffectProfile(effect, `${traceName}::callback`, flowId, stack), deps);
    }
    finally {
        profileEnd();
    }
}
function useLayoutEffectProfiled(effect, deps) {
    return useEffectWithProfile(effect, deps, 'ReactLynx::hooks::useLayoutEffect');
}
function useEffectProfiled(effect, deps) {
    return useEffectWithProfile(effect, deps, 'ReactLynx::hooks::useEffect');
}
function useStateWithProfile(initialState) {
    const [state, setState] = (arguments.length === 0
        ? usePreactState()
        : usePreactState(initialState));
    const genericSetState = setState;
    const cachedTracedSetState = stateSetterTraceCache?.get(genericSetState);
    if (cachedTracedSetState) {
        return [state, cachedTracedSetState];
    }
    const tracedSetState = ((nextState) => {
        const stack = new Error().stack;
        const traceOption = stack
            ? { args: { stack } }
            : undefined;
        profileStart('ReactLynx::hooks::useState::setter', traceOption);
        try {
            return setState(nextState);
        }
        finally {
            profileEnd();
        }
    });
    stateSetterTraceCache?.set(genericSetState, tracedSetState);
    return [state, tracedSetState];
}
const useState = (__BACKGROUND__ && isProfiling)
    ? useStateWithProfile
    : usePreactState;
/**
 * Accepts a function that contains imperative, possibly effectful code.
 * The effects run after main thread dom update without blocking it.
 *
 * @param effect - Imperative function that can return a cleanup function
 * @param deps - If present, effect will only activate if the values in the list change (using ===).
 *
 * @public
 */
const useEffect = (__BACKGROUND__ && isProfiling)
    ? useEffectProfiled
    : usePreactEffect;
/**
 * `useLayoutEffect` is now an alias of `useEffect`. Use `useEffect` instead.
 *
 * Accepts a function that contains imperative, possibly effectful code. The effects run after main thread dom update without blocking it.
 *
 * @param effect - Imperative function that can return a cleanup function
 * @param deps - If present, effect will only activate if the values in the list change (using ===).
 *
 * @public
 *
 * @deprecated `useLayoutEffect` in the background thread cannot offer the precise timing for reading layout information and synchronously re-render, which is different from React.
 */
const useLayoutEffect = (__BACKGROUND__ && isProfiling)
    ? useLayoutEffectProfiled
    : usePreactEffect;
export { 
// preact
useState, useReducer, useRef, useImperativeHandle, useLayoutEffect, useEffect, useCallback, useMemo, useContext, useDebugValue, useErrorBoundary, useId, };
//# sourceMappingURL=react.js.map