"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMessageModel = void 0;
const graphql_directives_1 = require("@aws-amplify/graphql-directives");
const graphql_1 = require("graphql");
const graphql_transformer_common_1 = require("graphql-transformer-common");
const createMessageModel = (conversationModelName, messageModelName, referenceFieldName, capitalizedFieldName, conversationMessageInterface) => {
    const messageSubscriptionFieldName = `onCreateAssistantResponse${capitalizedFieldName}`;
    const assistantMutationFieldName = `createAssistantResponse${capitalizedFieldName}`;
    const assistantStreamingMutationFieldName = `createAssistantResponseStream${capitalizedFieldName}`;
    const messageAuthDirective = constructMessageAuthDirective();
    const messageModelDirective = constructMessageModelDirective();
    const messageBelongsToConversationDirective = constructMessageSessionFieldBelongsToDirective(referenceFieldName);
    const messageConversationField = constructMessageSessionField(messageBelongsToConversationDirective, conversationModelName);
    const messageModel = constructConversationMessageModel(messageModelName, messageConversationField, referenceFieldName, [messageModelDirective, messageAuthDirective], conversationMessageInterface);
    const messageSubscription = constructMessageSubscription(messageSubscriptionFieldName, messageModelName, [
        assistantMutationFieldName,
        assistantStreamingMutationFieldName,
    ]);
    const assistantMutationInput = constructAssistantResponseMutationInput(messageModelName);
    const assistantMutationField = constructAssistantMutationField(assistantMutationFieldName, messageModelName, assistantMutationInput.name.value);
    const assistantStreamingMutationInput = constructAssistantResponseStreamingMutationInput(messageModelName);
    const assistantStreamingMutationField = constructAssistantStreamingMutationField(assistantStreamingMutationFieldName, messageModelName, assistantStreamingMutationInput.name.value);
    return {
        messageAuthDirective,
        messageModelDirective,
        messageBelongsToConversationDirective,
        messageConversationField,
        messageModel,
        messageSubscription,
        assistantMutationInput,
        assistantMutationField,
        assistantStreamingMutationInput,
        assistantStreamingMutationField,
    };
};
exports.createMessageModel = createMessageModel;
const constructMessageModelDirective = () => {
    return (0, graphql_transformer_common_1.makeDirective)('model', [
        (0, graphql_transformer_common_1.makeArgument)('subscriptions', (0, graphql_transformer_common_1.makeValueNode)({ onUpdate: null, onDelete: null })),
        (0, graphql_transformer_common_1.makeArgument)('mutations', (0, graphql_transformer_common_1.makeValueNode)({ update: null })),
    ]);
};
const constructMessageAuthDirective = () => {
    return (0, graphql_transformer_common_1.makeDirective)('auth', [
        (0, graphql_transformer_common_1.makeArgument)('rules', {
            kind: graphql_1.Kind.LIST,
            values: [
                {
                    kind: graphql_1.Kind.OBJECT,
                    fields: [
                        {
                            kind: graphql_1.Kind.OBJECT_FIELD,
                            name: { kind: graphql_1.Kind.NAME, value: 'allow' },
                            value: { kind: graphql_1.Kind.ENUM, value: 'owner' },
                        },
                        {
                            kind: graphql_1.Kind.OBJECT_FIELD,
                            name: { kind: graphql_1.Kind.NAME, value: 'ownerField' },
                            value: { kind: graphql_1.Kind.STRING, value: 'owner' },
                        },
                    ],
                },
            ],
        }),
    ]);
};
const constructMessageSessionFieldBelongsToDirective = (referenceFieldName) => {
    const referencesArg = (0, graphql_transformer_common_1.makeArgument)('references', (0, graphql_transformer_common_1.makeValueNode)(referenceFieldName));
    return (0, graphql_transformer_common_1.makeDirective)(graphql_directives_1.BelongsToDirective.name, [referencesArg]);
};
const constructMessageSessionField = (belongsToDirective, typeName) => {
    return (0, graphql_transformer_common_1.makeField)('conversation', [], (0, graphql_transformer_common_1.makeNamedType)(typeName), [belongsToDirective]);
};
const constructConversationMessageModel = (modelName, sessionField, referenceFieldName, typeDirectives, conversationMessageInterface) => {
    const id = (0, graphql_transformer_common_1.makeField)('id', [], (0, graphql_transformer_common_1.wrapNonNull)((0, graphql_transformer_common_1.makeNamedType)('ID')));
    const conversationId = (0, graphql_transformer_common_1.makeField)(referenceFieldName, [], (0, graphql_transformer_common_1.wrapNonNull)((0, graphql_transformer_common_1.makeNamedType)('ID')));
    const role = (0, graphql_transformer_common_1.makeField)('role', [], (0, graphql_transformer_common_1.makeNamedType)('ConversationParticipantRole'));
    const content = (0, graphql_transformer_common_1.makeField)('content', [], (0, graphql_transformer_common_1.makeListType)((0, graphql_transformer_common_1.makeNamedType)('ContentBlock')));
    const context = (0, graphql_transformer_common_1.makeField)('aiContext', [], (0, graphql_transformer_common_1.makeNamedType)('AWSJSON'));
    const uiComponents = (0, graphql_transformer_common_1.makeField)('toolConfiguration', [], (0, graphql_transformer_common_1.makeNamedType)('ToolConfiguration'));
    const associatedUserMessageId = (0, graphql_transformer_common_1.makeField)('associatedUserMessageId', [], (0, graphql_transformer_common_1.makeNamedType)('ID'));
    const object = {
        ...(0, graphql_transformer_common_1.blankObject)(modelName),
        interfaces: [conversationMessageInterface],
        fields: [id, conversationId, sessionField, role, content, context, uiComponents, associatedUserMessageId],
        directives: typeDirectives,
    };
    return object;
};
const constructMessageSubscription = (subscriptionName, conversationMessageTypeName, onMutationNames) => {
    const awsSubscribeDirective = (0, graphql_transformer_common_1.makeDirective)('aws_subscribe', [(0, graphql_transformer_common_1.makeArgument)('mutations', (0, graphql_transformer_common_1.makeValueNode)(onMutationNames))]);
    const cognitoAuthDirective = (0, graphql_transformer_common_1.makeDirective)('aws_cognito_user_pools', []);
    const args = [(0, graphql_transformer_common_1.makeInputValueDefinition)('conversationId', (0, graphql_transformer_common_1.makeNamedType)('ID'))];
    const subscriptionField = (0, graphql_transformer_common_1.makeField)(subscriptionName, args, (0, graphql_transformer_common_1.makeNamedType)(conversationMessageTypeName), [
        awsSubscribeDirective,
        cognitoAuthDirective,
    ]);
    return subscriptionField;
};
const constructAssistantMutationField = (fieldName, messageModelName, inputTypeName) => {
    const args = [(0, graphql_transformer_common_1.makeInputValueDefinition)('input', (0, graphql_transformer_common_1.makeNonNullType)((0, graphql_transformer_common_1.makeNamedType)(inputTypeName)))];
    const cognitoAuthDirective = (0, graphql_transformer_common_1.makeDirective)('aws_cognito_user_pools', []);
    const createAssistantResponseMutation = (0, graphql_transformer_common_1.makeField)(fieldName, args, (0, graphql_transformer_common_1.makeNamedType)(messageModelName), [cognitoAuthDirective]);
    return createAssistantResponseMutation;
};
const constructAssistantResponseMutationInput = (messageModelName) => {
    const inputName = `Create${messageModelName}AssistantInput`;
    return {
        kind: 'InputObjectTypeDefinition',
        name: { kind: 'Name', value: inputName },
        fields: [
            (0, graphql_transformer_common_1.makeInputValueDefinition)('conversationId', (0, graphql_transformer_common_1.makeNamedType)('ID')),
            (0, graphql_transformer_common_1.makeInputValueDefinition)('content', (0, graphql_transformer_common_1.makeListType)((0, graphql_transformer_common_1.makeNamedType)('ContentBlockInput'))),
            (0, graphql_transformer_common_1.makeInputValueDefinition)('associatedUserMessageId', (0, graphql_transformer_common_1.makeNamedType)('ID')),
        ],
    };
};
const constructAssistantStreamingMutationField = (fieldName, messageModelName, inputTypeName) => {
    const args = [(0, graphql_transformer_common_1.makeInputValueDefinition)('input', (0, graphql_transformer_common_1.makeNonNullType)((0, graphql_transformer_common_1.makeNamedType)(inputTypeName)))];
    const cognitoAuthDirective = (0, graphql_transformer_common_1.makeDirective)('aws_cognito_user_pools', []);
    const createAssistantResponseMutation = (0, graphql_transformer_common_1.makeField)(fieldName, args, (0, graphql_transformer_common_1.makeNamedType)(messageModelName), [cognitoAuthDirective]);
    return createAssistantResponseMutation;
};
const constructAssistantResponseStreamingMutationInput = (messageModelName) => {
    const inputName = `Create${messageModelName}AssistantStreamingInput`;
    return {
        kind: 'InputObjectTypeDefinition',
        name: { kind: 'Name', value: inputName },
        fields: [
            (0, graphql_transformer_common_1.makeInputValueDefinition)('conversationId', (0, graphql_transformer_common_1.makeNonNullType)((0, graphql_transformer_common_1.makeNamedType)('ID'))),
            (0, graphql_transformer_common_1.makeInputValueDefinition)('associatedUserMessageId', (0, graphql_transformer_common_1.makeNonNullType)((0, graphql_transformer_common_1.makeNamedType)('ID'))),
            (0, graphql_transformer_common_1.makeInputValueDefinition)('contentBlockIndex', (0, graphql_transformer_common_1.makeNonNullType)((0, graphql_transformer_common_1.makeNamedType)('Int'))),
            (0, graphql_transformer_common_1.makeInputValueDefinition)('contentBlockText', (0, graphql_transformer_common_1.makeNamedType)('String')),
            (0, graphql_transformer_common_1.makeInputValueDefinition)('contentBlockDeltaIndex', (0, graphql_transformer_common_1.makeNamedType)('Int')),
            (0, graphql_transformer_common_1.makeInputValueDefinition)('contentBlockToolUse', (0, graphql_transformer_common_1.makeNamedType)('String')),
            (0, graphql_transformer_common_1.makeInputValueDefinition)('contentBlockDoneAtIndex', (0, graphql_transformer_common_1.makeNamedType)('Int')),
            (0, graphql_transformer_common_1.makeInputValueDefinition)('stopReason', (0, graphql_transformer_common_1.makeNamedType)('String')),
        ],
    };
};
//# sourceMappingURL=message-model.js.map