import { buildAssetsSurface } from '../selectors/assets-selector.js';
import { buildMemorySurface } from '../selectors/memory-selector.js';
import { buildOperateSurface } from '../selectors/operate-selector.js';
import { buildRunsSurface } from '../selectors/runs-selector.js';
import { buildTrustSurface } from '../selectors/trust-selector.js';
const surfaceBuilders = {
    operate: buildOperateSurface,
    memory: buildMemorySurface,
    runs: buildRunsSurface,
    assets: buildAssetsSurface,
    trust: buildTrustSurface,
};
export const handleSurfaceRoutes = async (ctx, req, res, url) => {
    if (req.method !== 'GET' || !url.pathname.startsWith('/api/dashboard/surface/')) {
        return false;
    }
    const surface = decodeURIComponent(url.pathname.replace('/api/dashboard/surface/', ''));
    const builder = surfaceBuilders[surface];
    if (!builder) {
        ctx.respondJson(res, { error: 'dashboard-surface-not-found', surface }, 404);
        return true;
    }
    const ttlMs = surface === 'assets' ? 15_000 : 2_000;
    await ctx.respondCachedJson(res, `dashboard-surface:${surface}:${url.search}`, ttlMs, () => builder(ctx, url));
    return true;
};
