"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteTicket = 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");
const error_1 = require("../../client/error");
exports.deleteTicket = {
    key: 'deleteTicket',
    title: 'Delete ticket',
    description: 'Deletes a support ticket in Zendesk',
    category: extensions_core_1.Category.CUSTOMER_SUPPORT,
    fields: config_1.fields,
    previewable: false,
    onEvent: async ({ payload, onComplete, onError }) => {
        try {
            const { settings, fields: { ticketId }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    settings: settings_1.SettingsValidationSchema,
                    fields: config_1.FieldsValidationSchema,
                }),
                payload,
            });
            const client = (0, client_1.makeAPIClient)(settings);
            await client.deleteTicket(String(ticketId));
            await onComplete();
        }
        catch (err) {
            if ((0, error_1.isZendeskApiError)(err)) {
                const events = (0, error_1.zendeskApiErrorToActivityEvent)(err);
                await onError({ events });
            }
            else {
                const errorMessage = err instanceof Error ? err.message : 'Unknown error occurred';
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: `Failed to delete ticket: ${errorMessage}` },
                            error: {
                                category: 'SERVER_ERROR',
                                message: errorMessage,
                            },
                        },
                    ],
                });
            }
        }
    },
};
//# sourceMappingURL=deleteTicket.js.map