import { messages } from '../messages';
import { MessageLangProps, SupportedLanguage } from '../types';

export function MessageLangVar({ text, data = [], variables = {} }: MessageLangProps & { variables?: Record<string, string>; }, langFunc?: string) {
    const lang: SupportedLanguage = (langFunc as SupportedLanguage) || 'ja';

    let message = messages[lang][text];

    data.forEach((value, index) => {
        message = message.replace(new RegExp('\\{' + index + '\\}', 'g'), value);
    });

    if (variables) {
        Object.keys(variables).forEach(key => {
            message = message.replace(new RegExp('\\{' + key + '\\}', 'g'), variables[key]);
        });
    }

    return message;
}
