"use strict";module.exports=Service;var Namespace=require("./namespace");((Service.prototype=Object.create(Namespace.prototype)).constructor=Service).className="Service";var Method=require("./method"),util=require("./util"),rpc=require("./rpc");function Service(e,t){Namespace.call(this,e,t),this.methods={},this._methodsArray=null}function clearCache(e){return e._methodsArray=null,e}Service.fromJSON=function(e,t){var r=new Service(e,t.options);if(t.methods)for(var o=Object.keys(t.methods),i=0;i<o.length;++i)r.add(Method.fromJSON(o[i],t.methods[o[i]]));return t.nested&&r.addJSON(t.nested),t.edition&&(r._edition=t.edition),r.comment=t.comment,r._defaultEdition="proto3",r},Service.prototype.toJSON=function(e){var t=Namespace.prototype.toJSON.call(this,e),r=!!e&&!!e.keepComments;return util.toObject(["edition",this._editionToJSON(),"options",t&&t.options||void 0,"methods",Namespace.arrayToJSON(this.methodsArray,e)||{},"nested",t&&t.nested||void 0,"comment",r?this.comment:void 0])},Object.defineProperty(Service.prototype,"methodsArray",{get:function(){return this._methodsArray||(this._methodsArray=util.toArray(this.methods))}}),Service.prototype.get=function(e){return util.getProp(this.methods,e)||Namespace.prototype.get.call(this,e)},Service.prototype.resolveAll=function(){if(!this._needsRecursiveResolve)return this;Namespace.prototype.resolve.call(this);for(var e=this.methodsArray,t=0;t<e.length;++t)e[t].resolve();return this},Service.prototype._resolveFeaturesRecursive=function(e){return this._needsRecursiveFeatureResolution&&(e=this._edition||e,Namespace.prototype._resolveFeaturesRecursive.call(this,e),this.methodsArray.forEach(t=>{t._resolveFeaturesRecursive(e)})),this},Service.prototype.add=function(e){if(this.get(e.name))throw Error("duplicate name '"+e.name+"' in "+this);return e instanceof Method?(this.methods[e.name]=e,e.parent=this,clearCache(this)):Namespace.prototype.add.call(this,e)},Service.prototype.remove=function(e){if(e instanceof Method){if(this.methods[e.name]!==e)throw Error(e+" is not a member of "+this);return delete this.methods[e.name],e.parent=null,clearCache(this)}return Namespace.prototype.remove.call(this,e)},Service.prototype.create=function(e,t,r){for(var o,i=new rpc.Service(e,t,r),s=0;s<this.methodsArray.length;++s){var a=util.lcFirst((o=this._methodsArray[s]).resolve().name).replace(/[^$\w_]/g,"");i[a]=util.codegen(["r","c"],util.isReserved(a)?a+"_":a)("return this.rpcCall(m,q,s,r,c)")({m:o,q:o.resolvedRequestType.ctor,s:o.resolvedResponseType.ctor})}return i};