// This file is part of React-Invenio-Deposit
// Copyright (C) 2021 Graz University of Technology.
//
// Invenio-app-rdm is free software; you can redistribute it and/or modify it
// under the terms of the MIT License; see LICENSE file for more details.

const { readFileSync, writeFileSync, unlinkSync } = require("fs");
const { gettextToI18next } = require("i18next-conv");
const TRANSLATIONS_BASE_PATH = process.argv[2] || './';
const PACKAGE_JSON_BASE_PATH = process.argv[3] || './';
const languages = process.env.LANGUAGES.split(',') || ['en'];

// it accepts the same options as the cli.
// https://github.com/i18next/i18next-gettext-converter#options
const options = {
    /* you options here */
};

// Compile to JSON translations for each language
for (const lang of languages) {
    gettextToI18next(
        lang,
        readFileSync(`${TRANSLATIONS_BASE_PATH}/${lang}/LC_MESSAGES/messages.po`),
        options
    ).then((result) => {
        writeFileSync(`${PACKAGE_JSON_BASE_PATH}/messages/${lang}/LC_MESSAGES/translations.json`, result);
        unlinkSync(`${PACKAGE_JSON_BASE_PATH}/messages/${lang}/LC_MESSAGES/messages.po`)
    });
}

// (Re)generate translations module index
index_content = "// This file was autogenerated with oarepo-tools\n"

languages.forEach((lang) => index_content += `import TRANSLATE_${lang.toUpperCase()} from "./${lang}/LC_MESSAGES/translations.json"\n`)
translationOpts = languages.map((lang) => `${lang}: { translation: TRANSLATE_${lang.toUpperCase()} },`)

index_content += `export const translations = {
    ${translationOpts.join('\n    ')}
}
`

writeFileSync(`${PACKAGE_JSON_BASE_PATH}/messages/index.js`, index_content)
