"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateActionFields = exports.FieldsValidationSchema = exports.fields = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const lodash_1 = require("lodash");
const zod_1 = require("zod");
exports.fields = {
    signerRole: {
        id: 'signerRole',
        label: 'Signer role',
        description: "Must match an existing role in chosen template. It's case-sensitive.",
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    signerName: {
        id: 'signerName',
        label: 'Signer name',
        description: 'The name of the signer.',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    signerEmailAddress: {
        id: 'signerEmailAddress',
        label: 'Signer email address',
        description: 'The email address of the signer.',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    templateId: {
        id: 'templateId',
        label: 'Template ID',
        description: 'Use the template id to create a SignatureRequest from a template.',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    title: {
        id: 'title',
        label: 'Title',
        description: 'The title you want to assign to the SignatureRequest.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    subject: {
        id: 'subject',
        label: 'Subject',
        description: 'The subject in the email that will be sent to the signer.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    message: {
        id: 'message',
        label: 'Message',
        description: 'The custom message in the email that will be sent to the signer.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    customFields: {
        id: 'customFields',
        label: 'Custom fields',
        description: 'An array defining values and options for custom fields.',
        type: extensions_core_1.FieldType.JSON,
        required: false,
    },
};
exports.FieldsValidationSchema = zod_1.z.object({
    signerRole: zod_1.z.string(),
    signerName: zod_1.z.string(),
    signerEmailAddress: zod_1.z.string().email(),
    templateId: zod_1.z.string(),
    title: zod_1.z.optional(zod_1.z.string()),
    subject: zod_1.z.optional(zod_1.z.string()),
    message: zod_1.z.optional(zod_1.z.string()),
    customFields: zod_1.z
        .optional(zod_1.z.string())
        .transform((str, ctx) => {
        if ((0, lodash_1.isNil)(str) || (0, lodash_1.isEmpty)(str))
            return [];
        try {
            const parsedJson = JSON.parse(str);
            if ((0, lodash_1.isEmpty)(parsedJson)) {
                return [];
            }
            if (!Array.isArray(parsedJson)) {
                ctx.addIssue({
                    code: 'custom',
                    message: 'custom fields should be an array',
                });
                return zod_1.z.NEVER;
            }
            const allObjectsHaveKeys = parsedJson.every((obj) => {
                if (typeof obj !== 'object') {
                    ctx.addIssue({
                        code: 'custom',
                        message: 'Object entries in custom fields array should be an object',
                    });
                    return zod_1.z.NEVER;
                }
                return 'name' in obj && 'value' in obj;
            });
            if (!allObjectsHaveKeys) {
                ctx.addIssue({
                    code: 'custom',
                    message: 'Every object in custom fields array should have a `name` and `value` field',
                });
                return zod_1.z.NEVER;
            }
            return parsedJson;
        }
        catch (e) {
            ctx.addIssue({ code: 'custom', message: 'Invalid custom fields data' });
            return zod_1.z.NEVER;
        }
    }),
});
const validateActionFields = (fields) => {
    const parsedData = exports.FieldsValidationSchema.parse(fields);
    return parsedData;
};
exports.validateActionFields = validateActionFields;
//# sourceMappingURL=fields.js.map