"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const extensions_core_1 = require("@awell-health/extensions-core");
const lodash_1 = require("lodash");
const node_fetch_1 = __importDefault(require("node-fetch"));
const schema_1 = require("./schema");
class CalComApi {
    constructor(apiKey) {
        this.baseUrl = 'https://api.cal.com/v1';
        this.apiKey = apiKey;
    }
    constructUrl(url, params) {
        const nonEmptyParams = (0, lodash_1.omitBy)(params, lodash_1.isNil);
        const queryParams = new URLSearchParams({
            apiKey: this.apiKey,
            ...nonEmptyParams,
        });
        return `${this.baseUrl}${url}?${queryParams.toString()}`;
    }
    async getBooking(id) {
        const url = this.constructUrl(`/bookings/${id}`);
        const response = await (0, extensions_core_1.fetchTyped)(url, schema_1.GetBookingResponseSchema);
        return response.booking;
    }
    async createBooking(value) {
        var _a;
        const url = this.constructUrl(`/bookings`);
        const response = await (0, node_fetch_1.default)(url, {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(value),
        });
        const result = await response.json();
        if (response.status >= 400) {
            throw new Error((_a = result === null || result === void 0 ? void 0 : result.message) !== null && _a !== void 0 ? _a : 'Unknown error in Cal.com API has occurred');
        }
        return result;
    }
    async updateBooking(id, value) {
        const url = this.constructUrl(`/bookings/${id}`);
        const response = await (0, node_fetch_1.default)(url, {
            method: 'PATCH',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(value),
        });
        const result = await response.json();
        return result.booking;
    }
    async deleteBooking(id, value) {
        const url = this.constructUrl(`/bookings/${id}/cancel`, value);
        await (0, node_fetch_1.default)(url, {
            method: 'DELETE',
        });
    }
}
exports.default = CalComApi;
//# sourceMappingURL=calComApi.js.map