const out = {
	currentlang:	'en',
}

const i18n = {
	lang_change_long:			['Skipta yfir á íslensku',
														'Change to english'],
	lang_current:					['en',
														'is'],
	header_title:					['Fíladelfia archival center',
														'Fíladelfia myndhvelfing'],
	header_logout:					['Log out',
														'Skrá út'],
	title:									['Title',
														'Titill'],
	date:									['Date',
														'Dagsetning'],
	language:							['EN',
														'IS'],
	upload_goto:						['Upload',
														'Upphlaða'],
	login_error:						['Error while logging in: {0}',
														'Villa við innskráningu: {0}'],
	login_error_auth:			['Unknown error from server. Try again later.',
														'Óþekkt villa frá vefþjóni. Reyndu aftur seinna.'],
	login_missing_email:		['Email is missing',
														'Email eða nafn vantar'],
	login_missing_password:['Password is missing',
														'Lykilorð vantar'],
	login_email:						['Email or name',
														'Email eða nafn'],
	login_password:				['Password',
														'Lykilorð'],
	login_submit:					['Log in',
														'Skrá inn'],
	logout:									['Log out',
														'Skrá út'],
	upload_missing_title:	['Title is missing',
														'Titill vantar'],
	upload_missing_date:	['Date is missing',
														'Dagsetning vantar'],
	upload_missing_file:	['Video file missing',
														'Myndaskrá vantar'],
	upload_missing_banner:	['Poster image missing',
														'Mynd vantar'],
	upload_error:					['Error while uploading: {0}',
														'Villa við að hlaða upp myndefni: {0}'],
	unsplash:							['Photo by {0} on {1}',
														'Mynd eftir {0} frá {1}'],
	api_down:							['No internet or browser blocked the request.',
														'Ekkert net eða vafri blockaði fyrirspurn.'],
	edit:										['Edit',
														'Breyta'],
	delete:									['Delete',
														'Eyða'],
	article_speaker:			['Speaker',
														'Ræðumaður'],
	delete_error:					['Error while deleting: {0}',
														'Villa við að eyða efni: {0}'],
	article_error:				['Error while saving: {0}',
														'Villa við að vista: {0}'],
	months: {
		'1':									['January',
														'Janúar'],
		'2':									['February',
														'Febrúar'],
		'3':									['March',
														'Mars'],
		'4':									['April',
														'Apríl'],
		'5':									['May',
														'Maí'],
		'6':									['June',
														'Júní'],
		'7':									['July',
														'Júlí'],
		'8':									['August',
														'Ágúst'],
		'9':									['September',
														'September'],
		'10':									['Oktober',
														'Október'],
		'11':									['November',
														'Nóvember'],
		'12':									['December',
														'Desember'],
	},
}
const langs = {
	'en': 0,
	'is': 1,
}

const regexNumber = new RegExp('^\\d+$')

out.printdate = function(date) {
	let day = date.getDate().toString()
	if (out.currentlang === 'en') {
		let last = day[day.length - 1]
		if (last === '1') {
			day += 'st'
		} else if (last === '2') {
			day += 'nd'
		} else if (last === '3') {
			day += 'rd'
		} else {
			day += 'th'
		}
	} else {
		day += '.'
	}
	return `${day} ${out.months[date.getMonth() + 1]} ${date.getFullYear()}, ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
}

out.langset = function(lang) {
	out.currentlang = lang
	let index = langs[lang]

	for (let key of Object.keys(i18n)) {
		if (!Array.isArray(i18n[key])) {
			out[key] = {}
			for (let subKey of Object.keys(i18n[key])) {
				out[key][subKey] = i18n[key][subKey][index]
			}
		} else {
			out[key] = i18n[key][index]
		}
	}
}

out.langtoggle = function() {
	out.langset(out.currentlang === 'en' ? 'is' : 'en')
	return false
}

out.format = function(str, ...args) {
	return out.mformat(str, ...args).join('')
}

out.mformat = function(str, ...args) {
	let split = (str || '').split(/\{|\}/)
	return split.map(function(item) {
		if (regexNumber.test(item)) {
			return args[Number(item)] || item
		}
		return item
	})
}

out.langset('is')

module.exports = out
