/**
 * FilteringExporter — Category-based span filtering for OpenTelemetry.
 *
 * Wraps a real SpanExporter and drops spans from disabled categories
 * before they reach the backend. This lets you instrument aggressively
 * and control export volume at runtime.
 *
 * Categories are set via the `otel.category` span attribute.
 * Spans without a category are always exported.
 *
 * Control which categories are active via OTEL_ENABLED_CATEGORIES env var:
 *   OTEL_ENABLED_CATEGORIES=http,business,inference
 *
 * If OTEL_ENABLED_CATEGORIES is not set, all categories are exported.
 */
export const ALL_CATEGORIES = [
    "http",
    "db",
    "business",
    "inference",
    "state",
    "system",
];
function getEnabledCategories() {
    const env = process.env.OTEL_ENABLED_CATEGORIES;
    if (!env)
        return new Set(ALL_CATEGORIES);
    return new Set(env
        .split(",")
        .map((c) => c.trim())
        .filter(Boolean));
}
export class FilteringExporter {
    inner;
    enabledCategories;
    constructor(inner) {
        this.inner = inner;
        this.enabledCategories = getEnabledCategories();
    }
    export(spans, resultCallback) {
        const filtered = spans.filter((span) => {
            const category = span.attributes["otel.category"];
            // Spans without a category are always exported
            if (!category)
                return true;
            return this.enabledCategories.has(category);
        });
        if (filtered.length > 0) {
            this.inner.export(filtered, resultCallback);
        }
        else {
            resultCallback({ code: 0 });
        }
    }
    async shutdown() {
        return this.inner.shutdown();
    }
    async forceFlush() {
        return this.inner.forceFlush?.() ?? Promise.resolve();
    }
    /** Re-read OTEL_ENABLED_CATEGORIES from env. Call after changing the env var at runtime. */
    refreshCategories() {
        this.enabledCategories = getEnabledCategories();
    }
}
