"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertValidTimestamp = void 0;
exports.getUniqueIntercomContact = getUniqueIntercomContact;
const dayjs_1 = __importDefault(require("../../lib/dayjs"));
async function getUniqueIntercomContact(request, payload) {
    const { external_id, email } = payload;
    let query;
    if (external_id) {
        query = {
            field: 'external_id',
            operator: '=',
            value: external_id
        };
    }
    else if (email) {
        query = {
            field: 'email',
            operator: '=',
            value: email
        };
    }
    else {
        return;
    }
    const response = await request('https://api.intercom.io/contacts/search', {
        method: 'POST',
        json: { query }
    });
    if (response?.data?.total_count === 1) {
        return response.data.data[0];
    }
}
const convertValidTimestamp = (value) => {
    if (typeof value !== 'string' || /^\d+$/.test(value)) {
        return value;
    }
    const maybeDate = dayjs_1.default.utc(value);
    if (maybeDate.isValid()) {
        return maybeDate.unix();
    }
    return value;
};
exports.convertValidTimestamp = convertValidTimestamp;
//# sourceMappingURL=util.js.map