"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommaSeparatedEmailsValidationSchema = exports.getEmailValidation = void 0;
const lodash_1 = require("lodash");
const zod_1 = require("zod");
/**
 * Get the zod validation schema for email addresses
 * We need the local part to allows special characters like accents (é) as per modern email standards (UTF-8 encoding).
 */
const getEmailValidation = () => {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return zod_1.z.string().regex(emailRegex, { message: 'Invalid email address' });
};
exports.getEmailValidation = getEmailValidation;
/**
 * Zod validation schema for multiple email addresses
 * collected via a comma separated string
 */
exports.CommaSeparatedEmailsValidationSchema = zod_1.z
    .string()
    .optional()
    /**
     * Transform the value to an array of email addresses
     * and trim each email address
     */
    .transform((value) => {
    if ((0, lodash_1.isEmpty)(value) || value === undefined)
        return [];
    const emails = value.split(',');
    return emails
        .map((email) => {
        const trimmedEmail = email.trim();
        if ((0, lodash_1.isEmpty)(trimmedEmail))
            return undefined;
        return trimmedEmail;
    })
        .filter((email) => email !== undefined);
})
    /**
     * Validate each email address
     */
    .superRefine((emails, ctx) => {
    const emailValidation = (0, exports.getEmailValidation)();
    emails.forEach((email) => {
        if (!emailValidation.safeParse(email).success) {
            ctx.addIssue({
                code: 'custom',
                message: `Invalid email address: ${email}`,
            });
        }
    });
});
//# sourceMappingURL=getEmailValidation.js.map