"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SalesforceRestAPIClient = exports.SalesforceDataWrapper = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const cacheService_1 = require("./cacheService");
class SalesforceDataWrapper extends extensions_core_1.DataWrapper {
    constructor(token, baseUrl, opts) {
        super(token, baseUrl);
        this.apiVersion = opts.apiVersion;
    }
    async createRecord(input) {
        return await this.Request({
            method: 'POST',
            url: `/services/data/${this.apiVersion}/sobjects/${input.sObject}/`,
            headers: {
                'Content-Type': 'application/json',
            },
            data: JSON.stringify(input.data),
        });
    }
    async updateRecord(input) {
        await this.Request({
            method: 'PATCH',
            url: `/services/data/${this.apiVersion}/sobjects/${input.sObject}/${input.sObjectId}`,
            headers: {
                'Content-Type': 'application/json',
            },
            data: JSON.stringify(input.data),
        });
    }
    async getRecordShape(sObject) {
        return await this.Request({
            method: 'GET',
            url: `/services/data/${this.apiVersion}/sobjects/${sObject}/describe`,
            headers: {
                'Content-Type': 'application/json',
            },
        });
    }
    async getRecord(input) {
        return await this.Request({
            method: 'GET',
            url: `/services/data/${this.apiVersion}/sobjects/${input.sObject}/${input.sObjectId}`,
            headers: {
                'Content-Type': 'application/json',
            },
        });
    }
}
exports.SalesforceDataWrapper = SalesforceDataWrapper;
class SalesforceRestAPIClient extends extensions_core_1.APIClient {
    constructor({ authUrl, requestConfig, ...opts }) {
        const getAuth = () => {
            if ('username' in requestConfig && 'password' in requestConfig) {
                return new extensions_core_1.OAuthPassword({
                    auth_url: authUrl,
                    request_config: requestConfig,
                    /**
                     * Not sure whether caching is possible with the password grant
                     */
                    cacheService: cacheService_1.salesforceCacheService,
                });
            }
            return new extensions_core_1.OAuthClientCredentials({
                auth_url: authUrl,
                request_config: requestConfig,
                cacheService: cacheService_1.salesforceCacheService,
            });
        };
        super({
            ...opts,
            auth: getAuth(),
        });
        this.ctor = (token, baseUrl) => new SalesforceDataWrapper(token, baseUrl, { apiVersion: this.apiVersion });
        this.apiVersion = opts.apiVersion;
    }
    async createRecord(input) {
        return await this.FetchData(async (dw) => await dw.createRecord(input));
    }
    async updateRecord(input) {
        await this.FetchData(async (dw) => {
            await dw.updateRecord(input);
        });
    }
    async getRecordShape(sObject) {
        return await this.FetchData(async (dw) => await dw.getRecordShape(sObject));
    }
    async getRecord(input) {
        return await this.FetchData(async (dw) => await dw.getRecord(input));
    }
}
exports.SalesforceRestAPIClient = SalesforceRestAPIClient;
//# sourceMappingURL=client.js.map