import { getAppDataPath } from "@insite/server-framework/FileHelper";
import { saveTranslations } from "@insite/server-framework/InternalService";
import { logger } from "@insite/server-framework/StartServer";
import { constants, createReadStream, promises } from "fs";

export async function generateTranslations(authorization: string) {
    const filePath = `${getAppDataPath()}/translations.csv`;
    try {
        await promises.access(filePath, constants.F_OK);
        const response = await saveTranslations(createReadStream(filePath, { encoding: "utf8" }), authorization);
        return await response.json();
    } catch (error) {
        var message = error;
        if (error instanceof Error) {
            message = error.message;
        }
        logger.error(`An error occurred importing Spire translations: ${message}`);
        return { message: "An unkown error occurred generating translations" };
    }
}
