var fs = require('fs'),
    path = require('path'),
    _ = require('lodash'),
    gulp = require('gulp'),
    through = require('through2'),
    file = require('gulp-file');

module.exports = mapSrcPlugin;

var contents = {};

/**
 * @ignore
 * Loads `map.json` into stream.
 * @alias "map.src"
 * @param {Object} data Cross-plugin request data
 * @returns {stream.Transform} Stream
 */
function mapSrcPlugin (data) {
    if (!contents[data.src]) {
        contents[data.src] = stringifyMap(require(path.resolve(data.src + 'map.json')));
    }

    return file('map.json', contents[data.src], { src: true });
}

// TODO shared with ymb
function stringifyMap (map) {
    var stack = [];

    _.each(map, function (moduleInfo) {
        stack.push('[' + _.map(moduleInfo, function (value, index) {
                if (index == 2 && value.slice(0, 8) == 'function') {
                    return value;
                } else if (index == 5 && typeof value == 'object') {
                    return '{ ' + _.map(value, function (v, k) { return k + ': ' + v; }).join(', ') + ' }';
                } else {
                    return '\'' + value + '\'';
                }
            }).join(', ') + ']');
    });

    return '[\n    ' + stack.join(',\n    ') + '\n]';
}

@@ -221,16 +221,122 @@
  = %7B%7D;%0A%0A
+function requireFresh (src) %7B%0A    delete require.cache%5Brequire.resolve(src)%5D;%0A    return require(src);%0A%7D%0A%0A
 /**%0A * @
@@ -526,16 +526,38 @@
     if (
+!data.cacheEnabled %7C%7C 
 !content
@@ -613,23 +613,41 @@
 gifyMap(
+%0A            
 require
+Fresh
 (path.re
@@ -675,16 +675,25 @@
 .json'))
+%0A        
 );%0A    %7D

Allow watching changes in modules map for ymb dev mode