"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldsValidationSchema = exports.fields = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const lodash_1 = require("lodash");
const zod_1 = __importDefault(require("zod"));
const IdentifierOptionsSchema = zod_1.default.enum(['id', 'email', 'cio_id']);
exports.fields = {
    personIdentifierType: {
        id: 'personIdentifierType',
        label: 'Person identifier type',
        description: 'The identifier type used to identify the person. Defaults to email.',
        type: extensions_core_1.FieldType.STRING,
        required: true,
        options: {
            dropdownOptions: Object.values(IdentifierOptionsSchema.options).map((identifier) => ({
                label: identifier,
                value: identifier,
            })),
        },
    },
    identifierValue: {
        id: 'identifierValue',
        label: 'Identifier value',
        description: 'The value of the identifier used to identify the person.',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    eventName: {
        id: 'eventName',
        label: 'Event name',
        description: "The name of the event. This is how you'll find your event in Customer.io or select it when using events as campaign triggers.",
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    attributes: {
        id: 'attributes',
        label: 'Attributes',
        description: 'Additional information that you might want to reference in a message using liquid or use to set attributes on the identified person.',
        type: extensions_core_1.FieldType.JSON,
        required: false,
    },
};
exports.FieldsValidationSchema = zod_1.default.object({
    personIdentifierType: IdentifierOptionsSchema.default('email'),
    identifierValue: zod_1.default.string().min(1),
    eventName: zod_1.default.string().min(1),
    attributes: zod_1.default
        .string()
        .optional()
        .transform((str, ctx) => {
        if ((0, lodash_1.isNil)(str) || (0, lodash_1.isEmpty)(str))
            return undefined;
        try {
            const parsedJson = JSON.parse(str);
            if ((0, lodash_1.isEmpty)(parsedJson)) {
                return undefined;
            }
            if (typeof parsedJson !== 'object' || Array.isArray(parsedJson)) {
                ctx.addIssue({
                    code: 'custom',
                    message: 'Attributes should be an object',
                });
                return zod_1.default.NEVER;
            }
            return parsedJson;
        }
        catch (e) {
            ctx.addIssue({ code: 'custom', message: 'Invalid JSON.' });
            return zod_1.default.NEVER;
        }
    }),
});
//# sourceMappingURL=fields.js.map