"use strict";module.exports=verifier;var Enum=require("./enum"),util=require("./util");function invalid(e,i){return e.name+": "+i+(e.repeated&&"array"!==i?"[]":e.map&&"object"!==i?"{k:"+e.keyType+"}":"")+" expected"}function genVerifyValue(e,i,r,t){if(i.resolvedType)if(i.resolvedType instanceof Enum){e("switch(%s){",t)("default:")("return%j",invalid(i,"enum value"));for(var n=Object.keys(i.resolvedType.values),a=0;a<n.length;++a)e("case %i:",i.resolvedType.values[n[a]]);e("break")("}")}else".google.protobuf.Duration"===i.resolvedType.fullName||".google.protobuf.Timestamp"===i.resolvedType.fullName?e('if(typeof %s==="string")',t)(";")("else {")("var e=types[%i].verify(%s);",r,t)("if(e)")("return%j+e",i.name+".")("}"):e("{")("var e=types[%i].verify(%s);",r,t)("if(e)")("return%j+e",i.name+".")("}");else switch(i.type){case"int32":case"uint32":case"sint32":case"fixed32":case"sfixed32":e("if(!util.isInteger(%s))",t)("return%j",invalid(i,"integer"));break;case"int64":case"uint64":case"sint64":case"fixed64":case"sfixed64":e("if(!util.isInteger(%s)&&!(%s&&util.isInteger(%s.low)&&util.isInteger(%s.high)))",t,t,t,t)("return%j",invalid(i,"integer|Long"));break;case"float":case"double":e('if(typeof %s!=="number")',t)("return%j",invalid(i,"number"));break;case"bool":e('if(typeof %s!=="boolean")',t)("return%j",invalid(i,"boolean"));break;case"string":e("if(!util.isString(%s))",t)("return%j",invalid(i,"string"));break;case"bytes":e('if(!(%s&&typeof %s.length==="number"||util.isString(%s)))',t,t,t)("return%j",invalid(i,"buffer"))}return e}function genVerifyKey(e,i,r){switch(i.keyType){case"int32":case"uint32":case"sint32":case"fixed32":case"sfixed32":e("if(!util.key32Re.test(%s))",r)("return%j",invalid(i,"integer key"));break;case"int64":case"uint64":case"sint64":case"fixed64":case"sfixed64":e("if(!util.key64Re.test(%s))",r)("return%j",invalid(i,"integer|Long key"));break;case"bool":e("if(!util.key2Re.test(%s))",r)("return%j",invalid(i,"boolean key"))}return e}function verifier(e){var i=util.codegen(["m"],e.name+"$verify")('if(typeof m!=="object"||m===null)')("return%j","object expected"),r=e.oneofsArray,t={};r.length&&i("var p={}");for(var n=0;n<e.fieldsArray.length;++n){var a=e._fieldsArray[n].resolve(),s="m"+util.safeProp(a.name);if(a.optional&&i("if(%s!=null&&m.hasOwnProperty(%j)){",s,a.name),a.map)i("if(!util.isObject(%s))",s)("return%j",invalid(a,"object"))("var k=Object.keys(%s)",s)("for(var i=0;i<k.length;++i){"),genVerifyKey(i,a,"k[i]"),genVerifyValue(i,a,n,s+"[k[i]]")("}");else if(a.repeated)i("if(!Array.isArray(%s))",s)("return%j",invalid(a,"array"))("for(var i=0;i<%s.length;++i){",s),genVerifyValue(i,a,n,s+"[i]")("}");else{if(a.partOf){var l=util.safeProp(a.partOf.name);1===t[a.partOf.name]&&i("if(p%s===1)",l)("return%j",a.partOf.name+": multiple values"),t[a.partOf.name]=1,i("p%s=1",l)}genVerifyValue(i,a,n,s)}a.optional&&i("}")}return i("return null")}