import * as React from "rehackt";
import { unwrapQueryRef, updateWrappedQueryRef, } from "../cache/QueryReference.js";
import { __use } from "./internal/index.js";
import { toApolloError } from "./useSuspenseQuery.js";
import { useSyncExternalStore } from "./useSyncExternalStore.js";
import { invariant } from "../../utilities/globals/index.js";
export function useReadQuery(queryRef) {
    var internalQueryRef = React.useMemo(function () { return unwrapQueryRef(queryRef); }, [queryRef]);
    React.useEffect(function () {
        if (!internalQueryRef.disposed) {
            return internalQueryRef.retain();
        }
    }, [internalQueryRef]);
    if (globalThis.__DEV__ !== false) {
        var didWarnOnDisposedQueryRef = React.useRef(void 0);
        if (didWarnOnDisposedQueryRef.current !== internalQueryRef &&
            internalQueryRef.disposed) {
            globalThis.__DEV__ !== false && invariant.warn(53);
            didWarnOnDisposedQueryRef.current = internalQueryRef;
        }
    }
    var promise = useSyncExternalStore(React.useCallback(function (forceUpdate) {
        return internalQueryRef.listen(function (promise) {
            updateWrappedQueryRef(queryRef, promise);
            forceUpdate();
        });
    }, [internalQueryRef]), function () { return queryRef.toPromise(); }, function () { return queryRef.toPromise(); });
    var result = __use(promise);
    return React.useMemo(function () {
        return {
            data: result.data,
            networkStatus: result.networkStatus,
            error: toApolloError(result),
        };
    }, [result]);
}
//# sourceMappingURL=useReadQuery.js.map