"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTask = 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.createTask = {
    key: 'createTask',
    title: 'Create task',
    description: 'Creates a new task in Zendesk',
    category: extensions_core_1.Category.CUSTOMER_SUPPORT,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { settings: { salesApiToken }, fields: { content, dueDate, ownerId, resourceType, resourceId, completed, remindAt, }, } = (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,
            });
            const res = await client.salesApi.createTask({
                content,
                due_date: dueDate,
                owner_id: ownerId,
                resource_type: resourceType,
                resource_id: resourceId,
                completed,
                remind_at: remindAt,
            });
            await onComplete({ data_points: { taskId: String(res.data.data.id) } });
        }
        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=createTask.js.map