export default class Lang {

	constructor({ translations = {}, prefix = '' } = {}) {
		this.translations = translations;
		this.prefix = prefix;
	}

	get(key = '', params = []) {
		if(key === '') return '';

		const normalizedParams = Array.isArray(params) ? params : [params];

		if(Object.prototype.hasOwnProperty.call(this.translations, key)) {
			let text = this.translations[key];
			normalizedParams.forEach((param, index) => {
				text = text.replace('$' + index, param);
			});
			return text;
		}

		console.warn('Missing translation:', key);
		return key;
	}

	parse(message = '') {
		if(this.prefix && typeof message === 'string' && message.startsWith(this.prefix)) {
			return this.get(message.slice(this.prefix.length));
		}
		return message;
	}
}
