"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
class CordialClient {
    constructor(settings, request) {
        this.apiUrl = `${settings.endpoint}/api/segment`;
        this.request = request;
        this.identityKeys = {
            segmentIdKey: settings.segmentIdKey,
        };
    }
    extractIdentities(payload) {
        return {
            segmentId: payload.segmentId,
            anonymousId: payload.anonymousId,
            userIdentities: payload.userIdentities,
        };
    }
    async addContactActivity(payload) {
        return this.request(`${this.apiUrl}/createContactactivity`, {
            method: 'post',
            json: {
                ...this.identityKeys,
                ...this.extractIdentities(payload),
                action: payload.action,
                time: payload.time,
                properties: payload.properties,
                context: payload.context
            }
        });
    }
    async upsertContact(payload) {
        return this.request(`${this.apiUrl}/upsertContact`, {
            method: 'post',
            json: {
                ...this.identityKeys,
                ...this.extractIdentities(payload),
                attributes: payload.attributes
            }
        });
    }
    async addContactToList(payload) {
        return this.request(`${this.apiUrl}/addContactToList`, {
            method: 'post',
            json: {
                ...this.identityKeys,
                ...this.extractIdentities(payload),
                groupId: payload.groupId,
                listName: payload.listName
            }
        });
    }
    async removeContactFromList(payload) {
        return this.request(`${this.apiUrl}/removeContactFromList`, {
            method: 'post',
            json: {
                ...this.identityKeys,
                ...this.extractIdentities(payload),
                groupId: payload.groupId
            }
        });
    }
    async addProductToCart(payload) {
        return this.request(`${this.apiUrl}/addProductToCart`, {
            method: 'post',
            json: {
                ...this.identityKeys,
                ...this.extractIdentities(payload),
                productID: payload.productID,
                sku: payload.sku,
                qty: payload.qty,
                category: payload.category,
                name: payload.name,
                description: payload.description,
                itemPrice: payload.itemPrice,
                url: payload.url,
                imageUrl: payload.imageUrl,
                properties: payload.properties
            }
        });
    }
    async removeProductFromCart(payload) {
        return this.request(`${this.apiUrl}/removeProductFromCart`, {
            method: 'post',
            json: {
                ...this.identityKeys,
                ...this.extractIdentities(payload),
                productID: payload.productID,
                qty: payload.qty
            }
        });
    }
    async upsertOrder(payload) {
        return this.request(`${this.apiUrl}/upsertOrder`, {
            method: 'post',
            json: {
                ...this.identityKeys,
                ...this.extractIdentities(payload),
                orderID: payload.orderID,
                purchaseDate: payload.purchaseDate,
                status: payload.status,
                totalAmount: payload.totalAmount,
                properties: payload.properties,
                items: payload.items,
                discountApplication: (!(0, isEmpty_1.default)(payload.discountApplication) && payload?.discountApplication?.type === 'fixed' && payload?.discountApplication?.amount)
                    ? payload.discountApplication
                    : null
            }
        });
    }
    mergeContacts(payload) {
        return this.request(`${this.apiUrl}/mergeContacts`, {
            method: 'post',
            json: {
                ...this.identityKeys,
                anonymousId: payload.anonymousId,
                segmentId: payload.segmentId,
                previousId: payload.previousId
            }
        });
    }
}
exports.default = CordialClient;
//# sourceMappingURL=cordial-client.js.map