/**
 * Copyright © Volker Schukai and all contributing authors, {{copyRightYear}}. All rights reserved.
 * Node module: @schukai/monster
 *
 * This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3).
 * The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html
 *
 * For those who do not wish to adhere to the AGPLv3, a commercial license is available.
 * Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms.
 * For more information about purchasing a commercial license, please contact Volker Schukai.
 *
 * SPDX-License-Identifier: AGPL-3.0
 */

import { Formatter } from "./formatter.mjs";
import { getLocaleOfDocument } from "../dom/locale.mjs";
import { Translations } from "./translations.mjs";

export { getInternalLocalizationMessage };

let internalTranslations = null;
getInternalTranslations();

/**
 *
 * @returns {*|Translations}
 */
function getInternalTranslations() {
	if (internalTranslations) {
		return internalTranslations;
	}

	let locale = "en";
	try {
		locale = getLocaleOfDocument();
	} catch (error) {}

	let messages = {};

	switch (locale.language) {
		case "de":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `Der Server hat die Anfrage nicht akzeptiert (Status \${status}).`,
				"the-response-does-not-contain-a-valid-json": `Die Antwort des Servers ist kein gültiges JSON (actual=\${actual}).`,
			};
			break;
		case "es":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `El servidor no ha aceptado la solicitud (Estado \${status}).`,
				"the-response-does-not-contain-a-valid-json": `La respuesta del servidor no es un JSON válido (actual=\${actual}).`,
			};
			break;
		case "zh":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `服务器未接受请求（状态 \${status}）。`,
				"the-response-does-not-contain-a-valid-json": `服务器响应不是有效的JSON（实际=\${actual}）。`,
			};
			break;
		case "fr":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `Le serveur n'a pas accepté la demande (Statut \${status}).`,
				"the-response-does-not-contain-a-valid-json": `La réponse du serveur n'est pas un JSON valide (actuel=\${actual}).`,
			};
			break;
		case "it":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `Il server non ha accettato la richiesta (Stato \${status}).`,
				"the-response-does-not-contain-a-valid-json": `La risposta del server non è un JSON valido (attuale=\${actual}).`,
			};
			break;
		case "nl":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `De server heeft het verzoek niet geaccepteerd (Status \${status}).`,
				"the-response-does-not-contain-a-valid-json": `De serverrespons is geen geldige JSON (actueel=\${actual}).`,
			};
			break;
		case "sv":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `Servern accepterade inte begäran (Status \${status}).`,
				"the-response-does-not-contain-a-valid-json": `Serverns svar är inte en giltig JSON (faktisk=\${actual}).`,
			};
			break;
		case "pl":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `Serwer nie zaakceptował żądania (Status \${status}).`,
				"the-response-does-not-contain-a-valid-json": `Odpowiedź serwera nie jest prawidłowym JSON-em (aktualne=\${actual}).`,
			};
			break;
		case "da":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `Serveren accepterede ikke forespørgslen (Status \${status}).`,
				"the-response-does-not-contain-a-valid-json": `Serverens svar er ikke en gyldig JSON (aktuel=\${actual}).`,
			};
			break;
		case "fi":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `Palvelin ei hyväksynyt pyyntöä (Tila \${status}).`,
				"the-response-does-not-contain-a-valid-json": `Palvelimen vastaus ei ole kelvollinen JSON (todellinen=\${actual}).`,
			};
			break;
		case "no":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `Serveren aksepterte ikke forespørselen (Status \${status}).`,
				"the-response-does-not-contain-a-valid-json": `Serverens respons er ikke en gyldig JSON (faktisk=\${actual}).`,
			};
			break;
		case "cs":
			messages = {
				"the-response-does-not-contain-an-accepted-status": `Server nepřijal požadavek (Stav \${status}).`,
				"the-response-does-not-contain-a-valid-json": `Odpověď serveru není platný JSON (skutečný=\${actual}).`,
			};
			break;
		default: // English
			messages = {
				"the-response-does-not-contain-an-accepted-status": `The server did not accept the request (Status \${status}).`,
				"the-response-does-not-contain-a-valid-json": `The server response is not a valid JSON (actual=\${actual}).`,
			};
	}

	const translation = new Translations(locale);
	translation.assignTranslations(messages);

	internalTranslations = translation;

	return translation;
}

/**
 * Returns the internal localization message.
 * @param message
 * @returns {string}
 */

function getInternalLocalizationMessage(message) {
	const formatter = new Formatter({}, getInternalTranslations());
	return formatter.format(message);
}
