"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLoadableQuery = useLoadableQuery;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const cache_1 = require("@apollo/client/cache");
const internal_1 = require("@apollo/client/react/internal");
const invariant_1 = require("@apollo/client/utilities/invariant");
const index_js_1 = require("./internal/index.cjs");
const useApolloClient_js_1 = require("./useApolloClient.cjs");
const useSuspenseQuery_js_1 = require("./useSuspenseQuery.cjs");
function useLoadableQuery(query, options = {}) {
    const client = (0, useApolloClient_js_1.useApolloClient)(options.client);
    const suspenseCache = (0, internal_1.getSuspenseCache)(client);
    const watchQueryOptions = (0, useSuspenseQuery_js_1.useWatchQueryOptions)({ client, query, options });
    const { queryKey = [] } = options;
    const [queryRef, setQueryRef] = React.useState(null);
    (0, internal_1.assertWrappedQueryRef)(queryRef);
    const internalQueryRef = queryRef && (0, internal_1.unwrapQueryRef)(queryRef);
    if (queryRef && internalQueryRef?.didChangeOptions(watchQueryOptions)) {
        const promise = internalQueryRef.applyOptions(watchQueryOptions);
        (0, internal_1.updateWrappedQueryRef)(queryRef, promise);
    }
    const calledDuringRender = (0, index_js_1.useRenderGuard)();
    const fetchMore = React.useCallback((options) => {
        if (!internalQueryRef) {
            throw new Error("The query has not been loaded. Please load the query.");
        }
        const promise = internalQueryRef.fetchMore(options);
        setQueryRef((0, internal_1.wrapQueryRef)(internalQueryRef));
        return promise;
    }, [internalQueryRef]);
    const refetch = React.useCallback((options) => {
        if (!internalQueryRef) {
            throw new Error("The query has not been loaded. Please load the query.");
        }
        const promise = internalQueryRef.refetch(options);
        setQueryRef((0, internal_1.wrapQueryRef)(internalQueryRef));
        return promise;
    }, [internalQueryRef]);
    const loadQuery = React.useCallback((...args) => {
        (0, invariant_1.invariant)(!calledDuringRender(), 34);
        const [variables] = args;
        const cacheKey = [
            query,
            (0, cache_1.canonicalStringify)(variables),
            ...[].concat(queryKey),
        ];
        const queryRef = suspenseCache.getQueryRef(cacheKey, () => client.watchQuery({
            ...watchQueryOptions,
            variables,
        }));
        setQueryRef((0, internal_1.wrapQueryRef)(queryRef));
    }, [
        query,
        queryKey,
        suspenseCache,
        watchQueryOptions,
        calledDuringRender,
        client,
    ]);
    const subscribeToMore = React.useCallback((options) => {
        (0, invariant_1.invariant)(internalQueryRef, 35);
        return internalQueryRef.observable.subscribeToMore(
        // TODO: The internalQueryRef doesn't have TVariables' type information so we have to cast it here
        options);
    }, [internalQueryRef]);
    const reset = React.useCallback(() => {
        setQueryRef(null);
    }, []);
    return [loadQuery, queryRef, { fetchMore, refetch, reset, subscribeToMore }];
}
//# sourceMappingURL=useLoadableQuery.cjs.map
