import { Observable } from "rxjs";
import { CombinedGraphQLErrors, graphQLResultHasProtocolErrors, PROTOCOL_ERRORS_SYMBOL, toErrorLike, } from "@apollo/client/errors";
import { ApolloLink } from "@apollo/client/link";
/**
 * @deprecated
 * Use `ErrorLink` from `@apollo/client/link/error` instead.
 */
export function onError(errorHandler) {
    return new ErrorLink(errorHandler);
}
export class ErrorLink extends ApolloLink {
    constructor(errorHandler) {
        super((operation, forward) => {
            return new Observable((observer) => {
                let sub;
                let retriedSub;
                let retriedResult;
                try {
                    sub = forward(operation).subscribe({
                        next: (result) => {
                            const handler = operation.client["queryManager"].incrementalHandler;
                            const errors = handler.isIncrementalResult(result) ?
                                handler.extractErrors(result)
                                : result.errors;
                            if (errors) {
                                retriedResult = errorHandler({
                                    error: new CombinedGraphQLErrors(result, errors),
                                    result,
                                    operation,
                                    forward,
                                });
                            }
                            else if (graphQLResultHasProtocolErrors(result)) {
                                retriedResult = errorHandler({
                                    error: result.extensions[PROTOCOL_ERRORS_SYMBOL],
                                    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();
                };
            });
        });
    }
}
//# sourceMappingURL=index.js.map
