"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.send = send;
exports.clean = clean;
exports.generateFile = generateFile;
exports.encodeString = encodeString;
exports.getAudienceAction = getAudienceAction;
const client_1 = require("./client");
async function send(payloads, settings, rawMapping, signal) {
    const delimiter = payloads[0]?.delimiter;
    const actionColName = payloads[0]?.audience_action_column_name;
    const batchColName = payloads[0]?.batch_size_column_name;
    const headers = Object.entries(rawMapping.columns)
        .filter(([_, value]) => value !== '')
        .map(([column]) => {
        return { cleanName: clean(delimiter, column), originalName: column };
    });
    if (actionColName) {
        headers.push({ cleanName: clean(delimiter, actionColName), originalName: actionColName });
    }
    if (batchColName) {
        headers.push({ cleanName: clean(delimiter, batchColName), originalName: batchColName });
    }
    const fileContent = generateFile(payloads, headers, delimiter, actionColName, batchColName);
    const s3Client = new client_1.Client(settings.s3_aws_region, settings.iam_role_arn, settings.iam_external_id);
    await s3Client.uploadS3(settings, fileContent, payloads[0]?.filename_prefix ?? '', payloads[0]?.s3_aws_folder_name ?? '', payloads[0]?.file_extension, signal);
}
function clean(delimiter, str) {
    if (!str) {
        return '';
    }
    return delimiter === 'tab' ? str : str.replace(delimiter, '');
}
function processField(value) {
    return encodeString(value === undefined || value === null
        ? ''
        : typeof value === 'object'
            ? String(JSON.stringify(value))
            : String(value));
}
function generateFile(payloads, headers, delimiter, actionColName, batchColName) {
    const rows = payloads.map((payload, index) => {
        const isLastRow = index === payloads.length - 1;
        const row = headers.map((header) => {
            if (header.originalName === actionColName) {
                return processField(getAudienceAction(payload));
            }
            if (header.originalName === batchColName) {
                return processField(payloads.length);
            }
            return processField(payload.columns[header.originalName]);
        });
        return Buffer.from(`${row.join(delimiter === 'tab' ? '\t' : delimiter)}${isLastRow ? '' : '\n'}`);
    });
    return Buffer.concat([
        Buffer.from(`${headers.map((header) => header.cleanName).join(delimiter === 'tab' ? '\t' : delimiter)}\n`),
        ...rows
    ]);
}
function encodeString(str) {
    return `"${String(str).replace(/"/g, '""')}"`;
}
function getAudienceAction(payload) {
    if (!payload.traits_or_props || !payload.computation_key) {
        return undefined;
    }
    return payload?.traits_or_props?.[payload.computation_key] ?? undefined;
}
//# sourceMappingURL=functions.js.map