const i18n = {
	en: {
		ON_PROJECT_ARCHIVE: 'The project was moved to an rachive. Message tracking has been discontinued.', 
		ON_PROJECT_RESTORE: 'The project has been recovered from the archives.',
		ON_CHAT_REMOVE: 'The chat was removed from a project.',
		CHAT_IN_USE: 'Chat is already in use',
		EXPIRED_KEY: 'The key validity time for binding has expired',
		OPEN_PROJECT: 'Open project',
		WELCOME: 'Welcome, strange',
		TASK_MESSAGE: ':clipboard: New task ${PRIORITY}\n${NAME}\n[${CREATOR}](tg://user?id=${CREATOR_ID}) > [${ASSIGNEE}](tg://user?id=${ASSIGNEE_ID})\nPlan date: ${PLAN_DATE}\n[Open a task page](${URL})',
		TASK_PRIORITY_0: '',
		TASK_PRIORITY_1: '',
		TASK_PRIORITY_2: '(Important)',
		TASK_PRIORITY_3: '(Critical)',		
		MEETING_MESSAGE: 'Meeting ${name}',		
		ADMIN_APP: 'Administrator Access',
		USER_APP: 'User Access'
	},
	ru: {
		ON_PROJECT_ARCHIVE: 'Проект помещен в архив. Отслеживание сообщений прекращено.', 
		ON_PROJECT_RESTORE: 'Проект восстановлен из архива.',
		ON_CHAT_REMOVE: 'Чат удален из проекта.',
		CHAT_IN_USE: 'Чат уже используется',
		EXPIRED_KEY: 'Время действия ключа для привязки истекло',
		OPEN_PROJECT: 'Открыть проект',
		WELCOME: 'Добро пожаловать',
		TASK_MESSAGE: '📋 Новая задача ${PRIORITY}\n${NAME}\n<a href="tg://user?id=${CREATOR_ID}">${CREATOR}</a> > <a href="tg://user?id=${ASSIGNEE_ID}">${ASSIGNEE}</a>\nСрок: ${PLAN_DATE}\n<a href="${URL}">Перейти на страницу задачи</a>',
		TASK_PRIORITY_0: '',
		TASK_PRIORITY_1: '',
		TASK_PRIORITY_2: '(Важно)',
		TASK_PRIORITY_3: '(Критично)',
		MEETING_MESSAGE: 'Встреча ${name}',		
		ADMIN_APP: 'Режим администратора',
		USER_APP: 'Режим пользователя'
	}
}

const hasLocale = (locale) => !!i18n[locale]
function getString (locale, code, args = {}) {
	const params = {}	
	Object.keys(args instanceof Object ? args : {}).forEach(key => {
		const value = args[key]
		params[key] = value[0] == '$' ? getString(locale, value.substring(1)) : value
	})	
	
	let res = i18n[locale] ? i18n[locale][code] : undefined
	if (res === undefined)
		res = i18n.en[code]
	if (res === undefined)
		res = code
	if (res === undefined)
		res = 'ERROR'

	res = res.replace(/\${(\w+)}/g, (match, key) => params.hasOwnProperty(key) ? params[key] : match)
	return res
}

module.exports = { hasLocale, getString }