/*
 * gult-stubby
 * https://github.com/felixzapata/gulp-pug-i18n
 *
 * Copyright (c) 2016 Félix Zapata
 * Licensed under the MIT license.
 */


'use strict';

var R = require('ramda');
var pug = require('pug');
var glob = require('glob');
var fs = require('fs');
var gutil = require('gulp-util');
var ext = require('gulp-util').replaceExtension;
var PluginError = require('gulp-util').PluginError;
var path = require('path');
var YAML = require('js-yaml');
var through = require('through2');
var PLUGIN_NAME = 'gulp-pug-i18n';


function getExtension(filepath) {
    return path.extname(filepath);
}

function setExtension(ext) {
    if (ext.charAt(0) !== '.') {
        ext = '.' + ext;
    }
    return ext;
}

function readYAML(filepath, options) {
    var src = fs.readFileSync(filepath, options);
    var result;
    try {
        result = YAML.load(src);
        return result;
    } catch (e) {
        throw new gutil.PluginError(PLUGIN_NAME, 'Unable to parse "' + filepath + '" file (' + e.problem + ').', e);
    }
}

function readJSON(filepath, options) {
    var src = fs.readFileSync(filepath, options);
    var result;
    try {
        result = JSON.parse(src);
        return result;
    } catch (e) {
        throw new gutil.PluginError(PLUGIN_NAME, 'Unable to parse "' + filepath + '" file (' + e.message + ').', e);
    }
}

function addLocaleExtensionDest(obj, locale, outputExt) {

    var files = glob.sync(path.join(obj.cwd, obj.src));
    return _.map(files, function (file) {

        var dest, ext;

        locale = locale.toLowerCase();
        ext = getExtension(file);


        function getBaseName() {
            return path.basename(file).split('.')[0];
        }

        if (ext) {
            dest = path.join(obj.dest, path.basename(file, ext) + '.' + locale);
        } else {
            dest = path.join(obj.dest, getBaseName() + '.' + locale);
        }

        if (obj.ext) {
            dest += setExtension(obj.ext);
        } else {
            dest += setExtension(outputExt);
        }

        return dest;
    });
}

function addLocaleDirnameDest(file, locale, outputExt) {
    var base, dest, ext = getExtension(file);
    if (ext) {
        dest = path.join(path.dirname(file), locale, path.basename(file, ext) + setExtension(ext));
    } else {
        if (/(\/|\*+)$/i.test(file)) {
            base = file.split('/');
            dest = path.join(path.join.apply(null, base.slice(0, -1)), locale, base.slice(-1)[0]);
        } else {
            dest = path.join(file, locale);
        }
    }
    dest = dest.replace(/\.jade$/i, setExtension(outputExt));
    return dest;
}

function readFile(filepath) {
    var data;
    try {
        if (/(\.yaml|\.yml)$/i.test(filepath)) {
            data = readYAML(filepath);
        } else if (/\.js$/i.test(filepath)) {
            data = require(path.resolve(filepath));
        } else {
            data = readJSON(filepath);
        }
    } catch (_error) {
        throw new gutil.PluginError("Cannot parse file '" + filepath + "': " + _error.message, _error);
    }
    return data;
}

function plugI18nPlugin(customOptions) {

    var defaultOptions = {};
    var options = customOptions ? Object.assign(defaultOptions, customOptions) : defaultOptions;
    var locales = options.i18n.locales;


    var bufferContents = function (file, enc, cb) {

        if (file.isNull()) {
            cb(null, file);
            return;
        }

        if (file.isStream()) {
            driver.quit();
            cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
            return;
        }

        if (locales && locales.length) {

            pug.compileFile(file.path)(locales);

        } else {
            gutil.log('Locales files not found. Nothing to translate');
        }
    }
    return through.obj(bufferContents);
}

module.exports = plugI18nPlugin;

@@ -3109,23 +3109,17 @@
 nPlugin(
-customO
+o
 ptions) 
@@ -3140,118 +3140,553 @@
 ault
-Options = %7B%7D;%0A    var options = customOptions ? Object.assign(defaultOptions, customOptions) : defaultOptions;
+Ext;%0A    var localeExtension; %0A    var namespace;%0A    var filePath;%0A    var fileExt;%0A    var locale;%0A%0A    if (!options.i18n) %7B%0A        options.i18n = %7B%7D;%0A    %7D else %7B%0A        locales = options.i18n.locales;%0A        namespace = options.i18n.namespace;%0A        localeExtension = options.i18n.localeExtension;%0A        defaultExt = options.i18n.defaultExt;%0A    %7D%0A%0A    if (!namespace) %7B%0A        namespace = '$i18n';%0A    %7D%0A%0A    if (!localeExtension) %7B%0A        localeExtension = false;%0A    %7D%0A%0A    if (!defaultExt) %7B%0A        defaultExt = '.html';%0A    %7D%0A
 %0A   
@@ -3692,32 +3692,42 @@
   var locales = 
+glob.sync(
 options.i18n.loc
@@ -3730,16 +3730,17 @@
 .locales
+)
 ;%0A%0A%0A    
@@ -4102,44 +4102,663 @@
     
-pug.compileFile(file.path)(locales);
+for(var i = 0, len = locales.length; i %3C len; i++) %7B%0A                filePath = locales%5Bi%5D;%0A                fileExt = filePath.split('.').slice(-1)%5B0%5D;%0A                locale = path.basename(filePath, '.' + fileExt);%0A                gutil.log('Loading locale ' + locale);%0A                gutil.log('Reading translation data: ' + filePath);%0A                if (!options.data) %7B%0A                    options.data = %7B%7D;%0A                %7D%0A                options.data%5Bnamespace%5D = readFile(filePath);%0A                //options.data.$localeName = locale;%0A                //fs.writeFileSync(dest, pug.compileFile(file.path, options)(options.data));%0A            %7D
 %0A%0A  

read locales files and set the options