/**
 * @mergeTarget
 * @module Src/Controllers/Translations
 */

import {
  LisioCategoryNames,
  LisioStringParameterNames,
  TranslatorTranslationOptions,
} from "@lisio/lisio-profils";
import type { TranslatorAPILanguagesDatas } from "../../messages/message-types";

/**
 * Category of the translation made through the Lisio translator API. * The API decides which engine actually translates, so the widget never refers to a given engine.
 * @source
 */
const TRANSLATOR_CATEGORY = LisioCategoryNames.TRANSLATOR_TRANSLATION;

/**
 * Parameter storing the language chosen for the translator API translation
 * @see {@link TRANSLATOR_CATEGORY}
 * @source
 */
const TRANSLATOR_PARAMETER = LisioStringParameterNames.TRANSLATOR_TRANSLATION;

/**
 * Type of {@link TRANSLATOR_CATEGORY}
 */
type TranslatorCategory = typeof TRANSLATOR_CATEGORY;

/**
 * Widget ISO codes (`en_gb`) of the authorized languages the translator API flagged as not
 * translatable from the language of the page.
 * @see {@link registerTranslatorLanguages}
 * @source
 */
const incompatibleLanguagesISOs = new Set<string>();

/**
 * Converts an ISO code of the translator API (`EN-GB`) to its widget form (`EN_GB`)
 * @param {string} apiIso - ISO code returned by the translator API
 * @returns Returns the ISO code used by the widget
 * @source
 */
function toWidgetIso(apiIso: string): string {
  return apiIso.replace("-", "_");
}

/**
 * Records the compatibility of the languages returned by the translator API route "checkIfAuthorized"
 * (asked with the page language), then lists them.\
 * Replaces what a previous call recorded.
 * @param {TranslatorAPILanguagesDatas} languages - Languages returned by the translator API
 * @returns Returns the widget ISO codes of all the authorized languages, compatible or not
 * @source
 */
function registerTranslatorLanguages(
  languages: TranslatorAPILanguagesDatas,
): string[] {
  incompatibleLanguagesISOs.clear();
  return languages.map((language) => {
    const iso = toWidgetIso(language.languageDTO.languageISO);
    if (language.compatible === false) {
      incompatibleLanguagesISOs.add(iso);
    }
    return iso;
  });
}

/**
 * Tells if a language can be chosen to translate the page.\
 * The translator API decides it from the page language and the engine of the domain (see
 * {@link registerTranslatorLanguages}): the language of the page itself cannot be chosen, the
 * "default" choice brings the user back to it. Languages that cannot be chosen are still listed, but disabled.
 * @param {string} targetIso - Widget ISO code of the language to translate to
 * @returns Returns true if the engine translates from the page language to the target
 * @source
 */
function isTranslatorLanguageCompatible(targetIso: string): boolean {
  return !incompatibleLanguagesISOs.has(targetIso);
}

export {
  TRANSLATOR_CATEGORY,
  TRANSLATOR_PARAMETER,
  TranslatorTranslationOptions,
  isTranslatorLanguageCompatible,
  registerTranslatorLanguages,
};
export type { TranslatorCategory };
