Object.defineProperty(exports, "__esModule", { value: true });
const compile_1 = require("./compile");
const InMemoryLogServer_1 = require("./LogServer/InMemoryLogServer");
const prettier = require("prettier");
const inMemoryLocStore = {
    _locs: {},
    write(locs, callback) {
        Object.keys(locs).forEach(key => {
            this._locs[key] = locs[key];
        });
        if (callback) {
            callback();
        }
    },
    getLoc(locId, callback) {
        callback(this._locs[locId]);
    }
};
const server = new InMemoryLogServer_1.default(inMemoryLocStore);
exports.server = server;
function instrumentAndRun(code, outsideArgs = {}, options = { logCode: false }) {
    const { logCode } = options;
    return new Promise(resolve => {
        code = `getTrackingAndNormalValue((function(){ ${code} })())`;
        compile_1.default(code).then((compileResult) => {
            var code = compileResult.code;
            const relevantCode = code.split("* HELPER_FUNCTIONS_END */")[1];
            server._locStore.write(compileResult.locs, function () {
                /* don't bother waiting since store is sync */
            });
            if (logCode) {
                console.log(prettier.format(code).split("* HELPER_FUNCTIONS_END */")[1]);
            }
            const __storeLog = server.storeLog.bind(server);
            var result = eval(code);
            delete global["__didInitializeDataFlowTracking"];
            result.code = relevantCode; // only the interesting code
            if (result.tracking) {
                server.loadLog(result.tracking, (err, log) => {
                    if (err) {
                        throw err;
                    }
                    // remove the extra fn arg/fnret/ret statement... from getTrackingAndNormalValue
                    result.tracking = log.extraArgs.returnValue.args.returnValue;
                    // console.log(result.tracking)
                    resolve(result);
                }, 10);
            }
            else {
                resolve(result);
            }
        });
    });
}
exports.instrumentAndRun = instrumentAndRun;
//# sourceMappingURL=testHelpers.js.map