'use strict';

/* ────────────────────────────────────────────────────────────────
   Debug Tools — Play mode, profiler, entity inspector, console
──────────────────────────────────────────────────────────────── */
class Profiler {
    constructor(maxFrames = 300) {
        this.frames = [];
        this.currentFrame = null;
        this._pending = null;
        this._frameIndex = 0;
        this.maxFrames = maxFrames;
    }
    beginFrame() {
        this.currentFrame = {
            frameIndex: this._frameIndex++,
            totalMs: 0,
            samples: [],
        };
    }
    begin(label) {
        this._pending = { label, start: performance.now() };
    }
    end() {
        if (!this._pending || !this.currentFrame)
            return;
        const endMs = performance.now();
        this.currentFrame.samples.push({
            label: this._pending.label,
            startMs: this._pending.start,
            endMs,
            durationMs: endMs - this._pending.start,
        });
        this._pending = null;
    }
    endFrame() {
        if (!this.currentFrame)
            return;
        const totalMs = this.currentFrame.samples.reduce((s, v) => s + v.durationMs, 0);
        this.currentFrame.totalMs = totalMs;
        this.frames.push(this.currentFrame);
        if (this.frames.length > this.maxFrames)
            this.frames.shift();
        this.currentFrame = null;
    }
    getFrames() {
        return this.frames;
    }
    getAverage(label) {
        const matching = [];
        for (const f of this.frames) {
            for (const s of f.samples) {
                if (s.label === label)
                    matching.push(s);
            }
        }
        if (matching.length === 0)
            return 0;
        return matching.reduce((sum, v) => sum + v.durationMs, 0) / matching.length;
    }
    getLastFrame() {
        return this.frames.length > 0 ? this.frames[this.frames.length - 1] : null;
    }
    clear() {
        this.frames.length = 0;
        this._frameIndex = 0;
    }
}
/* ── FPS Counter ──────────────────────────────────────────────── */
class FPSCounter {
    constructor() {
        this.times = [];
        this._fps = 0;
    }
    get fps() { return this._fps; }
    tick(now = performance.now()) {
        this.times.push(now);
        // Keep last 1 second
        while (this.times.length > 0 && this.times[0] <= now - 1000) {
            this.times.shift();
        }
        this._fps = this.times.length;
    }
}
class ConsoleLogger {
    constructor(maxEntries = 500) {
        this._entries = [];
        this.maxEntries = maxEntries;
    }
    set onChange(cb) {
        this._onChange = cb;
    }
    get entries() {
        return this._entries;
    }
    log(message) { this.add('info', message); }
    warn(message) { this.add('warn', message); }
    error(message) { this.add('error', message); }
    debug(message) { this.add('debug', message); }
    add(level, message) {
        var _a;
        // Collapse duplicate consecutive messages
        const last = this._entries[this._entries.length - 1];
        if (last && last.level === level && last.message === message) {
            last.count++;
            last.timestamp = Date.now();
        }
        else {
            this._entries.push({ level, message, timestamp: Date.now(), count: 1 });
            if (this._entries.length > this.maxEntries) {
                this._entries.shift();
            }
        }
        (_a = this._onChange) === null || _a === void 0 ? void 0 : _a.call(this);
    }
    clear() {
        var _a;
        this._entries.length = 0;
        (_a = this._onChange) === null || _a === void 0 ? void 0 : _a.call(this);
    }
    filtered(levels) {
        return this._entries.filter(e => levels.includes(e.level));
    }
}
const ALL_COMPONENT_TYPES = [
    'Transform', 'Sprite', 'Shape', 'Collider2D', 'RigidBody2D',
    'Text', 'Animator', 'Script', 'AudioSource', 'ParticleEmitter', 'CameraTarget',
];
function snapshotEntity(world, id) {
    const entity = world.getEntity(id);
    if (!entity)
        return null;
    const components = {};
    for (const type of ALL_COMPONENT_TYPES) {
        const c = world.getComponent(id, type);
        if (c)
            components[type] = JSON.parse(JSON.stringify(c));
    }
    return {
        id: entity.id,
        name: entity.name,
        tags: [...entity.tags],
        enabled: entity.enabled,
        components,
    };
}
function snapshotWorld(world) {
    return world.allEntities().map(e => snapshotEntity(world, e.id));
}
/* ── Collision Debug Overlay ──────────────────────────────────── */
function renderCollisionDebug(ctx, world) {
    const entityIds = world.query('Transform', 'Collider2D');
    for (const eid of entityIds) {
        const transform = world.getComponent(eid, 'Transform');
        const collider = world.getComponent(eid, 'Collider2D');
        const pos = transform.position;
        ctx.strokeStyle = collider.isTrigger ? 'rgba(0,255,255,0.6)' : 'rgba(0,255,0,0.6)';
        ctx.lineWidth = 1;
        if (collider.shape === 'circle') {
            ctx.beginPath();
            ctx.arc(pos.x + collider.offset.x, pos.y + collider.offset.y, collider.radius, 0, Math.PI * 2);
            ctx.stroke();
        }
        else {
            ctx.strokeRect(pos.x + collider.offset.x - collider.width / 2, pos.y + collider.offset.y - collider.height / 2, collider.width, collider.height);
        }
    }
}
function getMemoryStats(world) {
    var _a, _b, _c, _d;
    const perf = performance;
    return {
        entityCount: world.allEntities().length,
        systemCount: 0, // systems are internal
        jsHeapUsed: (_b = (_a = perf.memory) === null || _a === void 0 ? void 0 : _a.usedJSHeapSize) !== null && _b !== void 0 ? _b : null,
        jsHeapTotal: (_d = (_c = perf.memory) === null || _c === void 0 ? void 0 : _c.totalJSHeapSize) !== null && _d !== void 0 ? _d : null,
    };
}

exports.ConsoleLogger = ConsoleLogger;
exports.FPSCounter = FPSCounter;
exports.Profiler = Profiler;
exports.getMemoryStats = getMemoryStats;
exports.renderCollisionDebug = renderCollisionDebug;
exports.snapshotEntity = snapshotEntity;
exports.snapshotWorld = snapshotWorld;
//# sourceMappingURL=DebugTools.js.map
