"use strict";

// NOTE: Originates from https://git.pixie.town/f0x/shayu-doc/src/branch/master/components/code.jsx, and was slightly modified

const defaultValue = require('default-value');
const React = require('react');
const {default: SyntaxHighlighter} = require('react-syntax-highlighter');

function findLanguageClass(className='') {
	let lang;
	let classes = className.split(' ');
	classes.some((classStr) => {
		let match = classStr.match(/^language-(.+)/);
		if (match) {
			lang = match[1];
		}
		return match;
	});
	return lang
}

let langPresets = {
	default: {
		showLineNumbers: true,
	},
	shell: {
		showLineNumbers: false
	}
}

function code({className, children}) {
	let lang = defaultValue(findLanguageClass(className), "text");
	let preset = defaultValue(langPresets[lang], langPresets.default);
	preset = {...langPresets.default, ...preset};

	return <SyntaxHighlighter
			language={lang}
			className={className}
			useInlineStyles={false}
			showLineNumbers={preset.showLineNumbers}
		>
		{children}
	</SyntaxHighlighter>
}

module.exports = code;
