import PerformScript from "./perform-script";
import {ScriptOptions} from "./types";

/**
 * Evaluate an expression in FileMaker using optional letVars. </p>
 * <p>This is also a handy way to set $$GLOBAL variables, since any values in the `letVars` object are set in a `Let(...)` statement. e.g.:
 * ```
 * fmPromise.evaluate(a+b, {a:1, b:2, $$LAST_ACTION:'Add'})
 * ```
 * @param exp:string calculation to evaluate
 * @param letVars:object key/value pairs which may be used in <code>exp</code>
 * @param options {}
 * @return {Promise} containing the evaluated result
 */
export default function Evaluate(exp:string, letVars = {}, options:ScriptOptions): Promise<any> {
	const letEx = Object.entries(letVars || {}).map((o) => o[0] + '=' + JSON.stringify(o[1])).join(';');
	const stmt = 'Let([' + letEx + '] ; ' + exp + ')';
	return PerformScript('fmPromise.evaluate', stmt, options);
};
