import { TileSetNameParser, TileSetType } from '@basemaps/config';
import { GoogleTms, VectorFormat } from '@basemaps/geo';
import { HttpHeader, LambdaHttpResponse } from '@linzjs/lambda';
import { CotarCache } from './cotar.cache.js';
import { NotFound } from './routes/response.js';
import { TileSets } from './tile.set.cache.js';
export class TileSetVector {
    constructor(name, tileMatrix) {
        this.type = TileSetType.Vector;
        this.components = TileSetNameParser.parse(name);
        this.tileMatrix = tileMatrix;
    }
    async init(record) {
        this.tileSet = record;
    }
    /** What format does tile set use */
    get format() {
        return VectorFormat.MapboxVectorTiles;
    }
    get id() {
        return TileSets.id(this.fullName, this.tileMatrix);
    }
    get fullName() {
        return TileSetNameParser.componentsToName(this.components);
    }
    async tile(req, xyz) {
        if (xyz.ext !== VectorFormat.MapboxVectorTiles)
            return NotFound;
        if (xyz.tileMatrix.identifier !== GoogleTms.identifier)
            return NotFound;
        if (this.tileSet.layers.length > 1)
            return new LambdaHttpResponse(500, 'Too many layers in tileset');
        const [layer] = this.tileSet.layers;
        if (layer[3857] == null)
            return new LambdaHttpResponse(500, 'Layer url not found from tileset Config');
        req.timer.start('cotar:load');
        const cotar = await CotarCache.get(layer[3857]);
        if (cotar == null)
            return new LambdaHttpResponse(500, 'Failed to load VectorTiles');
        req.timer.end('cotar:load');
        // Flip Y coordinate because MBTiles files are TMS.
        const y = (1 << xyz.z) - 1 - xyz.y;
        req.timer.start('cotar:tile');
        const tile = await cotar.get(`tiles/${xyz.z}/${xyz.x}/${y}.pbf.gz`);
        if (tile == null)
            return NotFound;
        req.timer.end('cotar:tile');
        const response = new LambdaHttpResponse(200, 'Ok');
        response.buffer(Buffer.from(tile), 'application/x-protobuf');
        response.header(HttpHeader.ContentEncoding, 'gzip');
        return response;
    }
}
//# sourceMappingURL=tile.set.vector.js.map