export class Profiler {
    now;
    active = [];
    markers = [];
    constructor(now = () => performance.now()) {
        this.now = now;
    }
    begin(name) {
        const trimmed = name.trim();
        if (trimmed.length === 0) {
            throw new Error("Profiler marker name is required");
        }
        this.active.push({ name: trimmed, startMs: this.now(), depth: this.active.length });
    }
    end(name) {
        const active = this.active.pop();
        if (!active) {
            throw new Error("Cannot end profiler marker because no marker is active");
        }
        if (name !== undefined && active.name !== name) {
            throw new Error(`Profiler marker mismatch: expected ${active.name}, received ${name}`);
        }
        const endMs = this.now();
        const marker = {
            name: active.name,
            startMs: active.startMs,
            endMs,
            durationMs: endMs - active.startMs,
            depth: active.depth
        };
        this.markers.push(marker);
        return marker;
    }
    measure(name, fn) {
        this.begin(name);
        try {
            return fn();
        }
        finally {
            this.end(name);
        }
    }
    snapshot() {
        const markers = [...this.markers];
        return Object.freeze({
            markerCount: markers.length,
            totalDurationMs: markers.reduce((total, marker) => total + marker.durationMs, 0),
            markers
        });
    }
    reset() {
        this.active.length = 0;
        this.markers.length = 0;
    }
}
//# sourceMappingURL=Profiler.js.map