"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteBooking = void 0;
const zod_1 = require("zod");
const zod_validation_error_1 = require("zod-validation-error");
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const calComApi_1 = __importDefault(require("../../lib/api/v1/calComApi"));
const settings_1 = require("../../settings");
exports.deleteBooking = {
    key: 'deleteBooking',
    title: 'Delete booking (v1)',
    description: 'Deletes Booking in Cal.com',
    category: extensions_core_1.Category.SCHEDULING,
    fields: config_1.fields,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { settings: { apiKey }, fields: { bookingId, allRemainingBookings, reason }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    settings: settings_1.SettingsValidationSchema,
                    fields: config_1.FieldsValidationSchema,
                }),
                payload,
            });
            const calComApi = new calComApi_1.default(apiKey);
            await calComApi.deleteBooking(bookingId, {
                allRemainingBookings,
                cancellationReason: reason,
            });
            await onComplete();
        }
        catch (err) {
            if (err instanceof zod_1.ZodError) {
                const error = (0, zod_validation_error_1.fromZodError)(err);
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: error.message },
                            error: {
                                category: 'BAD_REQUEST',
                                message: error.message,
                            },
                        },
                    ],
                });
                return;
            }
            const errorMessage = err.message;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: {
                            en: `Get Booking failed: ${errorMessage}`,
                        },
                        error: {
                            category: 'SERVER_ERROR',
                            message: `Get Booking failed: ${errorMessage}`,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=deleteBooking.js.map