"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendFromRecords = sendFromRecords;
const types_1 = require("../types");
async function sendFromRecords(client, payloads, objectType, syncMode) {
    switch (syncMode) {
        case types_1.SyncMode.Upsert: {
            return await upsertRecords(client, payloads, objectType);
        }
        case types_1.SyncMode.Update: {
            return await updateRecords(client, payloads, objectType);
        }
        case types_1.SyncMode.Add: {
            return await addRecords(client, payloads, objectType);
        }
    }
}
async function upsertRecords(client, payloads, objectType) {
    const response = await client.batchObjectRequest(types_1.ObjReqType.Upsert, objectType, {
        inputs: payloads.map(({ object_details: { id_field_value }, properties, sensitive_properties }) => {
            const idFieldName = payloads[0].object_details.id_field_name;
            return {
                idProperty: idFieldName,
                id: id_field_value,
                properties: { ...properties, ...sensitive_properties, [idFieldName]: id_field_value }
            };
        })
    });
    return returnRecordsWithIds(payloads, response);
}
async function updateRecords(client, payloads, objectType) {
    const existingRecords = (await readRecords(client, payloads, objectType, types_1.ReadType.ReturnRecordsWithIds));
    if (existingRecords.length === 0) {
        return [];
    }
    const response = await client.batchObjectRequest(types_1.ObjReqType.Update, objectType, {
        inputs: existingRecords.map(({ object_details: { id_field_value }, properties, sensitive_properties }) => {
            const idFieldName = payloads[0].object_details.id_field_name;
            return {
                idProperty: idFieldName,
                id: id_field_value,
                properties: { ...properties, ...sensitive_properties }
            };
        })
    });
    return returnRecordsWithIds(existingRecords, response);
}
async function addRecords(client, payloads, objectType) {
    const recordsToCreate = (await readRecords(client, payloads, objectType, types_1.ReadType.ReturnRecordsWithoutIds));
    const response = await client.batchObjectRequest(types_1.ObjReqType.Create, objectType, {
        inputs: recordsToCreate.map(({ object_details: { id_field_value: fromIdFieldValue }, properties, sensitive_properties }) => {
            const idFieldName = payloads[0].object_details.id_field_name;
            return {
                idProperty: idFieldName,
                properties: { ...properties, ...sensitive_properties, [idFieldName]: fromIdFieldValue }
            };
        })
    });
    return returnRecordsWithIds(recordsToCreate, response);
}
async function readRecords(client, payloads, objectType, readType) {
    const idFieldName = payloads[0].object_details.id_field_name;
    const readResponse = await client.batchObjectRequest(types_1.ObjReqType.Read, objectType, {
        properties: [idFieldName],
        idProperty: idFieldName,
        inputs: payloads.map((payload) => {
            return { id: payload.object_details.id_field_value };
        })
    });
    switch (readType) {
        case types_1.ReadType.ReturnRecordsWithIds:
            return returnRecordsWithIds(payloads, readResponse);
        case types_1.ReadType.ReturnRecordsWithoutIds:
            return returnRecordsWithoutIds(payloads, readResponse);
    }
}
function returnRecordsWithIds(payloads, response) {
    const results = response?.data?.results || [];
    const matchingRecords = payloads
        .map((payload) => {
        const matchingResult = results.find((result) => {
            return result.properties[payload.object_details.id_field_name] === payload.object_details.id_field_value;
        });
        if (matchingResult) {
            const payloadWithIds = {
                ...payload,
                object_details: { ...payload.object_details, record_id: matchingResult.id }
            };
            if (payloadWithIds.associations) {
                payloadWithIds.associations.forEach((association) => {
                    association.from_record_id = matchingResult.id;
                });
            }
            if (payloadWithIds.dissociations) {
                payloadWithIds.dissociations.forEach((association) => {
                    association.from_record_id = matchingResult.id;
                });
            }
            return payloadWithIds;
        }
        return null;
    })
        .filter((payload) => payload !== null);
    return matchingRecords;
}
function returnRecordsWithoutIds(payloads, response) {
    const missingRecords = payloads.filter((payload) => {
        return !response.data.results.some((result) => {
            return result.properties[payload.object_details.id_field_name] === payload.object_details.id_field_value;
        });
    });
    return missingRecords;
}
//# sourceMappingURL=hubspot-record-functions.js.map