"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 atoms_1 = require("../../../lib/api/schema/atoms");
const zod_1 = __importDefault(require("zod"));
const lodash_1 = require("lodash");
exports.fields = {
    ticketId: {
        id: 'ticketId',
        label: 'Ticket ID',
        description: 'The ID of the ticket to update',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    subject: {
        id: 'subject',
        label: 'Subject',
        description: 'Subject of the ticket',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    type: {
        id: 'type',
        label: 'Type',
        description: 'Helps categorize the ticket according to the different kinds of issues your support team deals with',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    status: {
        id: 'status',
        label: 'Status',
        description: `The status of the ticket. Possible values: ${atoms_1.TicketStatusDescriptionString}`,
        type: extensions_core_1.FieldType.NUMERIC,
        required: false,
        options: {
            dropdownOptions: Object.entries(atoms_1.TicketStatus).map(([key, value]) => ({
                label: key,
                value,
            })),
        },
    },
    priority: {
        id: 'priority',
        label: 'Priority',
        description: `The priority of the ticket. Possible values: ${atoms_1.TicketPriorityDescriptionString}`,
        type: extensions_core_1.FieldType.NUMERIC,
        required: false,
        options: {
            dropdownOptions: Object.entries(atoms_1.TicketPriority).map(([key, value]) => ({
                label: key,
                value,
            })),
        },
    },
    description: {
        id: 'description',
        label: 'Description',
        description: 'Content of the ticket',
        type: extensions_core_1.FieldType.HTML,
        required: false,
    },
    customFields: {
        id: 'customFields',
        label: 'Custom fields',
        description: 'Key value pairs containing the names and values of custom fields. All values must be strings.',
        type: extensions_core_1.FieldType.JSON,
        required: false,
    },
    dueBy: {
        id: 'dueBy',
        label: 'Due by',
        description: 'Timestamp that denotes when the ticket is due to be resolved',
        type: extensions_core_1.FieldType.DATE,
        required: false,
    },
    tags: {
        id: 'tags',
        label: 'Tags',
        description: 'Comma separated list of tags to associate with the ticket.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
};
const NonEmptyStringSchema = zod_1.default
    .string()
    .transform((val) => (val === '' ? undefined : val));
exports.FieldsValidationSchema = zod_1.default.object({
    ticketId: zod_1.default.string().min(1),
    subject: NonEmptyStringSchema.optional(),
    type: NonEmptyStringSchema.optional(),
    status: atoms_1.TicketStatusSchema.optional(),
    priority: atoms_1.TicketPrioritySchema.optional(),
    description: NonEmptyStringSchema.optional(),
    dueBy: NonEmptyStringSchema.optional(),
    tags: NonEmptyStringSchema.optional().transform((val) => {
        if ((0, lodash_1.isNil)(val) || (0, lodash_1.isEmpty)(val))
            return undefined;
        return val
            .trim()
            .split(',')
            .map((tag) => tag.trim());
    }),
    customFields: 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);
            const allValuesAreStrings = Object.values(parsedJson).every((value) => typeof value === 'string');
            if (!allValuesAreStrings) {
                ctx.addIssue({
                    code: 'custom',
                    message: 'All values for custom fields must be strings',
                });
                return zod_1.default.NEVER;
            }
            return parsedJson;
        }
        catch (e) {
            ctx.addIssue({
                code: 'custom',
                message: 'Not a valid JSON object',
            });
            return zod_1.default.NEVER;
        }
    }),
});
//# sourceMappingURL=fields.js.map