"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateFile = generateFile;
exports.validate = validate;
function generateFile(payloads, audienceSettings) {
    const headers = [];
    const columnsField = payloads[0].columns;
    const rows = [];
    payloads.forEach((payload, index, arr) => {
        const action = payload.propertiesOrTraits[payload.audienceName];
        const row = [];
        if (![undefined, null, ''].includes(columnsField.audience_name)) {
            if (index === 0) {
                headers.push(columnsField.audience_name);
            }
            row.push(encodeString(String(payload.audienceName ?? '')));
        }
        if (![undefined, null, ''].includes(columnsField.audience_id)) {
            if (index === 0) {
                headers.push(columnsField.audience_id);
            }
            row.push(encodeString(String(payload.audienceId ?? '')));
        }
        if (![undefined, null, ''].includes(columnsField.audience_action)) {
            if (index === 0) {
                headers.push(columnsField.audience_action);
            }
            row.push(encodeString(String(action ?? '')));
        }
        if (![undefined, null, ''].includes(columnsField.email)) {
            if (index === 0) {
                headers.push(columnsField.email);
            }
            row.push(encodeString(String(payload.email ?? '')));
        }
        if (![undefined, null, ''].includes(columnsField.user_id)) {
            if (index === 0) {
                headers.push(columnsField.user_id);
            }
            row.push(encodeString(String(payload.userId ?? '')));
        }
        if (![undefined, null, ''].includes(columnsField.anonymous_id)) {
            if (index === 0) {
                headers.push(columnsField.anonymous_id);
            }
            row.push(encodeString(String(payload.anonymousId ?? '')));
        }
        if (![undefined, null, ''].includes(columnsField.timestamp)) {
            if (index === 0) {
                headers.push(columnsField.timestamp);
            }
            row.push(encodeString(String(payload.timestamp ?? '')));
        }
        if (![undefined, null, ''].includes(columnsField.message_id)) {
            if (index === 0) {
                headers.push(columnsField.message_id);
            }
            row.push(encodeString(String(payload.messageId ?? '')));
        }
        if (![undefined, null, ''].includes(columnsField.space_id)) {
            if (index === 0) {
                headers.push(columnsField.space_id);
            }
            row.push(encodeString(String(payload.spaceId ?? '')));
        }
        if (![undefined, null, ''].includes(columnsField.integrations_object)) {
            if (index === 0) {
                headers.push(columnsField.integrations_object);
            }
            row.push(encodeString(String(JSON.stringify(payload.integrationsObject) ?? '')));
        }
        if (![undefined, null, ''].includes(columnsField.properties_or_traits)) {
            if (index === 0) {
                headers.push(columnsField.properties_or_traits);
            }
            row.push(encodeString(String(JSON.stringify(payload.propertiesOrTraits) ?? '')));
        }
        if (payload.additional_identifiers_and_traits_columns) {
            for (const [key, value] of Object.entries(payload.additional_identifiers_and_traits_columns)) {
                if (index === 0) {
                    headers.push(String(value));
                }
                row.push(encodeString(String(payload.propertiesOrTraits[String(key)] ?? '')));
            }
        }
        const isLastRow = arr.length === index + 1;
        const rowString = `${row.join(audienceSettings.delimiter === 'tab' ? '\t' : audienceSettings.delimiter)}${isLastRow ? '' : '\n'}`;
        if (index === 0) {
            const headerString = `${headers.join(audienceSettings.delimiter === 'tab' ? '\t' : audienceSettings.delimiter)}\n`;
            rows.push(headerString);
        }
        rows.push(rowString);
    });
    return rows.join('');
}
function encodeString(str) {
    return `"${String(str).replace(/"/g, '""')}"`;
}
function validate(payloads, audienceSettings) {
    const delimiter = audienceSettings.delimiter;
    const columns = payloads[0].columns;
    const additionalIdentifierColumns = payloads[0].additional_identifiers_and_traits_columns;
    Object.values(columns).forEach((columnName) => {
        if (columnName.includes(delimiter)) {
            throw new Error(`Column name ${columnName} cannot contain delimiter: ${delimiter}`);
        }
    });
    if (additionalIdentifierColumns) {
        Object.entries(additionalIdentifierColumns).forEach(([key, value]) => {
            if (typeof value === 'string' && value.includes(delimiter)) {
                throw new Error(`Column name ${key} cannot contain delimiter: ${delimiter}`);
            }
        });
    }
}
//# sourceMappingURL=operations.js.map