import path from 'node:path';
import { existsSync } from 'node:fs';
import { rm } from 'node:fs/promises';

import { addCron } from '@opengis/fastify-table/utils.js';
import rootFolder from './mapnik/funcs/rootFolder.mjs';

async function clearOldVectorTiles() {
    if (existsSync(path.join(rootFolder, 'vtile'))) {
        await rm(path.join(rootFolder, 'vtile'), { recursive: true, force: true });
        console.log('clear tiles...', rootFolder, 'vtile');
        return 'vector tiles cleared';
    }
    return 'nothing to clear';
}

async function plugin() {
    addCron(clearOldVectorTiles, 60 * 60 * 24);
}

export default plugin;
