"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateBooking = 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.updateBooking = {
    key: 'updateBooking',
    title: 'Update booking (v1)',
    description: 'Updates Booking',
    category: extensions_core_1.Category.SCHEDULING,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { settings: { apiKey }, fields: { bookingId, title, description, status, start, end }, } = (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);
            const booking = await calComApi.updateBooking(bookingId, {
                title,
                description,
                status,
                start,
                end,
            });
            await onComplete({
                data_points: {
                    bookingId: String(booking.id),
                    bookingUid: booking.uid,
                },
            });
        }
        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=updateBooking.js.map