"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldsValidationSchema = exports.fields = void 0;
const zod_1 = require("zod");
const extensions_core_1 = require("@awell-health/extensions-core");
const lodash_1 = require("lodash");
const awell_1 = require("../../../../../../src/lib/awell");
exports.fields = {
    listIds: {
        id: 'listIds',
        label: 'List IDs',
        description: 'A comma-separated string of list IDs the contact will be added to.',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    email: {
        id: 'email',
        label: 'Email',
        description: "The contact's primary email.",
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    firstName: {
        id: 'firstName',
        label: 'First Name',
        description: "The contact's first name.",
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    lastName: {
        id: 'lastName',
        label: 'Last Name',
        description: "The contact's last name.",
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    customFields: {
        id: 'customFields',
        label: 'Custom fields',
        description: 'An object of custom field IDs and the values you want to associate with those custom fields.',
        type: extensions_core_1.FieldType.JSON,
        required: false,
    },
};
exports.FieldsValidationSchema = zod_1.z.object({
    listIds: zod_1.z
        .string()
        // Make sure all white spaces are stripped
        .transform((chars) => chars.replace(/\s/g, ''))
        .transform((chars) => chars.split(','))
        // Make sure there are no undefined or empty characteristics
        .transform((charsArray) => charsArray.filter((chars) => {
        if ((0, lodash_1.isNil)(chars) || (0, lodash_1.isEmpty)(chars))
            return false;
        return true;
    })),
    email: (0, awell_1.getEmailValidation)(),
    // max 50 chars - API limit
    firstName: zod_1.z.string().max(50),
    lastName: zod_1.z.string().max(50),
    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 (typeof parsedJson !== 'object' || Array.isArray(parsedJson)) {
                ctx.addIssue({
                    code: 'custom',
                    message: 'customFields should be an object',
                });
                return zod_1.z.NEVER;
            }
            return parsedJson;
        }
        catch (e) {
            ctx.addIssue({ code: 'custom', message: 'Invalid JSON' });
            return zod_1.z.NEVER;
        }
    }),
});
//# sourceMappingURL=fields.js.map