"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InfobipClient = void 0;
const axios_1 = __importDefault(require("axios"));
const form_data_1 = __importDefault(require("form-data"));
class InfobipBaseAPI {
    constructor({ baseUrl, apiToken, tokenHeaderKey, }) {
        this.post = async (url, { body }, headers) => {
            return await axios_1.default.post(`${this._baseUrl}/${url}`, body, {
                headers: { ...this._headers, ...headers },
            });
        };
        this.put = async (url, { body }) => {
            return await axios_1.default.put(`${this._baseUrl}/${url}`, body, {
                headers: this._headers,
            });
        };
        this.patch = async (url, { body }) => {
            return await axios_1.default.patch(`${this._baseUrl}/${url}`, body, {
                headers: this._headers,
            });
        };
        this.get = async (url) => {
            return await axios_1.default.get(`${this._baseUrl}/${url}`, {
                headers: this._headers,
            });
        };
        this.delete = async (url) => {
            return await axios_1.default.delete(`${this._baseUrl}/${url}`, {
                headers: this._headers,
            });
        };
        this._baseUrl = baseUrl;
        this._token = apiToken;
        this._headers = {
            [tokenHeaderKey]: this._token,
        };
    }
}
class InfobipSmsAPI {
    constructor({ baseUrl, apiToken }) {
        this.send = async (message) => {
            return await this._baseApi.post('sms/2/text/advanced', {
                body: message,
            }, {
                'Content-Type': 'application/json; charset=utf8',
                Accept: 'application/json',
            });
        };
        this._baseApi = new InfobipBaseAPI({
            baseUrl,
            apiToken: `App ${apiToken}`,
            tokenHeaderKey: 'Authorization',
        });
    }
}
class InfobipEmailAPI {
    constructor({ baseUrl, apiToken }) {
        this.send = async (message) => {
            const formData = new form_data_1.default();
            for (const key in message) {
                if (Object.hasOwnProperty.call(message, key)) {
                    // @ts-expect-error this is okay
                    const value = message[key];
                    if (value !== undefined && value !== '') {
                        formData.append(key, value.toString());
                    }
                }
            }
            return await this._baseApi.post('email/3/send', {
                body: formData,
            }, { 'Content-Type': 'multipart/form-data' });
        };
        this._baseApi = new InfobipBaseAPI({
            baseUrl,
            apiToken: `App ${apiToken}`,
            tokenHeaderKey: 'Authorization',
        });
    }
}
class InfobipClient {
    constructor({ baseUrl, apiToken }) {
        this.smsApi = new InfobipSmsAPI({ baseUrl, apiToken });
        this.emailApi = new InfobipEmailAPI({ baseUrl, apiToken });
    }
}
exports.InfobipClient = InfobipClient;
//# sourceMappingURL=client.js.map