"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Hubspot = void 0;
const errors_1 = require("../errors");
const properties_1 = require("../properties");
const utils_1 = require("../utils");
class Hubspot {
    constructor(request, objectType, toObjectType) {
        this.request = request;
        this.objectType = objectType;
        this.toObjectType = toObjectType;
    }
    async search(searchFields, objectType, responseProperties, responseSortBy) {
        if (typeof searchFields === 'object' && Object.keys(searchFields).length > 0) {
            const searchPayload = {
                filterGroups: [],
                properties: [...responseProperties],
                sorts: [...responseSortBy]
            };
            for (const [key, value] of Object.entries(searchFields)) {
                searchPayload.filterGroups.push({
                    filters: [
                        {
                            propertyName: key,
                            operator: utils_1.SearchFilterOperator.EQ,
                            value
                        }
                    ]
                });
            }
            return this.request(`${properties_1.HUBSPOT_BASE_URL}/crm/v3/objects/${objectType}/search`, {
                method: 'POST',
                json: {
                    ...searchPayload
                }
            });
        }
        return null;
    }
    async create(properties, associations = []) {
        return this.request(`${properties_1.HUBSPOT_BASE_URL}/crm/v3/objects/${this.objectType}`, {
            method: 'POST',
            json: {
                properties: properties,
                associations: associations
            }
        });
    }
    async update(uniqueIdentifier, properties, idProperty) {
        const updateURL = `${properties_1.HUBSPOT_BASE_URL}/crm/v3/objects/${this.objectType}/${uniqueIdentifier}` +
            (idProperty ? `?idProperty=${idProperty}` : '');
        return this.request(updateURL, {
            method: 'PATCH',
            json: {
                properties: properties
            }
        });
    }
    async associate(objectId, toObjectId, associations) {
        const associateURL = `${properties_1.HUBSPOT_BASE_URL}/crm/v4/objects/${this.objectType}/${objectId}/associations/${this.toObjectType}/${toObjectId}`;
        return this.request(associateURL, {
            method: 'PUT',
            json: associations
        });
    }
    async getObjectResponseToAssociate(searchFieldsToAssociateCustomObjects, associationType) {
        try {
            if (this.toObjectType &&
                associationType &&
                Object.keys(associationType).length > 0 &&
                searchFieldsToAssociateCustomObjects &&
                Object.keys(searchFieldsToAssociateCustomObjects).length > 0) {
                let searchCustomResponseToAssociate = null;
                searchCustomResponseToAssociate = await this.search({ ...searchFieldsToAssociateCustomObjects }, this.toObjectType, [], []);
                if (searchCustomResponseToAssociate?.data && searchCustomResponseToAssociate?.data?.total) {
                    return searchCustomResponseToAssociate;
                }
            }
            return null;
        }
        catch (e) {
            if (e?.response?.status === 400) {
                throw errors_1.CustomSearchToAssociateThrowableError;
            }
            throw e;
        }
    }
}
exports.Hubspot = Hubspot;
//# sourceMappingURL=index.js.map