const { I18nPlugin } = require("@11ty/eleventy");
const i18n = require("eleventy-plugin-i18n");
const eleventySass = require("eleventy-sass");
const toml = require("@iarna/toml");
const fs = require("fs");
const path = require("path");

module.exports = function (eleventyConfig, { defaultLanguage }) {
    // load translations
    const translationsToml = fs.readFileSync(
        path.join(__dirname, "..", "_data", "locale.toml"),
        "utf-8"
    );
    const translations = toml.parse(translationsToml);

    // plugins
    eleventyConfig.addPlugin(eleventySass);
    eleventyConfig.addPlugin(I18nPlugin, {
        defaultLanguage,
        errorMode: "allow-fallback",
    });
    eleventyConfig.addPlugin(i18n, {
        translations,
        fallbackLocales: {
            "*": "en",
        }
    });

    // data extensions
    eleventyConfig.addDataExtension("toml", (contents) => toml.parse(contents));
};