"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.completeTask = void 0;
const zod_1 = require("zod");
const extensions_core_1 = require("@awell-health/extensions-core");
const settings_1 = require("../../../settings");
const config_1 = require("./config");
const client_1 = require("../../client");
exports.completeTask = {
    key: 'completeTask',
    title: 'Complete a task',
    description: 'Completes a task in Zendesk',
    category: extensions_core_1.Category.CUSTOMER_SUPPORT,
    fields: config_1.fields,
    previewable: false,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { settings: { salesApiToken }, fields: { taskId }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    settings: settings_1.SettingsValidationSchema,
                    fields: config_1.FieldsValidationSchema,
                }),
                payload,
            });
            const client = new client_1.ZendeskClient({
                salesApiToken,
            });
            await client.salesApi.updateTask(taskId, {
                completed: true,
            });
            await onComplete();
        }
        catch (err) {
            if ((0, client_1.isSalesApiError)(err)) {
                const events = (0, client_1.salesApiErrorToActivityEvent)(err);
                await onError({ events });
            }
            else {
                // re-throw to be handled in extensions server
                throw err;
            }
        }
    },
};
//# sourceMappingURL=completeTask.js.map