const DareError = require('./error');

module.exports = function unwrap_field(expression, formatter = (obj => obj)) {

	const reg = /^(([a-z_]+)\(([a-z_]+\s){,5}){,5}([a-z0-9$._*]+?)(\)){,5}$/i;
	const m = typeof expression === 'string' && expression.match(reg);

	if (m) {
		const field = m[4];
		const prefix = m[1] || '';
		const suffix = m[5] || '';

		if ((prefix.match(/\(/g) || []).length === suffix.length) {

			const a = field.split('.');
			const field_name = a.pop();
			const field_path = a.join('.');

			// This passes the test
			return formatter({
				field,
				field_name,
				field_path,
				prefix,
				suffix
			});
		}
	}

	// Is this a valid field
	throw new DareError(DareError.INVALID_REFERENCE, `The field definition '${expression}' is invalid.`);
};
@@ -155,13 +155,15 @@
 %5Cs)%7B
+0
 ,5%7D)%7B
+0
 ,5%7D(
@@ -178,23 +178,75 @@
 ._*%5D
-+?
+*
 )(%5C))%7B
+0
 ,5%7D$/i;
+ // eslint-disable-line security/detect-unsafe-regex
 %0A%09co

apply detect-unsafe-eval, potential ReDoS