"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=[],this._edition="proto2",this._fullyQualifiedObjects={}}function SYNC(){}Root.fromJSON=function(e,t){return t||(t=new Root),e.options&&t.setOptions(e.options),t.addJSON(e.nested).resolveAll()},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 i=this;if(!n)return util.asPromise(e,i,t,o);var r=n===SYNC;function s(e,t){if(n){if(r)throw e;t&&t.resolveAll();var o=n;n=null,o(e,t)}}function l(e){var t=e.lastIndexOf("google/protobuf/");if(t>-1){var o=e.substring(t);if(o in common)return o}return null}function a(e,t){try{if(util.isString(t)&&"{"===t.charAt(0)&&(t=JSON.parse(t)),util.isString(t)){parse.filename=e;var n,a=parse(t,i,o),u=0;if(a.imports)for(;u<a.imports.length;++u)(n=l(a.imports[u])||i.resolvePath(e,a.imports[u]))&&f(n);if(a.weakImports)for(u=0;u<a.weakImports.length;++u)(n=l(a.weakImports[u])||i.resolvePath(e,a.weakImports[u]))&&f(n,!0)}else i.setOptions(t.options).addJSON(t.nested)}catch(e){s(e)}r||d||s(null,i)}function f(e,t){if(e=l(e)||e,!(i.files.indexOf(e)>-1)){if(i.files.push(e),e in common)return void(r?a(e,common[e]):(++d,setTimeout(function(){--d,a(e,common[e])})));if(r){var o;try{o=util.fs.readFileSync(e).toString("utf8")}catch(e){t||s(e);return}a(e,o)}else++d,i.fetch(e,function(o,r){if(--d,n){if(o)return void(t?d||s(null,i):s(o));a(e,r)}})}}var d=0;util.isString(t)&&(t=[t]);for(var u,p=0;p<t.length;++p)(u=i.resolvePath("",t[p]))&&f(u);return r?i.resolveAll():d||s(null,i),i},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._needsRecursiveResolve)return this;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)}(e instanceof Type||e instanceof Enum||e instanceof Field)&&(this._fullyQualifiedObjects[e.fullName]=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]}delete this._fullyQualifiedObjects[e.fullName]},Root._configure=function(e,t,o){Type=e,parse=t,common=o};