"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"));
exports.fields = {
    flowId: {
        id: 'flowId',
        label: 'Flow ID',
        description: 'The ID of the flow you would like to trigger',
        type: extensions_core_1.FieldType.STRING,
        required: true,
    },
    autoComplete: {
        id: 'autoComplete',
        label: 'Autocomplete',
        description: 'If true, the activity will not wait for the flow to complete (default: false)',
        type: extensions_core_1.FieldType.BOOLEAN,
        required: false,
    },
    data: {
        id: 'data',
        label: 'Data',
        description: 'Data to be ingested when triggering the flow, including the `awell_activity_id` so the flow can perform the appropriate completeExtensionActivity callback.',
        type: extensions_core_1.FieldType.JSON,
        required: true,
    },
};
exports.FieldsValidationSchema = zod_1.default.object({
    flowId: zod_1.default.string().nonempty(),
    autoComplete: zod_1.default.boolean().optional().default(false),
    data: zod_1.default.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: 'Data should be an object',
                });
                return zod_1.default.NEVER;
            }
            for (const key in parsedJson) {
                if (typeof parsedJson[key] !== 'string') {
                    ctx.addIssue({
                        code: 'custom',
                        message: `Value for key "${key}" must be a string`,
                    });
                    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