const path = require('path');
const fs = require('fs');
const main = require('../release/main');
const onigLibs = require('../out/tests/onigLibs');

const Registry = main.Registry;

const onigurumaRegistry = new Registry({ loadGrammar, onigLib: onigLibs.getOniguruma()});
const onigasmRegistry = new Registry({ loadGrammar, onigLib: onigLibs.getVSCodeOniguruma()});

function tokenize(grammar, content) {
	const start = Date.now();
	let ruleStack = null;
	for (let i = 0; i < content.length; i++) {
		const r = grammar.tokenizeLine(content[i], ruleStack);
		ruleStack = r.ruleStack;
	}
	return Date.now() - start;
}

async function tokenizeFile(filePath, scope, message) {
	const content = fs.readFileSync(filePath, 'utf8')
	const lines = content.split(/\r\n|\r|\n/);

	let onigurumaGrammar  = await onigurumaRegistry.loadGrammar(scope);
	let onigurumaTime = tokenize(onigurumaGrammar, lines);

	let onigasmGrammar  = await onigasmRegistry.loadGrammar(scope);
	let onigasmTime = tokenize(onigasmGrammar, lines);
	console.log();
	console.log(message);
	console.log('TOKENIZING ' + content.length + ' lines using grammar ' + scope);
	const comparison = (onigurumaTime < onigasmTime ? `${(onigasmTime / onigurumaTime).toFixed(1)}x slower` : `${(onigurumaTime / onigasmTime).toFixed(1)}x faster`);
	console.log(`Oniguruma: ${onigurumaTime} ms., Onigasm: ${onigasmTime} ms. (${comparison})`);
}

function loadGrammar(scopeName) {
	let grammarPath = null;
	if (scopeName === 'source.js') {
		grammarPath = path.resolve(__dirname, '..', 'test-cases/first-mate/fixtures/javascript.json');
	} else if (scopeName === 'source.ts') {
		grammarPath = path.resolve(__dirname, '..', 'test-cases/themes/syntaxes/TypeScript.tmLanguage.json');
	} else if (scopeName === 'source.css') {
		grammarPath = path.resolve(__dirname, '..', 'test-cases/first-mate/fixtures/css.json');
	} else if (scopeName === 'source.json') {
		grammarPath = path.resolve(__dirname, '..', 'test-cases/themes/syntaxes/JSON.json');
	} else {
		return null;
	}
	return Promise.resolve(main.parseRawGrammar(fs.readFileSync(grammarPath).toString(), grammarPath));
}

async function test() {
	await tokenizeFile(path.join(__dirname, 'large.js.txt'), 'source.js', 'jQuery v2.0.3');
	await tokenizeFile(path.join(__dirname, 'bootstrap.css.txt'), 'source.css', 'Bootstrap CSS v3.1.1'),
	await tokenizeFile(path.join(__dirname, 'vscode.d.ts.txt'), 'source.ts', 'vscode.d.ts');
	await tokenizeFile(path.join(__dirname, 'JavaScript.tmLanguage.json.txt'), 'source.ts', 'JSON');
	await tokenizeFile(path.join(__dirname, 'bootstrap.min.css.txt'), 'source.css', 'Bootstrap CSS v3.1.1 minified')
	await tokenizeFile(path.join(__dirname, 'large.min.js.txt'), 'source.js', 'jQuery v2.0.3 minified');
	await tokenizeFile(path.join(__dirname, 'main.08642f99.css.txt'), 'source.css', 'Bootstrap with multi-byte minified')
};
test();



@@ -276,103 +276,8 @@
 %7D);%0D
-%0Aconst onigasmRegistry = new Registry(%7B loadGrammar, onigLib: onigLibs.getVSCodeOniguruma()%7D);%0D
 %0A%0D%0Af
@@ -824,127 +824,8 @@
 %0D%0A%0D%0A
-%09let onigasmGrammar  = await onigasmRegistry.loadGrammar(scope);%0D%0A%09let onigasmTime = tokenize(onigasmGrammar, lines);%0D%0A
 %09con
@@ -951,257 +951,48 @@
 cons
-t comparison = (onigurumaTime %3C onigasmTime ? %60$%7B(onigasmTime / onigurumaTime).toFixed(1)%7Dx slower%60 : %60$%7B(onigurumaTime / onigasmTime).toFixed(1)%7Dx faster%60);%0D%0A%09console.log(%60Oniguruma: $%7BonigurumaTime%7D ms., Onigasm: $%7BonigasmTime%7D ms. ($%7Bcomparison%7D)
+ole.log(%60Oniguruma: $%7BonigurumaTime%7D ms.
 %60);%0D

Fix benchmark script