import { __DEV__ } from "@apollo/client/utilities/environment";
import { EMPTY } from "rxjs";
import { createOperation, transformOperation, validateOperation, } from "@apollo/client/link/utils";
import { invariant, newInvariantError, } from "@apollo/client/utilities/invariant";
function passthrough(op, forward) {
    return (forward ? forward(op) : EMPTY);
}
function toLink(handler) {
    return typeof handler === "function" ? new ApolloLink(handler) : handler;
}
function isTerminating(link) {
    return link.request.length <= 1;
}
export class ApolloLink {
    static empty() {
        return new ApolloLink(() => 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) || EMPTY
                    : rightLink.request(operation) || EMPTY;
            });
        }
        else {
            ret = new ApolloLink((operation, forward) => {
                return test(operation) ?
                    leftLink.request(operation, forward) || EMPTY
                    : rightLink.request(operation, forward) || EMPTY;
            });
        }
        return Object.assign(ret, { left: leftLink, right: rightLink });
    }
    static execute(link, operation, context) {
        return (link.request(createOperation(operation.context, transformOperation(validateOperation(operation)), context)) || EMPTY);
    }
    static concat(first, second) {
        const firstLink = toLink(first);
        if (isTerminating(firstLink)) {
            __DEV__ && invariant.warn(63, firstLink);
            return firstLink;
        }
        const nextLink = toLink(second);
        let ret;
        if (isTerminating(nextLink)) {
            ret = new ApolloLink((operation) => firstLink.request(operation, (op) => nextLink.request(op) || EMPTY) ||
                EMPTY);
        }
        else {
            ret = new ApolloLink((operation, forward) => {
                return (firstLink.request(operation, (op) => {
                    return nextLink.request(op, forward) || EMPTY;
                }) || 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 newInvariantError(64);
    }
    /**
    * @internal
    * Used to iterate through all links that are concatenations or `split` links.
    * 
    * @deprecated This is an internal API and should not be used directly. This can be removed or changed at any time.
    */
    left;
    /**
    * @internal
    * Used to iterate through all links that are concatenations or `split` links.
    * 
    * @deprecated This is an internal API and should not be used directly. This can be removed or changed at any time.
    */
    right;
    /**
    * @internal
    * Can be provided by a link that has an internal cache to report it's memory details.
    * 
    * @deprecated This is an internal API and should not be used directly. This can be removed or changed at any time.
    */
    getMemoryInternals;
}
//# sourceMappingURL=ApolloLink.js.map
