"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConversationPrepareHandler = void 0;
const graphql_transformer_core_1 = require("@aws-amplify/graphql-transformer-core");
class ConversationPrepareHandler {
    constructor(modelTransformer, hasManyTransformer, belongsToTransformer, authProvider) {
        this.modelTransformer = modelTransformer;
        this.hasManyTransformer = hasManyTransformer;
        this.belongsToTransformer = belongsToTransformer;
        this.authProvider = authProvider;
    }
    prepare(ctx, directives) {
        for (const directive of directives) {
            this.prepareResourcesForDirective(directive, ctx);
        }
    }
    prepareResourcesForDirective(directive, ctx) {
        const { conversationAuthDirective, conversationModelDirective, conversationHasManyMessagesDirective, conversationMessagesField, conversationModel, } = directive.conversationModel;
        const { messageAuthDirective, messageModelDirective, messageBelongsToConversationDirective, messageConversationField, messageModel, messageSubscription, assistantMutationField, assistantMutationInput, assistantStreamingMutationField, assistantStreamingMutationInput, } = directive.messageModel;
        const sessionModelName = conversationModel.name.value;
        const messageModelName = messageModel.name.value;
        ctx.output.addInput(assistantMutationInput);
        ctx.output.addInput(assistantStreamingMutationInput);
        ctx.output.addMutationFields([assistantMutationField, assistantStreamingMutationField]);
        ctx.output.addSubscriptionFields([messageSubscription]);
        ctx.output.addObject(conversationModel);
        ctx.output.addObject(messageModel);
        ctx.providerRegistry.registerDataSourceProvider(conversationModel, this.modelTransformer);
        ctx.providerRegistry.registerDataSourceProvider(messageModel, this.modelTransformer);
        ctx.dataSourceStrategies[sessionModelName] = graphql_transformer_core_1.DDB_AMPLIFY_MANAGED_DATASOURCE_STRATEGY;
        ctx.dataSourceStrategies[messageModelName] = graphql_transformer_core_1.DDB_AMPLIFY_MANAGED_DATASOURCE_STRATEGY;
        this.modelTransformer.object(conversationModel, conversationModelDirective, ctx);
        this.modelTransformer.object(messageModel, messageModelDirective, ctx);
        this.modelTransformer.before(ctx);
        this.belongsToTransformer.field(messageModel, messageConversationField, messageBelongsToConversationDirective, ctx);
        this.hasManyTransformer.field(conversationModel, conversationMessagesField, conversationHasManyMessagesDirective, ctx);
        if (!this.authProvider.object) {
            throw new graphql_transformer_core_1.InvalidTransformerError('No authentication provider found.');
        }
        this.authProvider.object(conversationModel, conversationAuthDirective, ctx);
        this.authProvider.object(messageModel, messageAuthDirective, ctx);
    }
}
exports.ConversationPrepareHandler = ConversationPrepareHandler;
//# sourceMappingURL=conversation-prepare-handler.js.map