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,
}, reply) {
    if (!config.mapServerAddress) {
        return reply.status(400).send({ error: 'mapnik server address needed', code: 400 });
    }

    const { data, links, err } = await GetLogs({ path: params['*'] });
    console.log(links);
    if (err) {
        return reply.status(400).send(err);
    }

    if (links) {
        return reply.headers({
            "Content-Type": "text/html; charset=UTF-8",
            "Content-Security-Policy": "default-src 'none'",
            "X-Content-Type-Options": "nosniff",
        }).send(links.replace(/\/api\/gis-logger/g, '/logger-gis'));
    }

    return reply.headers({ "Content-type": "text/plain; charset=UTF-8" }).send(data);
}
