"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBookingv2 = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const validatePayloadAndCreateSdk_1 = require("../../../lib/api/validatePayloadAndCreateSdk");
const lodash_1 = require("lodash");
const addEventLog_1 = require("../../../../../src/lib/awell/addEventLog");
exports.getBookingv2 = {
    key: 'getBookingv2',
    title: 'Get booking (v2)',
    description: 'Retrieve booking details from Cal.com using the v2 api',
    category: extensions_core_1.Category.SCHEDULING,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { fields: { bookingUid }, calv2Client, } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const response = await calv2Client.getBooking({
            bookingUid,
        });
        if (response.data.status === 'error') {
            await onError({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: !(0, lodash_1.isNil)(response.data.error)
                            ? JSON.stringify(response.data.error)
                            : 'Unknown error',
                    }),
                ],
            });
            return;
        }
        const bookingData = response.data.data;
        await onComplete({
            data_points: {
                bookingData: JSON.stringify(bookingData),
                eventTypeId: String(bookingData.eventType.id),
                title: bookingData.title,
                description: bookingData.description,
                start: bookingData.start,
                end: bookingData.end,
                status: bookingData.status,
                cancelUrl: `https://app.cal.com/booking/${bookingData.uid}?cancel=true`,
                rescheduleUrl: `https://app.cal.com/reschedule/${bookingData.uid}`,
                firstAttendeeEmail: bookingData.attendees[0].email,
                firstAttendeeTimezone: bookingData.attendees[0].timeZone,
                firstAttendeeName: bookingData.attendees[0].name,
                location: bookingData.location,
                bookingFieldsResponses: (0, lodash_1.isNil)(bookingData === null || bookingData === void 0 ? void 0 : bookingData.bookingFieldsResponses)
                    ? undefined
                    : JSON.stringify(bookingData.bookingFieldsResponses),
                metadata: (0, lodash_1.isNil)(bookingData === null || bookingData === void 0 ? void 0 : bookingData.metadata)
                    ? undefined
                    : JSON.stringify(bookingData.metadata),
            },
        });
    },
};
//# sourceMappingURL=getBooking.js.map