import i18n from 'i18n-js';

import { TranslationKey } from './translation-keys';

/**
 * Translates text.
 *
 * @param key The i18n key.
 */
export function translate(key: TranslationKey, options?: object) {
  return key ? i18n.t(key, options) : null;
}

type TxOrText = { text?: string; tx?: TranslationKey; txOptions?: object };

/**
 * If txs is passed, returns a TxOrText[] with all the txs ans txOptions.
 * If txs is empty, returns a TxOrText[] with the texts elements.
 *
 * @param {string[]} texts Array of strings with the texts
 * @param {TranslationKey[]} txs Array with translatable strings
 * @param {object[]} txOptions Array with the options for the translatable strings
 */
export function getTxOrText(
  texts: string[],
  txs: TranslationKey[],
  txOptions: object[],
): TxOrText[] {
  if (txs && txs.length > 0) {
    return txs.map((tx, index) => ({
      tx,
      txOptions: txOptions[index],
    }));
  } else if (texts) {
    return texts.map((text) => ({
      text,
    }));
  }
  return [];
}
