import { Observable } from "rxjs";
import { CombinedGraphQLErrors, graphQLResultHasProtocolErrors, PROTOCOL_ERRORS_SYMBOL, toErrorLike, } from "@apollo/client/errors";
import { ApolloLink } from "@apollo/client/link/core";
export function onError(errorHandler) {
    return new ApolloLink((operation, forward) => {
        return new Observable((observer) => {
            let sub;
            let retriedSub;
            let retriedResult;
            try {
                sub = forward(operation).subscribe({
                    next: (result) => {
                        if (result.errors) {
                            retriedResult = errorHandler({
                                error: new CombinedGraphQLErrors(result),
                                response: result,
                                operation,
                                forward,
                            });
                        }
                        else if (graphQLResultHasProtocolErrors(result)) {
                            retriedResult = errorHandler({
                                error: result.extensions[PROTOCOL_ERRORS_SYMBOL],
                                response: result,
                                operation,
                                forward,
                            });
                        }
                        retriedSub = retriedResult?.subscribe(observer);
                        if (!retriedSub) {
                            observer.next(result);
                        }
                    },
                    error: (error) => {
                        retriedResult = errorHandler({
                            operation,
                            error: 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: toErrorLike(e), operation, forward });
                observer.error(e);
            }
            return () => {
                if (sub)
                    sub.unsubscribe();
                if (retriedSub)
                    retriedSub.unsubscribe();
            };
        });
    });
}
export class ErrorLink extends ApolloLink {
    link;
    constructor(errorHandler) {
        super();
        this.link = onError(errorHandler);
    }
    request(operation, forward) {
        return this.link.request(operation, forward);
    }
}
//# sourceMappingURL=index.js.map
