"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultipleCustomRecordsInSearchResultToAssociateThrowableError = exports.CustomSearchToAssociateThrowableError = exports.MultipleCustomRecordsInSearchResultThrowableError = exports.CustomSearchThrowableError = exports.SegmentUniqueIdentifierMissingRetryableError = exports.MultipleCompaniesInSearchResultThrowableError = exports.RestrictedPropertyThrowableError = exports.CompanySearchThrowableError = exports.MissingIdentityCallThrowableError = exports.HubSpotError = void 0;
exports.isSegmentUniqueIdentifierPropertyError = isSegmentUniqueIdentifierPropertyError;
const actions_core_1 = require("@segment/actions-core");
const actions_core_2 = require("@segment/actions-core");
const properties_1 = require("./properties");
class HubSpotError extends actions_core_1.HTTPError {
}
exports.HubSpotError = HubSpotError;
exports.MissingIdentityCallThrowableError = new actions_core_1.IntegrationError('Identify (Upsert Contact) must be called before Group (Upsert Company) for the HubSpot Cloud (Actions) destination if ’Associate Contact with Company’ property is set to Yes (true).', 'Missing Identity Call', 400);
exports.CompanySearchThrowableError = new actions_core_1.IntegrationError('HubSpot returned 400 error while performing a company search. Please check if ’Company Search Fields’ contains valid properties defined in HubSpot.', 'Company Search Failed', 400);
exports.RestrictedPropertyThrowableError = new actions_core_2.PayloadValidationError(`Segment uses ’${properties_1.SEGMENT_UNIQUE_IDENTIFIER}’ property internally to map Segment Groups to HubSpot Companies. This property shouldn’t be defined explicitly.`);
exports.MultipleCompaniesInSearchResultThrowableError = new actions_core_1.IntegrationError(`The search criteria defined by Company Search Fields returned more than one companies. The update request will be rejected.`, 'Search Criteria Not Unique', 400);
exports.SegmentUniqueIdentifierMissingRetryableError = new actions_core_1.RetryableError(`The ’${properties_1.SEGMENT_UNIQUE_IDENTIFIER}’ property doesn't exist in HubSpot. Segment will attempt to create the property and retry the action.`);
exports.CustomSearchThrowableError = new actions_core_1.IntegrationError('HubSpot returned 400 error while performing a custom object record search. Please check if Custom Object Record Search Fields’ contains valid properties defined in HubSpot.', 'Custom Search Failed', 400);
exports.MultipleCustomRecordsInSearchResultThrowableError = new actions_core_1.IntegrationError(`The search criteria defined by Custom Object Record Search Fields returned more than one records. The update request will be rejected.`, 'Search Criteria Not Unique', 400);
exports.CustomSearchToAssociateThrowableError = new actions_core_1.IntegrationError("HubSpot returned 400 error while performing a custom object record search to associate. Please check if 'Search Fields to Associate custom Object' contains valid properties defined in HubSpot.", 'Custom Search To Associate Failed', 400);
exports.MultipleCustomRecordsInSearchResultToAssociateThrowableError = new actions_core_1.IntegrationError(`Upsert Custom Object operation was completed but the association failed as the search association object returned more than one items. Ensure that you use an unique identifier to lookup an association object.`, 'Associate Search Criteria Not Unique', 400);
function isSegmentUniqueIdentifierPropertyError(error, segmentUniqueIdentifierProperty) {
    if (error?.response?.status !== 400) {
        return false;
    }
    try {
        const errorMessage = error?.response?.data?.message?.replace('Property values were not valid: ', '');
        const errorList = JSON.parse(errorMessage);
        return (errorList.length === 1 &&
            errorList[0].name === segmentUniqueIdentifierProperty &&
            errorList[0].error === 'PROPERTY_DOESNT_EXIST');
    }
    catch (e) {
        return false;
    }
}
//# sourceMappingURL=errors.js.map