"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class QualtricsApiClient {
    constructor(dc, apiToken, request) {
        this.baseUrl = `https://${dc || 'iad1'}.qualtrics.com`;
        this.apiToken = apiToken;
        this.request = request;
    }
    async whoaAmI() {
        const endpoint = `/API/v3/whoami`;
        return (await this.makeRequest(endpoint, 'get')).result;
    }
    async listDirectories() {
        const endpoint = `/API/v3/directories/`;
        return (await this.makeRequest(endpoint, 'get')).result;
    }
    async createDirectoryContact(directoryId, body) {
        const endpoint = `/API/v3/directories/${directoryId}/contacts`;
        return (await this.makeRequest(endpoint, 'post', body)).result;
    }
    async createContactTransaction(directoryId, body) {
        const endpoint = `/API/v3/directories/${directoryId}/transactions`;
        return (await this.makeRequest(endpoint, 'post', body)).result;
    }
    async searchDirectoryForContact(directoryId, body) {
        const endpoint = `/API/v3/directories/${directoryId}/contacts/search`;
        const filterBody = this.createFilterBody(body);
        if (!filterBody) {
            return [];
        }
        return (await this.makeRequest(endpoint, 'post', filterBody)).result?.elements;
    }
    async makeRequest(endpoint, method, body) {
        const response = await this.request(this.buildUrl(endpoint), {
            ...this.buildRequestParams(method),
            json: body
        });
        return response.data;
    }
    createFilterBody(body) {
        const filterArray = [];
        const bodyKeys = Object.keys(body);
        ['extRef', 'phone', 'email'].forEach((key) => {
            if (bodyKeys.includes(key) && body[key] !== undefined) {
                filterArray.push({
                    filterType: key,
                    comparison: 'eq',
                    value: body[key]
                });
            }
        });
        if (filterArray.length === 1) {
            return {
                filter: filterArray[0]
            };
        }
        else if (filterArray.length > 1) {
            return {
                filter: {
                    conjunction: 'and',
                    filters: filterArray
                }
            };
        }
        return;
    }
    buildUrl(endpoint) {
        let parsedEndpoint = endpoint;
        if (parsedEndpoint.startsWith('/')) {
            parsedEndpoint = parsedEndpoint.substring(1);
        }
        return `${this.baseUrl}/${parsedEndpoint}`;
    }
    buildRequestParams(method) {
        return {
            method,
            headers: {
                'Content-type': 'application/json',
                'X-API-TOKEN': this.apiToken,
                'Internal-Team': 'integrations',
                'Internal-Service': 'segment-destination'
            }
        };
    }
}
exports.default = QualtricsApiClient;
//# sourceMappingURL=qualtricsApiClient.js.map