"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.SceneReportMemoryCache = void 0;
class SceneReportMemoryCache {
    constructor(maxSize) {
        this.maxSize = maxSize;
        this.store = new Map();
        if (!Number.isInteger(maxSize) || maxSize <= 0) {
            throw new Error(`SceneReportMemoryCache maxSize must be a positive integer, got ${maxSize}`);
        }
    }
    /**
     * Return the cached report for `traceId` and mark it as most-recently-used
     * by re-inserting it at the tail of the Map. Returns `undefined` on miss.
     */
    get(traceId, routeProfile = 'legacy') {
        const key = this.cacheKey(traceId, routeProfile);
        const report = this.store.get(key);
        if (report === undefined)
            return undefined;
        this.store.delete(key);
        this.store.set(key, report);
        return report;
    }
    /**
     * Insert or update `traceId`. If the cache is full and this is a new key,
     * evict the least-recently-used entry (the first one in insertion order).
     * Re-setting an existing key refreshes its position without evicting.
     */
    set(traceId, report, routeProfile = 'legacy') {
        const key = this.cacheKey(traceId, routeProfile);
        if (this.store.has(key)) {
            this.store.delete(key);
        }
        else if (this.store.size >= this.maxSize) {
            const oldestKey = this.store.keys().next().value;
            if (oldestKey !== undefined) {
                this.store.delete(oldestKey);
            }
        }
        this.store.set(key, report);
    }
    delete(traceId, routeProfile = 'legacy') {
        return this.store.delete(this.cacheKey(traceId, routeProfile));
    }
    clear() {
        this.store.clear();
    }
    get size() {
        return this.store.size;
    }
    cacheKey(traceId, routeProfile) {
        return `${traceId}::${routeProfile}`;
    }
}
exports.SceneReportMemoryCache = SceneReportMemoryCache;
//# sourceMappingURL=sceneReportMemoryCache.js.map