"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 TextLineApi {
    constructor(accessToken) {
        this.baseUrl = 'https://application.textline.com';
        this.accessToken = accessToken;
    }
    constructUrl(url, params) {
        const nonEmptyParams = (0, lodash_1.omitBy)(params, lodash_1.isNil);
        const stringParams = Object.fromEntries(Object.entries(nonEmptyParams).map(([key, value]) => [key, String(value)]));
        const queryParams = new URLSearchParams(stringParams);
        return `${this.baseUrl}${url}?${queryParams.toString()}`;
    }
    async getMessages(phoneNumber, afterMessageId, departmentId, page, pageSize) {
        const url = this.constructUrl(`/api/conversations.json`, {
            phone_number: phoneNumber,
            after_uuid: afterMessageId,
            page_size: pageSize,
            page,
            group_uuid: departmentId,
        });
        const response = await (0, extensions_core_1.fetchTyped)(url, schema_1.GetMessagesSchema, {
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
                'X-TGP-ACCESS-TOKEN': this.accessToken,
            },
        });
        return response;
    }
    async sendMessage(content, recipient, departmentId) {
        const url = this.constructUrl(`/api/conversations.json`);
        const response = await (0, node_fetch_1.default)(url, {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
                'X-TGP-ACCESS-TOKEN': this.accessToken,
            },
            body: JSON.stringify({
                phone_number: recipient,
                comment: {
                    body: content,
                },
                group_uuid: departmentId,
            }),
        });
        const result = await response.json();
        if (response.status >= 400) {
            throw new Error(!(0, lodash_1.isNil)(result === null || result === void 0 ? void 0 : result.errors)
                ? JSON.stringify(result === null || result === void 0 ? void 0 : result.errors)
                : 'Unknown error in TextLine API has occurred');
        }
        return result;
    }
    async setContactConsent(consentStatus, recipient) {
        const url = this.constructUrl(`/api/customers/set_consent.json`);
        const response = await (0, node_fetch_1.default)(url, {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
                'X-TGP-ACCESS-TOKEN': this.accessToken,
            },
            body: JSON.stringify({
                phone_number: recipient,
                consent: consentStatus ? 1 : 0,
            }),
        });
        const result = await response.json();
        if (response.status >= 400) {
            throw new Error(!(0, lodash_1.isNil)(result === null || result === void 0 ? void 0 : result.errors)
                ? JSON.stringify(result === null || result === void 0 ? void 0 : result.errors)
                : 'Unknown error in TextLine API has occurred');
        }
    }
}
exports.default = TextLineApi;
//# sourceMappingURL=textLineApi.js.map