import fs from 'fs';
var Errorable = require('./errorable');

function Generator(options, loc) {
  var json = {};
  this.errors = {};
  this.json = json;
  this._generate(json, options, loc);
}
Generator.prototype.capitalize = function(str) {
  return str[0].toUpperCase() + str.substring(1).toLowerCase();
};

Generator.prototype.getName = function(name, k) {
  name = name || '';
  return name + this.capitalize(k);
};

Generator.prototype.save = function(file) {
  fs.writeFileSync(file, JSON.stringify(this.json, null, 2));
};

Generator.prototype._generate = function(savor, data, locale, name) {
  if (name && data && data.messages) {
    if (!savor[name]) {
      savor[name] = {};
    }
    savor[name].messages = data.messages;
    if (data.code !== undefined) {
      savor[name].code = data.code;
    }
    this.errors[name] = new Errorable({
      name: name,
      messages: data.messages,
      code: data.code,
      locale: locale
    });
  }
  if (typeof data !== 'object') {
    throw Error('Parse JSON Error, Please check you Json Format at : ' + JSON.stringify(data));
  }
  for (var k in data) {
    if (data[k].alias) {
      this._generate(savor, data[data[k].alias], locale, this.getName(name, k));
      continue;
    }
    if (k === 'messages' || k === 'code') {
      continue;
    }
    this._generate(savor, data[k], locale, this.getName(name, k));
  }
};

module.exports = Generator;

@@ -1,23 +1,26 @@
-import fs from 
+var fs = require(
 'fs'
+)
 ;%0Ava
@@ -89,16 +89,25 @@
 ons, loc
+, toUpper
 ) %7B%0A  va
@@ -115,24 +115,103 @@
  json = %7B%7D;%0A
+  (typeof toUpper == 'boolean' && toUpper) ? toUpper = true : toUpper = false;%0A
   this.error
@@ -238,16 +238,42 @@
 = json;%0A
+  this.toUpper = toUpper;%0A
   this._
@@ -349,21 +349,42 @@
 tion(str
-) %7B%0A 
+,toUpper) %7B%0A  if(!toUpper)
  return 
@@ -438,16 +438,44 @@
 Case();%0A
+  return str.toUpperCase();%0A
 %7D;%0A%0AGene
@@ -516,16 +516,25 @@
 (name, k
+, toUpper
 ) %7B%0A  na
@@ -556,39 +556,152 @@
 ;%0A  
-return name + this.capitalize(k
+if(!toUpper) return name+ this.capitalize(k);%0A  return (name&&name!=='') ? name+%22_%22+ this.capitalize(k,toUpper) : name+this.capitalize(k,toUpper
 );%0A%7D
@@ -1270,24 +1270,47 @@
 'object') %7B%0A
+    console.log(data);%0A
     throw Er
@@ -1519,24 +1519,37 @@
 Name(name, k
+,this.toUpper
 ));%0A      co
@@ -1691,16 +1691,29 @@
 (name, k
+,this.toUpper
 ));%0A  %7D%0A

add toUpper option