"use strict";
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GQtyError = exports.useHasuraClientInner = void 0;
exports.useHasuraClient = useHasuraClient;
const create_client_1 = require("./create-client");
var create_client_2 = require("./create-client");
Object.defineProperty(exports, "useHasuraClientInner", { enumerable: true, get: function () { return create_client_2.useHasuraClientInner; } });
__exportStar(require("./gqty"), exports);
var gqty_1 = require("gqty");
Object.defineProperty(exports, "GQtyError", { enumerable: true, get: function () { return gqty_1.GQtyError; } });
function useHasuraClient(args) {
    const { client, subscriptionsClient } = (0, create_client_1.useHasuraClientInner)(args);
    const retries = args.retries || 3;
    return {
        client,
        subscriptionsClient,
        async mutate(resolved, options) {
            if (!options)
                options = {};
            if (options.noCache === undefined)
                options.noCache = true;
            let err;
            for (let tries = 1; tries <= retries; tries++) {
                try {
                    const result = await client.resolve(({ mutation }) => resolved(mutation), options);
                    return result;
                }
                catch (error) {
                    err = error;
                    if (error.message !== 'database query error')
                        break;
                    await new Promise((resolve) => setTimeout(resolve, 1000 * tries + 1000));
                }
            }
            if (args.onError)
                throw args.onError(err) || err;
            throw err;
        },
        async query(resolved, options) {
            let err;
            for (let tries = 1; tries <= retries; tries++) {
                try {
                    const result = await client.resolve(({ query }) => resolved(query), options);
                    return result;
                }
                catch (error) {
                    err = error;
                    if (error.message !== 'database query error')
                        break;
                    await new Promise((resolve) => setTimeout(resolve, 1000 * tries + 1000));
                }
            }
            if (args.onError)
                throw args.onError(err) || err;
            throw err;
        },
        subscribe(resolved, onData, onError) {
            if (!subscriptionsClient)
                throw new Error('Subscriptions not enabled');
            let unsub;
            const stop = async () => {
                if (!unsub)
                    return;
                await unsub().catch(() => undefined);
            };
            client.resolved(() => resolved(client.subscription), {
                onSubscription(event) {
                    if (event.unsubscribe)
                        unsub = event.unsubscribe;
                    if (event.type === 'data')
                        onData(event.data, stop);
                    else if (event.type === 'with-errors' && onError)
                        onError(event.error, stop);
                },
            });
            return stop;
        },
    };
}
