"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var mapResources_1 = require("./mapResources");
var utils_1 = require("./utils");
var graphql_1 = require("graphql");
var mapParametersToArguments_1 = require("./mapParametersToArguments");
var mapApi = function (apiJson, context) {
    var _a, _b;
    var name = apiJson.name, id = apiJson.id, description = apiJson.description, parameters = apiJson.parameters, version = apiJson.version, resources = apiJson.resources, baseUrl = apiJson.baseUrl, schemas = apiJson.schemas;
    var graphQLTypes = context.graphQLTypes, resolvers = context.resolvers;
    var API_ROOT = utils_1.upperFirst(name) + "Resources";
    var queryTypeName = utils_1.upperFirst(name) + "ApiQuery";
    resolvers[queryTypeName] = (_a = {}, _a[utils_1.upperFirst(name) + "Api"] = function (parent) { return parent; }, _a);
    resolvers[utils_1.upperFirst(name) + "ApiRoot"] = { root: function (parent) { return parent; } };
    var resourceResolvers = {};
    resolvers[API_ROOT] = resourceResolvers;
    var fields = mapResources_1.mapResources(resources, context.graphQLTypes, resourceResolvers, resolvers);
    if (utils_1.keys(fields).length === 0) {
        throw "No fields for API " + id;
    }
    var schema = new graphql_1.GraphQLSchema({
        query: new graphql_1.GraphQLObjectType({
            name: queryTypeName,
            fields: (_b = {},
                _b[utils_1.upperFirst(name) + "Api"] = {
                    type: new graphql_1.GraphQLObjectType({
                        name: utils_1.upperFirst(name) + "ApiRoot",
                        fields: {
                            root: {
                                type: new graphql_1.GraphQLObjectType({
                                    name: API_ROOT,
                                    fields: fields
                                }),
                                args: mapParametersToArguments_1.mapParametersToArguments(parameters, 'Root')
                            }
                        }
                    })
                },
                _b)
        })
    });
    return { schema: graphql_1.printSchema(schema), resolvers: resolvers };
};
exports.mapApi = mapApi;
//# sourceMappingURL=mapApi.js.map