"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var path_1 = require("path");
var istanbul_1 = require("istanbul");
var source_map_1 = require("source-map");
var lang_1 = require("@dojo/core/lang");
var instrumentationSourceMap = {};
var fileSources = {};
var instrumenters = {};
function instrument(filedata, filepath, instrumenterOptions) {
    var instrumenter = getInstrumenter(instrumenterOptions);
    var options = instrumenter.opts;
    if (!options.codeGenerationOptions) {
        options.codeGenerationOptions = {};
    }
    options.codeGenerationOptions = lang_1.mixin(options.codeGenerationOptions, {
        sourceMap: path_1.normalize(filepath),
        sourceMapWithCode: true
    });
    var code = instrumenter.instrumentSync(filedata, path_1.normalize(filepath));
    var map = instrumenter.lastSourceMap();
    if (map) {
        instrumentationSourceMap[filepath] = new source_map_1.SourceMapConsumer(map.toString());
        fileSources[filepath] = filedata;
    }
    return code;
}
exports.default = instrument;
function getInstrumenter(instrumenterOptions) {
    instrumenterOptions = instrumenterOptions || {};
    var coverageVariable = instrumenterOptions.coverageVariable;
    if (!instrumenters[coverageVariable]) {
        var options = lang_1.mixin({
            coverageVariable: coverageVariable,
            noCompact: true,
            noAutoWrap: true
        }, instrumenterOptions);
        instrumenters[coverageVariable] = new istanbul_1.Instrumenter(options);
    }
    return instrumenters[coverageVariable];
}
//# sourceMappingURL=instrument.js.map