/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import * as fs from 'node:fs';
import * as path from 'node:path';
import { ExportResultCode } from '@opentelemetry/core';
import { AggregationTemporality, } from '@opentelemetry/sdk-metrics';
class FileExporter {
    filePath;
    constructor(filePath) {
        this.filePath = filePath;
        // Ensure directory exists
        const dir = path.dirname(filePath);
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir, { recursive: true });
        }
    }
    serialize(data) {
        return JSON.stringify(data, null, 2) + '\n';
    }
    writeToFile(data) {
        // Use synchronous append to ensure immediate write
        fs.appendFileSync(this.filePath, data, 'utf-8');
    }
    shutdown() {
        // Nothing to do for sync writes
        return Promise.resolve();
    }
}
export class FileSpanExporter extends FileExporter {
    export(spans, resultCallback) {
        try {
            const data = spans.map((span) => this.serialize(span)).join('');
            this.writeToFile(data);
            resultCallback({
                code: ExportResultCode.SUCCESS,
            });
        }
        catch (error) {
            resultCallback({
                code: ExportResultCode.FAILED,
                error: error,
            });
        }
    }
}
export class FileLogExporter extends FileExporter {
    export(logs, resultCallback) {
        try {
            const data = logs.map((log) => this.serialize(log)).join('');
            this.writeToFile(data);
            resultCallback({
                code: ExportResultCode.SUCCESS,
            });
        }
        catch (error) {
            resultCallback({
                code: ExportResultCode.FAILED,
                error: error,
            });
        }
    }
}
export class FileMetricExporter extends FileExporter {
    export(metrics, resultCallback) {
        try {
            const data = this.serialize(metrics);
            this.writeToFile(data);
            resultCallback({
                code: ExportResultCode.SUCCESS,
            });
        }
        catch (error) {
            resultCallback({
                code: ExportResultCode.FAILED,
                error: error,
            });
        }
    }
    getPreferredAggregationTemporality() {
        return AggregationTemporality.CUMULATIVE;
    }
    async forceFlush() {
        return Promise.resolve();
    }
}
//# sourceMappingURL=file-exporters.js.map