"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importDefault(require("react"));
const embed_react_1 = __importStar(require("@calcom/embed-react"));
const CalDotComScheduling = ({ calLink, hideEventTypeDetails = false, onBookingSuccessful, metadata, }) => {
    const eventListenerRef = react_1.default.useRef(false);
    const calApiRef = react_1.default.useRef(null);
    const bookingSuccessfulCallback = (e) => {
        const { data } = e.detail;
        const { confirmed, eventType, date, booking } = data;
        onBookingSuccessful({ confirmed, eventType, date, booking });
    };
    const initComponent = async () => {
        const cal = await (0, embed_react_1.getCalApi)();
        calApiRef.current = cal;
        if (cal != null && !eventListenerRef.current) {
            cal('ui', {
                hideEventTypeDetails,
            });
            cal('on', {
                action: 'bookingSuccessful',
                callback: bookingSuccessfulCallback,
            });
            eventListenerRef.current = true;
        }
    };
    react_1.default.useEffect(() => {
        void initComponent();
        return () => {
            const cleanup = async () => {
                // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
                const cal = calApiRef == null ? (await (0, embed_react_1.getCalApi)()) : calApiRef.current;
                if (cal != null && eventListenerRef.current) {
                    cal('off', {
                        action: 'bookingSuccessful',
                        callback: bookingSuccessfulCallback,
                    });
                    eventListenerRef.current = false;
                }
            };
            void cleanup();
        };
    }, [hideEventTypeDetails]);
    let metadataString = '';
    if (metadata != null && Object.keys(metadata).length > 0) {
        metadataString = Object.entries(metadata)
            .map(([key, value]) => `metadata[${key}]=${value}`)
            .join('&');
    }
    const metadataSeparator = calLink.includes('?') ? '&' : '?';
    const composedCalLink = `${calLink}${metadataString !== '' ? `${metadataSeparator}${metadataString}` : ''}`;
    return ((0, jsx_runtime_1.jsx)("div", { style: { width: '100%', height: '600px' }, children: (0, jsx_runtime_1.jsx)(embed_react_1.default, { calLink: composedCalLink, style: { width: '100%', height: '100%', overflow: 'hidden' } }) }));
};
const CalDotComBookAppointmentComponent = ({ activityDetails, onSubmit, }) => {
    var _a, _b;
    console.log('activityDetails', activityDetails);
    const calLink = (_b = (_a = activityDetails.fields.find(field => field.id === 'calLink')) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : '';
    const handleBookingSuccessful = ({ confirmed, date, eventType, booking, }) => {
        console.log("bookingSuccessful", confirmed, date, eventType, booking);
        void onSubmit(activityDetails.activity_id, [
            { key: 'booking_confirmed', value: String(confirmed !== null && confirmed !== void 0 ? confirmed : false) },
            { key: 'booking_date', value: String(date !== null && date !== void 0 ? date : '') },
            { key: 'event_type', value: JSON.stringify(eventType !== null && eventType !== void 0 ? eventType : {}) },
            { key: 'booking_details', value: JSON.stringify(booking !== null && booking !== void 0 ? booking : {}) },
        ]);
    };
    return ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(CalDotComScheduling, { calLink: calLink, onBookingSuccessful: handleBookingSuccessful, hideEventTypeDetails: false }) }));
};
exports.default = CalDotComBookAppointmentComponent;
//# sourceMappingURL=index.js.map