"use strict";;
const {
    __DEV__
} = require("@apollo/client/utilities/environment");

Object.defineProperty(exports, "__esModule", { value: true });
exports.ApolloLink = void 0;
const rxjs_1 = require("rxjs");
const utils_1 = require("@apollo/client/link/utils");
const invariant_1 = require("@apollo/client/utilities/invariant");
function passthrough(op, forward) {
    return (forward ? forward(op) : rxjs_1.EMPTY);
}
function toLink(handler) {
    return typeof handler === "function" ? new ApolloLink(handler) : handler;
}
function isTerminating(link) {
    return link.request.length <= 1;
}
class ApolloLink {
    static empty() {
        return new ApolloLink(() => rxjs_1.EMPTY);
    }
    static from(links) {
        if (links.length === 0)
            return ApolloLink.empty();
        return links.map(toLink).reduce((x, y) => x.concat(y));
    }
    static split(test, left, right) {
        const leftLink = toLink(left);
        const rightLink = toLink(right || new ApolloLink(passthrough));
        let ret;
        if (isTerminating(leftLink) && isTerminating(rightLink)) {
            ret = new ApolloLink((operation) => {
                return test(operation) ?
                    leftLink.request(operation) || rxjs_1.EMPTY
                    : rightLink.request(operation) || rxjs_1.EMPTY;
            });
        }
        else {
            ret = new ApolloLink((operation, forward) => {
                return test(operation) ?
                    leftLink.request(operation, forward) || rxjs_1.EMPTY
                    : rightLink.request(operation, forward) || rxjs_1.EMPTY;
            });
        }
        return Object.assign(ret, { left: leftLink, right: rightLink });
    }
    static execute(link, operation) {
        return (link.request((0, utils_1.createOperation)(operation.context, (0, utils_1.transformOperation)((0, utils_1.validateOperation)(operation)))) || rxjs_1.EMPTY);
    }
    static concat(first, second) {
        const firstLink = toLink(first);
        if (isTerminating(firstLink)) {
            __DEV__ && invariant_1.invariant.warn(56, firstLink);
            return firstLink;
        }
        const nextLink = toLink(second);
        let ret;
        if (isTerminating(nextLink)) {
            ret = new ApolloLink((operation) => firstLink.request(operation, (op) => nextLink.request(op) || rxjs_1.EMPTY) ||
                rxjs_1.EMPTY);
        }
        else {
            ret = new ApolloLink((operation, forward) => {
                return (firstLink.request(operation, (op) => {
                    return nextLink.request(op, forward) || rxjs_1.EMPTY;
                }) || rxjs_1.EMPTY);
            });
        }
        return Object.assign(ret, { left: firstLink, right: nextLink });
    }
    constructor(request) {
        if (request)
            this.request = request;
    }
    split(test, left, right) {
        return this.concat(ApolloLink.split(test, left, right || new ApolloLink(passthrough)));
    }
    concat(next) {
        return ApolloLink.concat(this, next);
    }
    request(operation, forward) {
        throw (0, invariant_1.newInvariantError)(57);
    }
    /**
     * @internal
     * Used to iterate through all links that are concatenations or `split` links.
     */
    left;
    /**
     * @internal
     * Used to iterate through all links that are concatenations or `split` links.
     */
    right;
    /**
     * @internal
     * Can be provided by a link that has an internal cache to report it's memory details.
     */
    getMemoryInternals;
}
exports.ApolloLink = ApolloLink;
//# sourceMappingURL=ApolloLink.cjs.map
