"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.post = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const zod_1 = require("zod");
const errors_1 = require("../../lib/errors");
const lodash_1 = require("lodash");
exports.post = {
    key: 'post',
    title: 'POST request',
    description: 'Make a POST request',
    category: extensions_core_1.Category.DATA,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b;
        const { fields: { endpoint, headers, jsonPayload, additionalPayload }, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({
                fields: config_1.FieldsValidationSchema,
            }),
            payload,
        });
        try {
            const response = await fetch(endpoint, {
                method: 'POST',
                headers: {
                    ...headers,
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    ...jsonPayload,
                    ...additionalPayload,
                }),
            });
            if (response.ok) {
                const getResponseBody = async (response) => {
                    const res = await response.text();
                    try {
                        const jsonRes = JSON.parse(res);
                        return jsonRes;
                    }
                    catch (e) {
                        return (0, lodash_1.isEmpty)(res) ? 'No content' : res;
                    }
                };
                const responseBody = await getResponseBody(response);
                const statusCode = response.status;
                await onComplete({
                    data_points: {
                        response: typeof responseBody === 'string'
                            ? responseBody
                            : JSON.stringify(responseBody),
                        statusCode: String(statusCode),
                    },
                });
            }
            else {
                throw new errors_1.FetchError(response.status, response.statusText, response.body);
            }
        }
        catch (error) {
            if (error instanceof errors_1.FetchError) {
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: {
                                en: `${error.message} ${error.responseBody}`,
                            },
                            error: {
                                category: 'SERVER_ERROR',
                                message: `${error.message} ${error.responseBody}`,
                            },
                        },
                    ],
                });
            }
            else {
                const parsedError = error;
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: {
                                en: (_a = parsedError.message) !== null && _a !== void 0 ? _a : 'Unexpected error',
                            },
                            error: {
                                category: 'SERVER_ERROR',
                                message: (_b = parsedError.message) !== null && _b !== void 0 ? _b : 'Unexpected error',
                            },
                        },
                    ],
                });
            }
        }
    },
};
//# sourceMappingURL=post.js.map