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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { Arg, Ctx, Field, InputType, ObjectType, Query, Resolver } from 'type-graphql';
import { RunViewByIDInput } from '../generic/RunViewResolver.js';
import { Message } from '@memberjunction/communication-types';
import { EntityCommunicationsEngine } from '@memberjunction/entity-communications-server';
import { GraphQLJSONObject } from 'graphql-type-json';
import { TemplateEngineServer } from '@memberjunction/templates';
import { z } from 'zod';
let CommunicationProviderMessageType = class CommunicationProviderMessageType {
    ID;
    CommunicationProviderID;
    CommunicationBaseMessageTypeID;
    Name;
    Status;
    AdditionalAttributes;
    _mj_CreatedAt;
    _mj_UpdatedAt;
    CommunicationProvider;
    CommunicationBaseMessageType;
};
__decorate([
    Field(),
    __metadata("design:type", Number)
], CommunicationProviderMessageType.prototype, "ID", void 0);
__decorate([
    Field(),
    __metadata("design:type", Number)
], CommunicationProviderMessageType.prototype, "CommunicationProviderID", void 0);
__decorate([
    Field(),
    __metadata("design:type", Number)
], CommunicationProviderMessageType.prototype, "CommunicationBaseMessageTypeID", void 0);
__decorate([
    Field(),
    __metadata("design:type", String)
], CommunicationProviderMessageType.prototype, "Name", void 0);
__decorate([
    Field(),
    __metadata("design:type", String)
], CommunicationProviderMessageType.prototype, "Status", void 0);
__decorate([
    Field(),
    __metadata("design:type", String)
], CommunicationProviderMessageType.prototype, "AdditionalAttributes", void 0);
__decorate([
    Field(),
    __metadata("design:type", Date)
], CommunicationProviderMessageType.prototype, "_mj_CreatedAt", void 0);
__decorate([
    Field(),
    __metadata("design:type", Date)
], CommunicationProviderMessageType.prototype, "_mj_UpdatedAt", void 0);
__decorate([
    Field(),
    __metadata("design:type", String)
], CommunicationProviderMessageType.prototype, "CommunicationProvider", void 0);
__decorate([
    Field(),
    __metadata("design:type", String)
], CommunicationProviderMessageType.prototype, "CommunicationBaseMessageType", void 0);
CommunicationProviderMessageType = __decorate([
    InputType()
], CommunicationProviderMessageType);
export { CommunicationProviderMessageType };
let TemplateInputType = class TemplateInputType {
    ID;
    Name;
    Description;
    UserPrompt;
    CategoryID;
    UserID;
    ActiveAt;
    DisabledAt;
    IsActive;
    _mj_CreatedAt;
    _mj_UpdatedAt;
    Category;
    User;
};
__decorate([
    Field(),
    __metadata("design:type", Number)
], TemplateInputType.prototype, "ID", void 0);
__decorate([
    Field(),
    __metadata("design:type", String)
], TemplateInputType.prototype, "Name", void 0);
__decorate([
    Field(),
    __metadata("design:type", String)
], TemplateInputType.prototype, "Description", void 0);
__decorate([
    Field({ nullable: true }),
    __metadata("design:type", String)
], TemplateInputType.prototype, "UserPrompt", void 0);
__decorate([
    Field({ nullable: true }),
    __metadata("design:type", Number)
], TemplateInputType.prototype, "CategoryID", void 0);
__decorate([
    Field(),
    __metadata("design:type", Number)
], TemplateInputType.prototype, "UserID", void 0);
__decorate([
    Field({ nullable: true }),
    __metadata("design:type", Date)
], TemplateInputType.prototype, "ActiveAt", void 0);
__decorate([
    Field({ nullable: true }),
    __metadata("design:type", Date)
], TemplateInputType.prototype, "DisabledAt", void 0);
__decorate([
    Field(),
    __metadata("design:type", Boolean)
], TemplateInputType.prototype, "IsActive", void 0);
__decorate([
    Field(),
    __metadata("design:type", Date)
], TemplateInputType.prototype, "_mj_CreatedAt", void 0);
__decorate([
    Field(),
    __metadata("design:type", Date)
], TemplateInputType.prototype, "_mj_UpdatedAt", void 0);
__decorate([
    Field({ nullable: true }),
    __metadata("design:type", String)
], TemplateInputType.prototype, "Category", void 0);
__decorate([
    Field({ nullable: true }),
    __metadata("design:type", String)
], TemplateInputType.prototype, "User", void 0);
TemplateInputType = __decorate([
    InputType()
], TemplateInputType);
export { TemplateInputType };
let CommunicationMessageInput = class CommunicationMessageInput {
    MessageType;
    From;
    To;
    Body;
    BodyTemplate;
    HTMLBody;
    HTMLBodyTemplate;
    Subject;
    SubjectTemplate;
    ContextData;
};
__decorate([
    Field(() => CommunicationProviderMessageType),
    __metadata("design:type", CommunicationProviderMessageType)
], CommunicationMessageInput.prototype, "MessageType", void 0);
__decorate([
    Field(),
    __metadata("design:type", String)
], CommunicationMessageInput.prototype, "From", void 0);
__decorate([
    Field(),
    __metadata("design:type", String)
], CommunicationMessageInput.prototype, "To", void 0);
__decorate([
    Field({ nullable: true }),
    __metadata("design:type", String)
], CommunicationMessageInput.prototype, "Body", void 0);
__decorate([
    Field(() => TemplateInputType, { nullable: true }),
    __metadata("design:type", TemplateInputType)
], CommunicationMessageInput.prototype, "BodyTemplate", void 0);
__decorate([
    Field({ nullable: true }),
    __metadata("design:type", String)
], CommunicationMessageInput.prototype, "HTMLBody", void 0);
__decorate([
    Field(() => TemplateInputType, { nullable: true }),
    __metadata("design:type", TemplateInputType)
], CommunicationMessageInput.prototype, "HTMLBodyTemplate", void 0);
__decorate([
    Field({ nullable: true }),
    __metadata("design:type", String)
], CommunicationMessageInput.prototype, "Subject", void 0);
__decorate([
    Field(() => TemplateInputType, { nullable: true }),
    __metadata("design:type", TemplateInputType)
], CommunicationMessageInput.prototype, "SubjectTemplate", void 0);
__decorate([
    Field(() => GraphQLJSONObject, { nullable: true }),
    __metadata("design:type", Object)
], CommunicationMessageInput.prototype, "ContextData", void 0);
CommunicationMessageInput = __decorate([
    InputType()
], CommunicationMessageInput);
export { CommunicationMessageInput };
let RunEntityCommunicationResultType = class RunEntityCommunicationResultType {
    Success;
    ErrorMessage;
    Results;
};
__decorate([
    Field(),
    __metadata("design:type", Boolean)
], RunEntityCommunicationResultType.prototype, "Success", void 0);
__decorate([
    Field({ nullable: true }),
    __metadata("design:type", String)
], RunEntityCommunicationResultType.prototype, "ErrorMessage", void 0);
__decorate([
    Field(() => GraphQLJSONObject, { nullable: true }),
    __metadata("design:type", Object)
], RunEntityCommunicationResultType.prototype, "Results", void 0);
RunEntityCommunicationResultType = __decorate([
    ObjectType()
], RunEntityCommunicationResultType);
export { RunEntityCommunicationResultType };
let ReportResolver = class ReportResolver {
    async RunEntityCommunicationByViewID(entityID, runViewByIDInput, providerName, providerMessageTypeName, message, previewOnly, includeProcessedMessages, { userPayload }) {
        try {
            await EntityCommunicationsEngine.Instance.Config(false, userPayload.userRecord);
            const newMessage = new Message(message);
            await TemplateEngineServer.Instance.Config(false, userPayload.userRecord);
            if (newMessage.BodyTemplate) {
                newMessage.BodyTemplate = TemplateEngineServer.Instance.FindTemplate(newMessage.BodyTemplate.Name);
            }
            if (newMessage.HTMLBodyTemplate) {
                newMessage.HTMLBodyTemplate = TemplateEngineServer.Instance.FindTemplate(newMessage.HTMLBodyTemplate.Name);
            }
            if (newMessage.SubjectTemplate) {
                newMessage.SubjectTemplate = TemplateEngineServer.Instance.FindTemplate(newMessage.SubjectTemplate.Name);
            }
            const params = {
                EntityID: entityID,
                RunViewParams: runViewByIDInput,
                ProviderName: providerName,
                ProviderMessageTypeName: providerMessageTypeName,
                Message: newMessage,
                PreviewOnly: previewOnly,
                IncludeProcessedMessages: includeProcessedMessages,
            };
            const result = await EntityCommunicationsEngine.Instance.RunEntityCommunication(params);
            return {
                Success: result.Success,
                ErrorMessage: result.ErrorMessage,
                Results: includeProcessedMessages && result.Results ? { Results: result.Results } : undefined,
            };
        }
        catch (e) {
            const { message } = z
                .object({ message: z.string() })
                .catch({ message: JSON.stringify(e) })
                .parse(e);
            return {
                Success: false,
                ErrorMessage: message,
            };
        }
    }
};
__decorate([
    Query(() => RunEntityCommunicationResultType),
    __param(0, Arg('entityID', () => String)),
    __param(1, Arg('runViewByIDInput', () => RunViewByIDInput)),
    __param(2, Arg('providerName', () => String)),
    __param(3, Arg('providerMessageTypeName', () => String)),
    __param(4, Arg('message', () => CommunicationMessageInput)),
    __param(5, Arg('previewOnly', () => Boolean)),
    __param(6, Arg('includeProcessedMessages', () => Boolean)),
    __param(7, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, RunViewByIDInput, String, String, CommunicationMessageInput, Boolean, Boolean, Object]),
    __metadata("design:returntype", Promise)
], ReportResolver.prototype, "RunEntityCommunicationByViewID", null);
ReportResolver = __decorate([
    Resolver(RunEntityCommunicationResultType)
], ReportResolver);
export { ReportResolver };
//# sourceMappingURL=EntityCommunicationsResolver.js.map