import { config } from '@opengis/fastify-table/utils.js';

import mapnik from '../../../plugins/mapnik/funcs/mapnik.js';

const { FileStat } = mapnik();

// example: GET /api/gis-files?path=tiles/3828667893214610450/13/4884/2828.png
export default async function fileStat({
    query,
}, reply) {
    if (!config.mapServerAddress) {
        return reply.status(400).send({ error: 'mapnik server not configured', code: 400 });
    }

    if (config.ready?.mapnik !== true) {
        return reply.status(400).send({ error: 'mapnik server not ready', code: 400 });
    }

    const { path } = query || {};

    if (!path) {
        return reply.status(400).send({ error: 'not enough query params: path', code: 400 });
    }

    const data = await FileStat({ path });
    return { data };
}
