"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBooking = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const zod_1 = require("zod");
const zod_validation_error_1 = require("zod-validation-error");
const calComApi_1 = __importDefault(require("../../../lib/api/v1/calComApi"));
const settings_1 = require("../../../settings");
const config_1 = require("./config");
exports.getBooking = {
    key: 'getBooking',
    title: 'Get booking (v1)',
    description: 'Get Booking and save data in Data Points',
    category: extensions_core_1.Category.SCHEDULING,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a, _b, _c;
        try {
            const { fields: { bookingId }, settings: { apiKey }, } = (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.getBooking(bookingId);
            await onComplete({
                data_points: {
                    eventTypeId: `${booking.eventTypeId}`,
                    title: booking.title,
                    description: booking.description,
                    startTime: booking.startTime,
                    endTime: booking.endTime,
                    status: booking.status,
                    cancelUrl: `https://app.cal.com/booking/${booking.uid}?cancel=true`,
                    rescheduleUrl: `https://app.cal.com/reschedule/${booking.uid}`,
                    videoCallUrl: (_a = booking.metadata.videoCallUrl) !== null && _a !== void 0 ? _a : '',
                    firstAttendeeEmail: booking.attendees[0].email,
                    firstAttendeeTimezone: booking.attendees[0].timeZone,
                    firstAttendeeName: booking.attendees[0].name,
                    userEmail: booking.user.email,
                    location: (_c = (_b = booking.responses) === null || _b === void 0 ? void 0 : _b.location) === null || _c === void 0 ? void 0 : _c.value,
                },
            });
        }
        catch (error) {
            if (error instanceof zod_1.ZodError) {
                const err = (0, zod_validation_error_1.fromZodError)(error);
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: err.message },
                            error: {
                                category: 'BAD_REQUEST',
                                message: err.message,
                            },
                        },
                    ],
                });
                return;
            }
            const errorMessage = error.message;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: {
                            en: `Get Booking failed: ${errorMessage}`,
                        },
                        error: {
                            category: 'SERVER_ERROR',
                            message: `Get Booking failed: ${errorMessage}`,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=getBooking.js.map