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

export const handleFileChange = (destinationDir: string, filePath: string, stats?: fs.Stats) => {
    const filename = path.basename(filePath);
    if (!stats) {
        console.log("\x1b[31m%s\x1b[0m", "No file stats");
        return;
    }

    if (stats.isFile()) {
        try {
            fs.copyFile(filePath, path.join(destinationDir, filename), (err) => {
                if (err) throw err;
                console.log("\x1b[2;37m%s\x1b[0m", `${filename} was copied`);
            });
        } catch (error) {
            console.log("\x1b[31m%s\x1b[0m", error);
        }
    }
};

export const handleFileUnlink = (destinationDir: string, filePath: string) => {
    const filename = path.basename(filePath);

    try {
        fs.unlink(path.join(destinationDir, filename), (err) => {
            if (err) throw err;
            console.log("\x1b[2;37m%s\x1b[0m", `${filename} was removed`);
        });
    } catch (error) {
        console.log("\x1b[31m%s\x1b[0m", error);
    }
};
