"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeAPIClient = exports.CanvasAPIClient = exports.CanvasDataWrapper = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const validation_1 = require("../validation");
const utils_1 = require("./utils");
class CanvasDataWrapper extends extensions_core_1.DataWrapper {
    async createAppointment(data) {
        const response = await this.RequestRaw({
            method: 'POST',
            url: `/Appointment`,
            data,
        });
        return (0, utils_1.extractIdFromLocationHeader)(response);
    }
    async getAppointment(id) {
        return await this.Request({
            method: 'GET',
            url: `/Appointment/${id}`,
        });
    }
    async updateAppointment(data) {
        const response = await this.RequestRaw({
            method: 'PUT',
            url: `/Appointment/${data.id}`,
            data,
        });
        return (0, utils_1.extractIdFromLocationHeader)(response);
    }
    async createTask(data) {
        const response = await this.RequestRaw({
            method: 'POST',
            url: `/Task`,
            data,
        });
        return (0, utils_1.extractIdFromLocationHeader)(response);
    }
    async updateTask(data) {
        const response = await this.RequestRaw({
            method: 'PUT',
            url: `/Task/${data.id}`,
            data,
        });
        return (0, utils_1.extractIdFromLocationHeader)(response);
    }
    async getTask(id) {
        const response = await this.Request({
            method: 'GET',
            url: `/Task?_id=${id}`,
        });
        if (response.total === 0)
            throw new Error('Task not found');
        return response.entry[0].resource;
    }
    async createPatient(data) {
        const response = await this.RequestRaw({
            method: 'POST',
            url: `/Patient`,
            data,
        });
        return (0, utils_1.extractIdFromLocationHeader)(response);
    }
    async updatePatient(data) {
        const response = await this.RequestRaw({
            method: 'PUT',
            url: `/Patient/${data.id}`,
            data,
        });
        return (0, utils_1.extractIdFromLocationHeader)(response);
    }
    async getPatient(id) {
        return await this.Request({
            method: 'GET',
            url: `/Patient/${id}`,
        });
    }
    async createQuestionnaireResponses(data) {
        const response = await this.RequestRaw({
            method: 'POST',
            url: `/QuestionnaireResponse`,
            data,
        });
        return (0, utils_1.extractIdFromLocationHeader)(response);
    }
    async getQuestionnaireResponse(id) {
        return await this.Request({
            method: 'GET',
            url: `/QuestionnaireResponse/${id}`,
        });
    }
    async createCoverage(data) {
        const response = await this.RequestRaw({
            method: 'POST',
            url: `/Coverage`,
            data,
        });
        return (0, utils_1.extractIdFromLocationHeader)(response);
    }
    async updateCoverage(data) {
        const response = await this.RequestRaw({
            method: 'PUT',
            url: `/Coverage/${data.id}`,
            data,
        });
        return (0, utils_1.extractIdFromLocationHeader)(response);
    }
    async createClaim(data) {
        const response = await this.RequestRaw({
            method: 'POST',
            url: `/Claim`,
            data,
        });
        return (0, utils_1.extractIdFromLocationHeader)(response);
    }
}
exports.CanvasDataWrapper = CanvasDataWrapper;
class CanvasAPIClient extends extensions_core_1.APIClient {
    constructor({ authUrl, requestConfig, ...opts }) {
        super({
            ...opts,
            auth: new extensions_core_1.OAuthClientCredentials({
                auth_url: authUrl,
                request_config: requestConfig,
            }),
        });
        this.ctor = (token, baseUrl) => new CanvasDataWrapper(token, baseUrl);
    }
    async createAppointment(obj) {
        return await this.FetchData(async (dw) => await dw.createAppointment(obj));
    }
    async getAppointment(id) {
        return await this.FetchData(async (dw) => await dw.getAppointment(id));
    }
    async updateAppointment(obj) {
        return await this.FetchData(async (dw) => await dw.updateAppointment(obj));
    }
    async createTask(obj) {
        return await this.FetchData(async (dw) => await dw.createTask(obj));
    }
    async getTask(id) {
        return await this.FetchData(async (dw) => await dw.getTask(id));
    }
    async updateTask(obj) {
        return await this.FetchData(async (dw) => await dw.updateTask(obj));
    }
    async createPatient(obj) {
        return await this.FetchData(async (dw) => await dw.createPatient(obj));
    }
    async updatePatient(obj) {
        return await this.FetchData(async (dw) => await dw.updatePatient(obj));
    }
    async getPatient(id) {
        return await this.FetchData(async (dw) => await dw.getPatient(id));
    }
    async createQuestionnaireResponses(obj) {
        return await this.FetchData(async (dw) => await dw.createQuestionnaireResponses(obj));
    }
    async getQuestionnaireResponse(id) {
        return await this.FetchData(async (dw) => await dw.getQuestionnaireResponse(id));
    }
    async createCoverage(obj) {
        return await this.FetchData(async (dw) => await dw.createCoverage(obj));
    }
    async updateCoverage(obj) {
        return await this.FetchData(async (dw) => await dw.updateCoverage(obj));
    }
    async createClaim(obj) {
        return await this.FetchData(async (dw) => await dw.createClaim(obj));
    }
}
exports.CanvasAPIClient = CanvasAPIClient;
const makeAPIClient = (payloadSettings) => {
    const { base_url, auth_url, ...auth_request_settings } = validation_1.settingsSchema.parse(payloadSettings);
    return new CanvasAPIClient({
        authUrl: auth_url,
        requestConfig: auth_request_settings,
        baseUrl: base_url,
    });
};
exports.makeAPIClient = makeAPIClient;
//# sourceMappingURL=client.js.map