import fs from "fs";
import path from "path";

export function renameUnderscoreFilesInDist() {
    return {
        name: "rename-underscore-files-dist",
        closeBundle() {
            const distDir = path.resolve("./dist");
            if (!fs.existsSync(distDir)) {
                console.warn("Dist folder bestaat nog niet");
                return;
            }

            const renameFilesInDir = (dirPath: string) => {
                const items = fs.readdirSync(dirPath);
                for (const item of items) {
                    const fullPath = path.join(dirPath, item);
                    const stat = fs.statSync(fullPath);

                    if (stat.isDirectory()) {
                        renameFilesInDir(fullPath);
                    } else if (item.includes("_")) {
                        const newName = item.replace(/_/g, "-");
                        const newPath = path.join(dirPath, newName);
                        fs.renameSync(fullPath, newPath);
                        console.log(`Renamed: ${fullPath} → ${newPath}`);
                    }
                }
            };
            renameFilesInDir(distDir);
        },
    };
}
