import * as CC from '@meesvdw/coloredconsole';

export function Log(level: 'info' | 'error' | 'warn' | 'crit', ...args: any[]) {
	if (level.includes('.js')) {
		level = args[0];
		args.splice(0, 1);
	}
	function log(origin: string, ...args: any[]) {
		level = <'info' | 'error' | 'warn' | 'crit'>level.toLowerCase().replace('warning', 'warn');

		var format;
		if (level == 'info') format = CC.BG.black + CC.white + '  INFO  ';
		else if (level == 'error') format = CC.BG.black + CC.red + '  ERROR ';
		else if (level == 'warn') format = CC.BG.black + CC.yellow + '  WARN  ';
		else if (level == 'crit') format = CC.BG.red + CC.black + '  CRIT  ';

		if (origin == 'Core' && level != 'crit') origin = CC.green + origin + CC.white;

		var d = new Date();
		var n = d.toLocaleTimeString();

		format = `${format} ${n}  ${origin}   ${args} ${CC.reset}`;
		console.log(format);
	}

	var error = new Error();
	if (error && error.stack) {
		var stackArray = error.stack.replace('Error\n', '').replace(/ at /g, '%*%').replace(/ /g, '').split('%*%');
		stackArray.splice(0, 1);

		if (stackArray.length > 1) {
			var callerSplit = stackArray[1].replace(/\\/g, '/').split('/');

			var caller = callerSplit[callerSplit.length - 1].replace(/\n/g, '').replace(')', '');

			return log(caller, ...args);
		}
	}
	log('Unknown', ...args);
}
