import { ConfigProviderMemory } from '@basemaps/config';
import { initConfigFromUrls } from '@basemaps/config-loader';
import { TileMatrixSets } from '@basemaps/geo';
import { fsa, LogConfig, setDefaultConfig } from '@basemaps/shared';
import { LambdaUrlRequest } from '@linzjs/lambda';
import { renderPreview } from '../routes/preview.js';
const target = fsa.toUrl(`/home/blacha/tmp/basemaps/bm-724/test-north-island_20230220_10m/`);
const location = { lat: -39.0852555, lon: 177.3998405 };
const z = 12;
const outputFormat = 'webp';
let tileMatrix = null;
async function main() {
    const log = LogConfig.get();
    const provider = new ConfigProviderMemory();
    setDefaultConfig(provider);
    const { tileSet, imagery } = await initConfigFromUrls(provider, [target]);
    if (tileSet.layers.length === 0)
        throw new Error('No imagery found in path: ' + target.href);
    log.info({ tileSet: tileSet.name, layers: tileSet.layers.length }, 'TileSet:Loaded');
    for (const im of imagery) {
        log.info({ url: im.uri, title: im.title, tileMatrix: im.tileMatrix, files: im.files.length }, 'Imagery:Loaded');
        if (tileMatrix == null) {
            tileMatrix = TileMatrixSets.find(im.tileMatrix);
            log.info({ tileMatrix: im.tileMatrix }, 'Imagery:TileMatrix:Set');
        }
    }
    if (tileMatrix == null)
        throw new Error('No tileMatrix found');
    const req = new LambdaUrlRequest({ headers: {} }, {}, LogConfig.get());
    const res = await renderPreview(req, {
        tileMatrix,
        tileSet,
        location,
        z,
        output: { title: outputFormat, format: [outputFormat], name: 'rgba' },
    });
    const previewFile = fsa.toUrl(`./z${z}_${location.lon}_${location.lat}.${outputFormat}`);
    await fsa.write(previewFile, Buffer.from(res.body, 'base64'));
    log.info({ path: previewFile }, 'Tile:Write');
}
main().catch((err) => {
    LogConfig.get().fatal({ err }, 'Cli:Failed');
    throw err;
});
//# sourceMappingURL=render.preview.js.map