import { lf } from '@linzjs/lambda';
import { LogConfig } from '@basemaps/shared';
import { Ping, Version } from './routes/api.js';
import { Health } from './routes/health.js';
import { Tiles } from './routes/tile.js';
import { Router } from './router.js';
import { createHash } from 'crypto';
import { Imagery } from './routes/imagery.js';
import { Esri } from './routes/esri/rest.js';
import { St } from './source.tracer.js';
import { spriteGet } from './routes/sprites.js';
import { fontGet, fontList } from './routes/fonts.js';
const app = new Router();
app.get('ping', Ping);
app.get('health', Health);
app.get('version', Version);
app.get('tiles', Tiles);
app.get('imagery', Imagery);
app.get('esri', Esri);
let slowTimer = null;
export async function handleRequest(req) {
    // Reset the request tracing
    St.reset();
    // Warn if a request takes more than 10 seconds to process
    if (slowTimer)
        clearTimeout(slowTimer);
    slowTimer = setTimeout(() => req.log.warn(req.logContext, 'Lambda:Slow'), 10000);
    slowTimer.unref();
    req.set('name', 'LambdaTiler');
    try {
        const apiKey = Router.apiKey(req);
        if (apiKey != null) {
            const apiKeyHash = createHash('sha256').update(apiKey).digest('base64');
            req.set('api', apiKeyHash);
        }
        const ret = await app.handle(req);
        // TODO this could be relaxed to every say 5% of requests if logging gets too verbose.
        req.set('requests', St.requests.slice(0, 100)); // limit to 100 requests (some tiles need 100s of requests)
        req.set('requestCount', St.requests.length);
        return ret;
    }
    finally {
        if (slowTimer)
            clearTimeout(slowTimer);
        slowTimer = null;
    }
}
export const handler = lf.http(LogConfig.get());
handler.router.get('/v1/sprites/:spriteName', spriteGet);
handler.router.get('/v1/fonts.json', fontList);
handler.router.get('/v1/fonts/:fontStack/:range.pbf', fontGet);
handler.router.get('*', handleRequest);
