"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldsValidationSchema = exports.fields = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const zod_1 = require("zod");
const lodash_1 = require("lodash");
exports.fields = {
    careflowId: {
        id: 'careflowId',
        label: 'Care flow ID',
        description: 'The ID of the care flow to update the baseline info for. When not provided, the baseline info will be updated for the current care flow.',
        type: extensions_core_1.FieldType.STRING,
        required: false,
    },
    baselineInfo: {
        id: 'baselineInfo',
        label: 'Baseline info',
        description: `Use baseline data points often used to start a care flow. Example: 
      [
        {
          "data_point_definition_id": "age",
          "value": "29"
        },
        {
          "data_point_definition_id": "dob",
          "value": "1993-11-30"
        }
      ]`,
        type: extensions_core_1.FieldType.JSON,
        required: false,
    },
};
exports.FieldsValidationSchema = zod_1.z.object({
    careflowId: zod_1.z
        .string()
        .trim()
        .optional()
        .transform((str) => {
        const trimmed = (0, lodash_1.isNil)(str) ? str : str.trim();
        if ((0, lodash_1.isNil)(trimmed) || (0, lodash_1.isEmpty)(trimmed)) {
            return undefined;
        }
        return trimmed;
    }),
    baselineInfo: zod_1.z
        .optional(zod_1.z.string())
        .transform((str, ctx) => {
        try {
            if ((0, lodash_1.isNil)(str) || (0, lodash_1.isEmpty)(str)) {
                ctx.addIssue({
                    code: 'custom',
                    message: `Baseline info cannot be empty. It must be an array of data_point_definition_id and value pairs.`,
                });
                return zod_1.z.NEVER;
            }
            const parsedJson = JSON.parse(str);
            if ((0, lodash_1.isEmpty)(parsedJson)) {
                ctx.addIssue({
                    code: 'custom',
                    message: `Baseline info should be an array. Your parsed baselineInfo field is currently empty.`,
                });
                return zod_1.z.NEVER;
            }
            if (!Array.isArray(parsedJson)) {
                ctx.addIssue({
                    code: 'custom',
                    message: `Baseline info should be an array, it's currently a ${typeof parsedJson}.`,
                });
                return zod_1.z.NEVER;
            }
            const allObjectsHaveKeys = parsedJson.every((obj) => {
                if (typeof obj !== 'object') {
                    ctx.addIssue({
                        code: 'custom',
                        message: `Item "${String(obj)}" in baseline info array is not an object.`,
                    });
                    return zod_1.z.NEVER;
                }
                return 'data_point_definition_id' in obj && 'value' in obj;
            });
            const allObjectValuesAreStrings = parsedJson.every((obj) => {
                return Object.values(obj).every((v) => typeof v === 'string');
            });
            if (!allObjectsHaveKeys) {
                ctx.addIssue({
                    code: 'custom',
                    message: 'Every object in the baseline info array should (only) have a `data_point_definition_id` and `value` field.',
                });
                return zod_1.z.NEVER;
            }
            if (!allObjectValuesAreStrings) {
                ctx.addIssue({
                    code: 'custom',
                    message: 'Not all baseline info values are strings. Given data point values are polymorphic, the value for a data point should always be sent as a string.',
                });
                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