"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
function getNestedObjects(obj, objectPath = '', attributes = {}) {
    if (obj != null || obj != undefined) {
        Object.keys(obj).forEach((key) => {
            const currObjectPath = objectPath ? `${objectPath}.${key}` : key;
            if (typeof obj[key] !== 'object') {
                attributes[currObjectPath] = obj[key].toString();
            }
            else {
                getNestedObjects(obj[key], currObjectPath, attributes);
            }
        });
        return attributes;
    }
}
const objectToQueryString = (object) => Object.keys(object)
    .map((key) => `profile.${key}=${object[key].toString()}`)
    .join('&');
class AdobeTarget {
    constructor(userId, clientCode, traits, request) {
        this.updateProfile = async (statsContext) => {
            const err = await this.lookupProfile(this.userId, this.clientCode, statsContext);
            if (err) {
                throw err;
            }
            else {
                const traits = getNestedObjects(this.traits);
                if (traits) {
                    const requestUrl = `https://${this.clientCode}.tt.omtrdc.net/m2/${this.clientCode}/profile/update?mbox3rdPartyId=${this.userId}&${objectToQueryString(traits)}`;
                    return this.request(requestUrl, {
                        method: 'POST',
                        skipResponseCloning: true
                    });
                }
            }
        };
        this.lookupProfile = async (userId, clientCode, statsContext) => {
            try {
                await this.request(`https://${clientCode}.tt.omtrdc.net/rest/v1/profiles/thirdPartyId/${userId}?client=${clientCode}`, { method: 'get', skipResponseCloning: true });
            }
            catch (error) {
                if (error instanceof Error) {
                    const errorCode = error.message == 'Forbidden' ? 403 : 500;
                    if (errorCode == 500) {
                        statsContext?.statsClient.incr('actions-adobe-target.profile-not-found', 1, statsContext.tags);
                    }
                    throw new actions_core_1.APIError(error.message, errorCode);
                }
            }
            return undefined;
        };
        this.userId = userId;
        this.clientCode = clientCode;
        this.traits = traits;
        this.request = request;
    }
}
exports.default = AdobeTarget;
//# sourceMappingURL=adobeTarget_operations.js.map