"use strict";module.exports=Writer;var BufferWriter,util=require("./util/minimal"),LongBits=util.LongBits,base64=util.base64,utf8=util.utf8;function Op(t,i,r){this.fn=t,this.len=i,this.next=void 0,this.val=r}function noop(){}function State(t){this.head=t.head,this.tail=t.tail,this.len=t.len,this.next=t.states}function Writer(){this.len=0,this.head=new Op(noop,0,0),this.tail=this.head,this.states=null}var create=function(){return util.Buffer?function(){return(Writer.create=function(){return new BufferWriter})()}:function(){return new Writer}};function writeByte(t,i,r){i[r]=255&t}function writeVarint32(t,i,r){for(;t>127;)i[r++]=127&t|128,t>>>=7;i[r]=t}function VarintOp(t,i){this.len=t,this.next=void 0,this.val=i}function writeVarint64(t,i,r){for(;t.hi;)i[r++]=127&t.lo|128,t.lo=(t.lo>>>7|t.hi<<25)>>>0,t.hi>>>=7;for(;t.lo>127;)i[r++]=127&t.lo|128,t.lo=t.lo>>>7;i[r++]=t.lo}function writeFixed32(t,i,r){i[r]=255&t,i[r+1]=t>>>8&255,i[r+2]=t>>>16&255,i[r+3]=t>>>24}Writer.create=create(),Writer.alloc=function(t){return new util.Array(t)},util.Array!==Array&&(Writer.alloc=util.pool(Writer.alloc,util.Array.prototype.subarray)),Writer.prototype._push=function(t,i,r){return this.tail=this.tail.next=new Op(t,i,r),this.len+=i,this},VarintOp.prototype=Object.create(Op.prototype),VarintOp.prototype.fn=writeVarint32,Writer.prototype.uint32=function(t){return this.len+=(this.tail=this.tail.next=new VarintOp((t>>>=0)<128?1:t<16384?2:t<2097152?3:t<0x10000000?4:5,t)).len,this},Writer.prototype.int32=function(t){return t<0?this._push(writeVarint64,10,LongBits.fromNumber(t)):this.uint32(t)},Writer.prototype.sint32=function(t){return this.uint32((t<<1^t>>31)>>>0)},Writer.prototype.uint64=function(t){var i=LongBits.from(t);return this._push(writeVarint64,i.length(),i)},Writer.prototype.int64=Writer.prototype.uint64,Writer.prototype.sint64=function(t){var i=LongBits.from(t).zzEncode();return this._push(writeVarint64,i.length(),i)},Writer.prototype.bool=function(t){return this._push(writeByte,1,+!!t)},Writer.prototype.fixed32=function(t){return this._push(writeFixed32,4,t>>>0)},Writer.prototype.sfixed32=Writer.prototype.fixed32,Writer.prototype.fixed64=function(t){var i=LongBits.from(t);return this._push(writeFixed32,4,i.lo)._push(writeFixed32,4,i.hi)},Writer.prototype.sfixed64=Writer.prototype.fixed64,Writer.prototype.float=function(t){return this._push(util.float.writeFloatLE,4,t)},Writer.prototype.double=function(t){return this._push(util.float.writeDoubleLE,8,t)};var writeBytes=util.Array.prototype.set?function(t,i,r){i.set(t,r)}:function(t,i,r){for(var e=0;e<t.length;++e)i[r+e]=t[e]};Writer.prototype.bytes=function(t){var i=t.length>>>0;if(!i)return this._push(writeByte,1,0);if(util.isString(t)){var r=Writer.alloc(i=base64.length(t));base64.decode(t,r,0),t=r}return this.uint32(i)._push(writeBytes,i,t)},Writer.prototype.string=function(t){var i=utf8.length(t);return i?this.uint32(i)._push(utf8.write,i,t):this._push(writeByte,1,0)},Writer.prototype.fork=function(){return this.states=new State(this),this.head=this.tail=new Op(noop,0,0),this.len=0,this},Writer.prototype.reset=function(){return this.states?(this.head=this.states.head,this.tail=this.states.tail,this.len=this.states.len,this.states=this.states.next):(this.head=this.tail=new Op(noop,0,0),this.len=0),this},Writer.prototype.ldelim=function(){var t=this.head,i=this.tail,r=this.len;return this.reset().uint32(r),r&&(this.tail.next=t.next,this.tail=i,this.len+=r),this},Writer.prototype.finish=function(){for(var t=this.head.next,i=this.constructor.alloc(this.len),r=0;t;)t.fn(t.val,i,r),r+=t.len,t=t.next;return i},Writer._configure=function(t){BufferWriter=t,Writer.create=create(),BufferWriter._configure()};