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

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

const { GetLogs } = mapnik();

export default async function mapnikLogger({
    params, query,
}, reply) {
    if (!config.mapServerAddress) {
        return reply.status(400).send({ error: 'mapnik server address needed', code: 400 });
    }

    if (!['render', 'error', 'info', 'critical'].includes(params.type)) {
        return reply.status(400).send({ error: 'invalid params: type', code: 400 });
    }

    const { result } = await GetLogs({
        period: query.period,
        type: params.type,
    });
    return reply.status(200).send(result.join('\n'));
}
