"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendEventToAWS = void 0;
const uuid_1 = require("@lukeed/uuid");
const s3_1 = require("../../lib/AWS/s3");
const client_s3_1 = require("@aws-sdk/client-s3");
const properties_1 = require("./properties");
const NODE_ENV = process.env['NODE_ENV'] || `stage`;
const AWS_REGION = process.env['AWS_REGION'] || `us-west-2`;
const S3_BUCKET_NAME = `integrations-outbound-event-store-${NODE_ENV}-${AWS_REGION}`;
const sendEventToAWS = async (input) => {
    const uuidValue = (0, uuid_1.v4)();
    const aggreagtedFilePath = `${input.destinationInstanceID ?? ''}${input.subscriptionId ? '/' + input.subscriptionId : ''}${input.audienceComputeId ? '/' + input.audienceComputeId : ''}`.replace(/^\/+|\/+$/g, '') || '';
    const userdataFilePath = `${properties_1.ACTION_SLUG}/${aggreagtedFilePath}/${uuidValue}.csv`;
    const metadataFilePath = `${properties_1.ACTION_SLUG}/${aggreagtedFilePath}/meta.json`;
    const metadata = {
        audienceKey: input.audienceComputeId || '',
        uploadType: input.uploadType,
        filename: input.filename,
        gzipCompressFile: input.gzipCompressFile,
        segmentInternal: {
            audienceId: input.audienceComputeId || '',
            destinationConfigId: input.destinationInstanceID || '',
            subscriptionId: input.subscriptionId || ''
        }
    };
    if (input.uploadType === 'sftp') {
        metadata.sftpInfo = {
            sftpHost: properties_1.LIVERAMP_SFTP_SERVER,
            sftpPort: properties_1.LIVERAMP_SFTP_PORT,
            sftpUsername: input.sftpInfo?.sftpUsername || '',
            sftpPassword: input.sftpInfo?.sftpPassword || '',
            sftpFolderPath: input.sftpInfo?.sftpFolderPath || ''
        };
    }
    else {
        metadata.s3Info = {
            s3BucketName: input.s3Info?.s3BucketName || '',
            s3Region: input.s3Info?.s3Region || '',
            s3AccessKeyId: input.s3Info?.s3AccessKeyId || '',
            s3SecretAccessKey: input.s3Info?.s3SecretAccessKey || '',
            s3BucketPath: input.s3Info?.s3BucketPath || ''
        };
    }
    const s3Client = (0, s3_1.getS3Client)('integrationsOutboundController');
    await Promise.all([
        s3Client.send(new client_s3_1.PutObjectCommand({
            Bucket: S3_BUCKET_NAME,
            Key: userdataFilePath,
            Body: input.fileContents,
            ContentType: 'text/csv',
            Metadata: {
                'row-count': `${input.rowCount}`
            }
        })),
        s3Client.send(new client_s3_1.PutObjectCommand({
            Bucket: S3_BUCKET_NAME,
            Key: metadataFilePath,
            Body: JSON.stringify(metadata),
            ContentType: 'application/json'
        }))
    ]);
};
exports.sendEventToAWS = sendEventToAWS;
//# sourceMappingURL=awsClient.js.map