"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
};
var FederationModule_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FederationModule = void 0;
const gateway_1 = require("@apollo/gateway");
const core_1 = require("@rxdi/core");
const apollo_server_1 = require("apollo-server");
const auth_data_source_1 = require("./auth-data-source");
const tokens_1 = require("./tokens");
let FederationModule = FederationModule_1 = class FederationModule {
    static forRoot({ port, serviceList, willSendRequest, context, apolloServerConfig, }) {
        return {
            module: FederationModule_1,
            providers: [
                {
                    provide: tokens_1.ApolloGatewayInternal,
                    useFactory: () => new gateway_1.ApolloGateway({
                        serviceList,
                        buildService: ({ url }) => {
                            const auth = new auth_data_source_1.AuthenticatedDataSource({ url });
                            if (willSendRequest) {
                                auth.willSendRequest = willSendRequest.bind(auth);
                            }
                            return auth;
                        },
                        __exposeQueryPlanExperimental: true,
                    }),
                },
                {
                    provide: tokens_1.ApolloServerInternal,
                    deps: [tokens_1.ApolloGatewayInternal],
                    useFactory: (gateway) => new apollo_server_1.ApolloServer(Object.assign({ gateway, engine: false, context: context
                            ? context
                            : ({ req: { headers } }) => ({ headers }), subscriptions: false }, apolloServerConfig)),
                },
                {
                    provide: 'Listen',
                    deps: [tokens_1.ApolloServerInternal],
                    lazy: true,
                    useFactory: (server) => server.listen({ port }).then(({ url }) => {
                        console.log(`🚀 Gateway started at ${url}`);
                    }),
                },
            ],
        };
    }
};
FederationModule = FederationModule_1 = __decorate([
    core_1.Module()
], FederationModule);
exports.FederationModule = FederationModule;
__exportStar(require("./tokens"), exports);
//# sourceMappingURL=index.js.map