"use strict";module.exports=Field;var Type,ReflectionObject=require("./object");((Field.prototype=Object.create(ReflectionObject.prototype)).constructor=Field).className="Field";var Enum=require("./enum"),types=require("./types"),util=require("./util"),ruleRe=/^required|optional|repeated$/;function Field(e,t,i,o,s,r,n){if(util.isObject(o)?(n=s,r=o,o=s=void 0):util.isObject(s)&&(n=r,r=s,s=void 0),ReflectionObject.call(this,e,r),!util.isInteger(t)||t<0)throw TypeError("id must be a non-negative integer");if(!util.isString(i))throw TypeError("type must be a string");if(void 0!==o&&!ruleRe.test(o=o.toString().toLowerCase()))throw TypeError("rule must be a string rule");if(void 0!==s&&!util.isString(s))throw TypeError("extend must be a string");"proto3_optional"===o&&(o="optional"),this.rule=o&&"optional"!==o?o:void 0,this.type=i,this.id=t,this.extend=s||void 0,this.repeated="repeated"===o,this.map=!1,this.message=null,this.partOf=null,this.typeDefault=null,this.defaultValue=null,this.long=!!util.Long&&void 0!==types.long[i],this.bytes="bytes"===i,this.resolvedType=null,this.extensionField=null,this.declaringField=null,this.comment=n}Field.fromJSON=function(e,t){var i=new Field(e,t.id,t.type,t.rule,t.extend,t.options,t.comment);return t.edition&&(i._edition=t.edition),i._defaultEdition="proto3",i},Object.defineProperty(Field.prototype,"required",{get:function(){return"LEGACY_REQUIRED"===this._features.field_presence}}),Object.defineProperty(Field.prototype,"optional",{get:function(){return!this.required}}),Object.defineProperty(Field.prototype,"delimited",{get:function(){return this.resolvedType instanceof Type&&"DELIMITED"===this._features.message_encoding}}),Object.defineProperty(Field.prototype,"packed",{get:function(){return"PACKED"===this._features.repeated_field_encoding}}),Object.defineProperty(Field.prototype,"hasPresence",{get:function(){return!this.repeated&&!this.map&&(this.partOf||this.declaringField||this.extensionField||"IMPLICIT"!==this._features.field_presence)}}),Field.prototype.setOption=function(e,t,i){return ReflectionObject.prototype.setOption.call(this,e,t,i)},Field.prototype.toJSON=function(e){var t=!!e&&!!e.keepComments;return util.toObject(["edition",this._editionToJSON(),"rule","optional"!==this.rule&&this.rule||void 0,"type",this.type,"id",this.id,"extend",this.extend,"options",this.options,"comment",t?this.comment:void 0])},Field.prototype.resolve=function(){if(this.resolved)return this;if(void 0===(this.typeDefault=types.defaults[this.type])?(this.resolvedType=(this.declaringField?this.declaringField.parent:this.parent).lookupTypeOrEnum(this.type),this.resolvedType instanceof Type?this.typeDefault=null:this.typeDefault=this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]):this.options&&this.options.proto3_optional&&(this.typeDefault=null),this.options&&null!=this.options.default&&(this.typeDefault=this.options.default,this.resolvedType instanceof Enum&&"string"==typeof this.typeDefault&&(this.typeDefault=this.resolvedType.values[this.typeDefault])),this.options&&(void 0===this.options.packed||!this.resolvedType||this.resolvedType instanceof Enum||delete this.options.packed,Object.keys(this.options).length||(this.options=void 0)),this.long)this.typeDefault=util.Long.fromNumber(this.typeDefault,"u"===this.type.charAt(0)),Object.freeze&&Object.freeze(this.typeDefault);else if(this.bytes&&"string"==typeof this.typeDefault){var e;util.base64.test(this.typeDefault)?util.base64.decode(this.typeDefault,e=util.newBuffer(util.base64.length(this.typeDefault)),0):util.utf8.write(this.typeDefault,e=util.newBuffer(util.utf8.length(this.typeDefault)),0),this.typeDefault=e}return this.map?this.defaultValue=util.emptyObject:this.repeated?this.defaultValue=util.emptyArray:this.defaultValue=this.typeDefault,this.parent instanceof Type&&(this.parent.ctor.prototype[this.name]=this.defaultValue),ReflectionObject.prototype.resolve.call(this)},Field.prototype._inferLegacyProtoFeatures=function(e){if("proto2"!==e&&"proto3"!==e)return{};var t={};if("required"===this.rule&&(t.field_presence="LEGACY_REQUIRED"),this.parent&&void 0===types.defaults[this.type]){var i=this.parent.get(this.type.split(".").pop());i&&i instanceof Type&&i.group&&(t.message_encoding="DELIMITED")}return!0===this.getOption("packed")?t.repeated_field_encoding="PACKED":!1===this.getOption("packed")&&(t.repeated_field_encoding="EXPANDED"),t},Field.prototype._resolveFeatures=function(e){return ReflectionObject.prototype._resolveFeatures.call(this,this._edition||e)},Field.d=function(e,t,i,o){return"function"==typeof t?t=util.decorateType(t).name:t&&"object"==typeof t&&(t=util.decorateEnum(t).name),function(s,r){util.decorateType(s.constructor).add(new Field(r,e,t,i,{default:o}))}},Field._configure=function(e){Type=e};