import { isLogsAttributeColumnId, getLogsAttributeKeyFromColumnId, } from "../Components/LogsViewer/types";
export var LogExportFormat;
(function (LogExportFormat) {
    LogExportFormat["CSV"] = "csv";
    LogExportFormat["JSON"] = "json";
})(LogExportFormat || (LogExportFormat = {}));
function escapeCsvValue(value) {
    if (value.includes(",") ||
        value.includes('"') ||
        value.includes("\n") ||
        value.includes("\r")) {
        return `"${value.replace(/"/g, '""')}"`;
    }
    return value;
}
function getLogFieldValue(log, columnId) {
    var _a, _b;
    if (isLogsAttributeColumnId(columnId)) {
        const attrKey = getLogsAttributeKeyFromColumnId(columnId);
        if (attrKey && log.attributes) {
            const val = log.attributes[attrKey];
            if (val === undefined || val === null) {
                return "";
            }
            if (typeof val === "object") {
                return JSON.stringify(val);
            }
            return String(val);
        }
        return "";
    }
    switch (columnId) {
        case "time":
            return log.time ? new Date(log.time).toISOString() : "";
        case "service":
            return ((_a = log.primaryEntityId) === null || _a === void 0 ? void 0 : _a.toString()) || "";
        case "severity":
            return ((_b = log.severityText) === null || _b === void 0 ? void 0 : _b.toString()) || "";
        case "message":
            return log.body || "";
        case "traceId":
            return log.traceId || "";
        case "spanId":
            return log.spanId || "";
        default:
            return "";
    }
}
function getColumnLabel(columnId) {
    if (isLogsAttributeColumnId(columnId)) {
        return getLogsAttributeKeyFromColumnId(columnId) || columnId;
    }
    switch (columnId) {
        case "time":
            return "Time";
        case "service":
            return "Service ID";
        case "severity":
            return "Severity";
        case "message":
            return "Message";
        case "traceId":
            return "Trace ID";
        case "spanId":
            return "Span ID";
        default:
            return columnId;
    }
}
export function exportLogsToCSV(logs, columns) {
    const header = columns
        .map((col) => {
        return escapeCsvValue(getColumnLabel(col));
    })
        .join(",");
    const rows = logs.map((log) => {
        return columns
            .map((col) => {
            return escapeCsvValue(getLogFieldValue(log, col));
        })
            .join(",");
    });
    return [header, ...rows].join("\n");
}
export function exportLogsToJSON(logs) {
    const data = logs.map((log) => {
        var _a, _b;
        const obj = {};
        obj["time"] = log.time ? new Date(log.time).toISOString() : null;
        obj["primaryEntityId"] = ((_a = log.primaryEntityId) === null || _a === void 0 ? void 0 : _a.toString()) || null;
        obj["severity"] = ((_b = log.severityText) === null || _b === void 0 ? void 0 : _b.toString()) || null;
        obj["severityNumber"] = log.severityNumber || null;
        obj["body"] = log.body || null;
        obj["traceId"] = log.traceId || null;
        obj["spanId"] = log.spanId || null;
        obj["attributes"] = log.attributes || {};
        return obj;
    });
    return JSON.stringify(data, null, 2);
}
function downloadFile(content, filename, mimeType) {
    const blob = new Blob([content], { type: mimeType });
    const url = window.URL.createObjectURL(blob);
    const anchor = document.createElement("a");
    anchor.href = url;
    anchor.download = filename;
    document.body.appendChild(anchor);
    anchor.click();
    document.body.removeChild(anchor);
    window.URL.revokeObjectURL(url);
}
function getTimestampFilename(format) {
    const now = new Date()
        .toISOString()
        .replace(/[:.]/g, "-")
        .slice(0, 19);
    return `logs-${now}.${format}`;
}
export function exportLogs(logs, format, columns) {
    if (format === LogExportFormat.CSV) {
        const csv = exportLogsToCSV(logs, columns);
        downloadFile(csv, getTimestampFilename(format), "text/csv;charset=utf-8;");
    }
    else {
        const json = exportLogsToJSON(logs);
        downloadFile(json, getTimestampFilename(format), "application/json;charset=utf-8;");
    }
}
//# sourceMappingURL=LogExport.js.map