const NOOP_SPAN = {
    setAttribute() { },
    setStatus() { },
    recordException() { },
    end() { },
};
const NOOP_TRACER = {
    startSpan() {
        return NOOP_SPAN;
    },
};
/**
 * AgBashTracer provides optional OpenTelemetry integration.
 * If @opentelemetry/api is not installed, all operations are no-ops (zero overhead).
 */
export class AgBashTracer {
    tracer = NOOP_TRACER;
    config;
    initialized = false;
    constructor(config) {
        this.config = config ?? {};
    }
    /** Attempt to initialize OTel. Call once at startup. Safe to call multiple times. */
    async initialize() {
        if (this.initialized)
            return;
        this.initialized = true;
        try {
            // Dynamic import - only loads if @opentelemetry/api is installed
            const otel = await import("@opentelemetry/api");
            const tracerName = this.config.serviceName ?? "ag-bash";
            this.tracer = otel.trace.getTracer(tracerName);
        }
        catch {
            // @opentelemetry/api not installed - keep no-op tracer
        }
    }
    /** Create a span for a bash execution call */
    startExecSpan(script) {
        const span = this.tracer.startSpan("ag-bash.exec", {
            attributes: {
                "ag-bash.script": script.slice(0, 500),
                ...(this.config.attributes ?? {}),
            },
        });
        return span;
    }
    /** Create a span for a tool call */
    startToolSpan(toolName, args) {
        const span = this.tracer.startSpan(`ag-bash.tool.${toolName}`, {
            attributes: {
                "ag-bash.tool.name": toolName,
                "ag-bash.tool.args": JSON.stringify(args).slice(0, 500),
                ...(this.config.attributes ?? {}),
            },
        });
        return span;
    }
    /** Create a span for a statement (only if statementLevel is enabled) */
    startStatementSpan(type, line) {
        if (!this.config.statementLevel)
            return NOOP_SPAN;
        const attrs = {
            "ag-bash.statement.type": type,
            ...(this.config.attributes ?? {}),
        };
        if (line !== undefined)
            attrs["ag-bash.statement.line"] = line;
        return this.tracer.startSpan(`ag-bash.statement.${type}`, {
            attributes: attrs,
        });
    }
    /** Check if the tracer is active (OTel was loaded successfully) */
    get isActive() {
        return this.tracer !== NOOP_TRACER;
    }
}
