"use strict";module.exports=Root;var Type,parse,common,Namespace=require("./namespace");((Root.prototype=Object.create(Namespace.prototype)).constructor=Root).className="Root";var Field=require("./field"),Enum=require("./enum"),OneOf=require("./oneof"),util=require("./util");function Root(e){Namespace.call(this,"",e),this.deferred=[],this.files=[]}function SYNC(){}Root.fromJSON=function(e,t){return t||(t=new Root),e.options&&t.setOptions(e.options),t.addJSON(e.nested)},Root.prototype.resolvePath=util.path.resolve,Root.prototype.fetch=util.fetch,Root.prototype.load=function e(t,o,n){"function"==typeof o&&(n=o,o=void 0);var r=this;if(!n)return util.asPromise(e,r,t,o);var i=n===SYNC;function s(e,t){if(n){if(i)throw e;var o=n;n=null,o(e,t)}}function a(e){var t=e.lastIndexOf("google/protobuf/");if(t>-1){var o=e.substring(t);if(o in common)return o}return null}function l(e,t){try{if(util.isString(t)&&"{"===t.charAt(0)&&(t=JSON.parse(t)),util.isString(t)){parse.filename=e;var n,l=parse(t,r,o),p=0;if(l.imports)for(;p<l.imports.length;++p)(n=a(l.imports[p])||r.resolvePath(e,l.imports[p]))&&f(n);if(l.weakImports)for(p=0;p<l.weakImports.length;++p)(n=a(l.weakImports[p])||r.resolvePath(e,l.weakImports[p]))&&f(n,!0)}else r.setOptions(t.options).addJSON(t.nested)}catch(e){s(e)}i||d||s(null,r)}function f(e,t){if(e=a(e)||e,!(r.files.indexOf(e)>-1)){if(r.files.push(e),e in common)return void(i?l(e,common[e]):(++d,setTimeout(function(){--d,l(e,common[e])})));if(i){var o;try{o=util.fs.readFileSync(e).toString("utf8")}catch(e){t||s(e);return}l(e,o)}else++d,r.fetch(e,function(o,i){if(--d,n){if(o)return void(t?d||s(null,r):s(o));l(e,i)}})}}var d=0;util.isString(t)&&(t=[t]);for(var p,u=0;u<t.length;++u)(p=r.resolvePath("",t[u]))&&f(p);if(i)return r;d||s(null,r)},Root.prototype.loadSync=function(e,t){if(!util.isNode)throw Error("not supported");return this.load(e,t,SYNC)},Root.prototype.resolveAll=function(){if(this.deferred.length)throw Error("unresolvable extensions: "+this.deferred.map(function(e){return"'extend "+e.extend+"' in "+e.parent.fullName}).join(", "));return Namespace.prototype.resolveAll.call(this)};var exposeRe=/^[A-Z]/;function tryHandleExtension(e,t){var o=t.parent.lookup(t.extend);if(o){var n=new Field(t.fullName,t.id,t.type,t.rule,void 0,t.options);return!!o.get(n.name)||(n.declaringField=t,t.extensionField=n,o.add(n),!0)}return!1}Root.prototype._handleAdd=function(e){if(e instanceof Field)void 0===e.extend||e.extensionField||tryHandleExtension(this,e)||this.deferred.push(e);else if(e instanceof Enum)exposeRe.test(e.name)&&(e.parent[e.name]=e.values);else if(!(e instanceof OneOf)){if(e instanceof Type)for(var t=0;t<this.deferred.length;)tryHandleExtension(this,this.deferred[t])?this.deferred.splice(t,1):++t;for(var o=0;o<e.nestedArray.length;++o)this._handleAdd(e._nestedArray[o]);exposeRe.test(e.name)&&(e.parent[e.name]=e)}},Root.prototype._handleRemove=function(e){if(e instanceof Field){if(void 0!==e.extend)if(e.extensionField)e.extensionField.parent.remove(e.extensionField),e.extensionField=null;else{var t=this.deferred.indexOf(e);t>-1&&this.deferred.splice(t,1)}}else if(e instanceof Enum)exposeRe.test(e.name)&&delete e.parent[e.name];else if(e instanceof Namespace){for(var o=0;o<e.nestedArray.length;++o)this._handleRemove(e._nestedArray[o]);exposeRe.test(e.name)&&delete e.parent[e.name]}},Root._configure=function(e,t,o){Type=e,parse=t,common=o};