import { GoogleTms, TileMatrixSet } from '@basemaps/geo';
import { Env, extractTileMatrixSet } from '@basemaps/shared';
import { HttpHeader, LambdaHttpResponse } from '@linzjs/lambda';
import { Router } from '../router.js';
import { TileSets } from '../tile.set.cache.js';
import { getTileMatrixId } from '../wmts.capability.js';
import { NotFound } from './response.js';
export async function tileJson(req) {
    var _a, _b, _c;
    const { version, rest, name } = Router.action(req);
    if (rest.length !== 3)
        return NotFound;
    const tileMatrix = extractTileMatrixSet(rest[1]);
    if (tileMatrix == null)
        return NotFound;
    req.timer.start('tileset:load');
    const tileSet = await TileSets.get(rest[0], tileMatrix);
    req.timer.end('tileset:load');
    if (tileSet == null)
        return NotFound;
    const apiKey = Router.apiKey(req);
    const host = (_a = Env.get(Env.PublicUrlBase)) !== null && _a !== void 0 ? _a : '';
    const tileUrl = [host, version, name, tileSet.fullName, getTileMatrixId(tileMatrix), '{z}', '{x}', '{y}'].join('/') +
        `.${tileSet.format}?api=${apiKey}`;
    const tileJson = { tiles: [tileUrl], tilejson: '3.0.0' };
    const maxZoom = TileMatrixSet.convertZoomLevel((_b = tileSet.tileSet.maxZoom) !== null && _b !== void 0 ? _b : 30, GoogleTms, tileMatrix, true);
    const minZoom = TileMatrixSet.convertZoomLevel((_c = tileSet.tileSet.minZoom) !== null && _c !== void 0 ? _c : 0, GoogleTms, tileMatrix, true);
    if (tileSet.tileSet.maxZoom)
        tileJson.maxzoom = maxZoom;
    if (tileSet.tileSet.minZoom)
        tileJson.minzoom = minZoom;
    const json = JSON.stringify(tileJson);
    const data = Buffer.from(json);
    const response = new LambdaHttpResponse(200, 'ok');
    response.header(HttpHeader.CacheControl, 'no-store');
    response.buffer(data, 'application/json');
    req.set('bytes', data.byteLength);
    return response;
}
