"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorLink = void 0;
exports.onError = onError;
const rxjs_1 = require("rxjs");
const errors_1 = require("@apollo/client/errors");
const core_1 = require("@apollo/client/link/core");
function onError(errorHandler) {
    return new core_1.ApolloLink((operation, forward) => {
        return new rxjs_1.Observable((observer) => {
            let sub;
            let retriedSub;
            let retriedResult;
            try {
                sub = forward(operation).subscribe({
                    next: (result) => {
                        if (result.errors) {
                            retriedResult = errorHandler({
                                error: new errors_1.CombinedGraphQLErrors(result),
                                response: result,
                                operation,
                                forward,
                            });
                        }
                        else if ((0, errors_1.graphQLResultHasProtocolErrors)(result)) {
                            retriedResult = errorHandler({
                                error: result.extensions[errors_1.PROTOCOL_ERRORS_SYMBOL],
                                response: result,
                                operation,
                                forward,
                            });
                        }
                        retriedSub = retriedResult?.subscribe(observer);
                        if (!retriedSub) {
                            observer.next(result);
                        }
                    },
                    error: (error) => {
                        retriedResult = errorHandler({
                            operation,
                            error: (0, errors_1.toErrorLike)(error),
                            forward,
                        });
                        retriedSub = retriedResult?.subscribe(observer);
                        if (!retriedSub) {
                            observer.error(error);
                        }
                    },
                    complete: () => {
                        // disable the previous sub from calling complete on observable
                        // if retry is in flight.
                        if (!retriedResult) {
                            observer.complete();
                        }
                    },
                });
            }
            catch (e) {
                errorHandler({ error: (0, errors_1.toErrorLike)(e), operation, forward });
                observer.error(e);
            }
            return () => {
                if (sub)
                    sub.unsubscribe();
                if (retriedSub)
                    retriedSub.unsubscribe();
            };
        });
    });
}
class ErrorLink extends core_1.ApolloLink {
    link;
    constructor(errorHandler) {
        super();
        this.link = onError(errorHandler);
    }
    request(operation, forward) {
        return this.link.request(operation, forward);
    }
}
exports.ErrorLink = ErrorLink;
//# sourceMappingURL=index.cjs.map
