import { existsSync, mkdirSync } from "node:fs";
import { writeFile } from "node:fs/promises";
import path from "node:path";
import { v7 } from "@aigne/uuid";
const getFileExtension = (type) => import("mime").then((m) => m.default.getExtension(type) || "png");
const saveFiles = async (files, options) => {
    return await Promise.all(files.map(async (file) => {
        if (options.dataDir && !existsSync(options.dataDir)) {
            mkdirSync(options.dataDir, { recursive: true });
        }
        if (file &&
            typeof file === "object" &&
            "type" in file &&
            file.type === "file" &&
            typeof file.data === "string" &&
            file.data.length > 200) {
            const ext = await getFileExtension(file.mimeType || "image/png");
            const id = v7();
            const filename = ext ? `${id}.${ext}` : id;
            const imagePath = path.join(options.dataDir, filename);
            try {
                await writeFile(imagePath, file.data, "base64");
                return { ...file, data: imagePath };
            }
            catch (error) {
                console.error("save files error", error);
                return file;
            }
        }
        if (file &&
            typeof file === "object" &&
            "base64" in file &&
            file.base64 &&
            file.base64.length > 200) {
            const ext = await getFileExtension("image/png");
            const id = v7();
            const filename = ext ? `${id}.${ext}` : id;
            const imagePath = path.join(options.dataDir, filename);
            try {
                await writeFile(imagePath, file.base64, "base64");
                return { ...file, base64: `${file.base64.slice(0, 20)}...`, path: imagePath };
            }
            catch (error) {
                console.error("save files error", error);
                return file;
            }
        }
        return file;
    }));
};
export default saveFiles;
