import { log } from '@mondaydotcomorg/atp-runtime';
let auditQueue = [];
let flushTimer = null;
let auditSinks = [];
let isShutdown = false;
const FLUSH_INTERVAL = 5000;
const MAX_QUEUE_SIZE = 100;
export function initAudit(sinks) {
    if (sinks) {
        auditSinks = Array.isArray(sinks) ? sinks : [sinks];
    }
    isShutdown = false;
    startFlushTimer();
}
export async function auditExecution(options) {
    if (isShutdown || auditSinks.length === 0) {
        return;
    }
    const event = {
        eventId: options.executionId || `exec_${Date.now()}`,
        timestamp: Date.now(),
        clientId: options.apiKey || 'anonymous',
        eventType: 'execution',
        action: 'complete',
        code: options.code,
        status: options.result.status === 'completed' ? 'success' : 'failed',
        resourceId: options.executionId,
        metadata: {
            ip: options.ip,
            result: options.result,
        },
    };
    auditQueue.push(event);
    if (auditQueue.length >= MAX_QUEUE_SIZE) {
        await flushAuditQueue();
    }
}
export async function flushAuditQueue() {
    if (auditQueue.length === 0 || auditSinks.length === 0) {
        return;
    }
    const events = [...auditQueue];
    auditQueue = [];
    try {
        await Promise.all(auditSinks.map(async (sink) => {
            if (sink.writeBatch) {
                await sink.writeBatch(events);
            }
            else {
                await Promise.all(events.map((event) => sink.write(event)));
            }
        }));
    }
    catch (error) {
        log.error('Failed to flush audit queue', { error });
    }
}
function startFlushTimer() {
    if (flushTimer) {
        clearInterval(flushTimer);
    }
    flushTimer = setInterval(() => {
        flushAuditQueue().catch((error) => {
            log.error('Failed to flush audit queue on timer', { error });
        });
    }, FLUSH_INTERVAL);
}
export function shutdownAudit() {
    isShutdown = true;
    if (flushTimer) {
        clearInterval(flushTimer);
        flushTimer = null;
    }
    auditSinks = [];
}
//# sourceMappingURL=audit.js.map