import fs from 'fs-extra';
import { stat } from 'node:fs/promises';
import * as path from 'path';
async function removeEmptyDirs(dir) {
    let files;
    try {
        const filenames = await fs.readdir(dir);
        const paths = filenames.map(f => path.join(dir, f));
        files = await Promise.all(paths.map(async (p) => ({ path: p, stat: await stat(p) })));
    }
    catch (error) {
        if (error.code === 'ENOENT')
            return;
        throw error;
    }
    const dirs = files.filter(f => f.stat.isDirectory()).map(f => f.path);
    for (const p of dirs.map(removeEmptyDirs))
        await p;
    files = await fs.readdir(dir).then(async (filenames) => {
        const paths = filenames.map(f => path.join(dir, f));
        return Promise.all(paths.map(async (p) => ({ path: p, stat: await stat(p) })));
    });
    if (files.length === 0)
        await fs.remove(dir);
}
const tidy = async function () {
    const cleanupPlugins = async () => {
        const pluginsDir = path.join(this.config.dataDir, 'plugins');
        if (await fs.pathExists(path.join(pluginsDir, 'plugins.json')))
            return;
        let pjson;
        try {
            pjson = await fs.readJSON(path.join(pluginsDir, 'package.json'));
        }
        catch (error) {
            if (error.code !== 'ENOENT')
                throw error;
            return;
        }
        if (!pjson.dependencies || Object.keys(pjson.dependencies).length === 0) {
            await fs.remove(path.join(pluginsDir));
        }
    };
    await removeEmptyDirs(path.join(this.config.dataDir, 'tmp'));
    if (this.config.configDir !== this.config.dataDir) {
        await removeEmptyDirs(this.config.configDir);
    }
    if (this.config.cacheDir !== this.config.dataDir) {
        await cleanupPlugins();
    }
};
export default tidy;
