var u=class extends Error{constructor(e){super(e),this.name="URIParseError"}},a=class extends u{constructor(e){super(`Invalid otpauth URI: ${e}`),this.name="InvalidURIError"}},g=class extends u{constructor(e){super(`Missing required parameter: ${e}`),this.name="MissingParameterError"}},p=class extends u{constructor(e,s){super(`Invalid value for parameter '${e}': ${s}`),this.name="InvalidParameterError"}};var d=2048,f=512,y=1024;function I(r,e){let s=r instanceof Error?r.message:String(r);return`Invalid URI encoding in ${e}: ${s}`}function h(r,e,s){if(r.length>e*3)throw new a(`${s} exceeds maximum length`);try{let t=decodeURIComponent(r);if(t.length>e)throw new a(`${s} exceeds maximum length of ${e} characters`);return t}catch(t){throw t instanceof a?t:new a(I(t,s))}}function b(r){if(r.length>d)throw new a(`URI exceeds maximum length of ${d} characters`);if(!r.startsWith("otpauth://"))throw new a(r);let e=r.slice(10),s=e.indexOf("/");if(s===-1)throw new a(r);let t=e.slice(0,s);if(t!=="hotp"&&t!=="totp")throw new p("type",t);let i=e.slice(s+1),o=i.indexOf("?"),n,c;o===-1?(n=h(i,f,"label"),c=""):(n=h(i.slice(0,o),f,"label"),c=i.slice(o+1));let l=O(c);if(!l.secret)throw new g("secret");return{type:t,label:n,params:l}}function O(r){let e={};if(!r)return e;let s=r.split("&");for(let t of s){let i=t.indexOf("=");if(i===-1)continue;let o=h(t.slice(0,i),64,"parameter key"),n=h(t.slice(i+1),y,`parameter '${o}'`);switch(o){case"secret":e.secret=n;break;case"issuer":e.issuer=n;break;case"algorithm":e.algorithm=T(n);break;case"digits":e.digits=w(n);break;case"counter":e.counter=P("counter",n,0);break;case"period":e.period=P("period",n,1);break}}return e}function P(r,e,s){if(!/^-?\d+$/.test(e))throw new p(r,e);let t=Number(e);if(!Number.isSafeInteger(t))throw new p(r,e);if(t<s)throw new p(r,e);return t}function T(r){let e=r.toLowerCase();if(e==="sha1"||e==="sha-1")return"sha1";if(e==="sha256"||e==="sha-256")return"sha256";if(e==="sha512"||e==="sha-512")return"sha512";throw new p("algorithm",r)}function w(r){if(!/^\d+$/.test(r))throw new p("digits",r);let e=Number(r);if(e===6||e===7||e===8)return e;throw new p("digits",r)}function m(r){let{type:e,label:s,params:t}=r,i=s.split(":").map(c=>encodeURIComponent(c)).join(":"),o=`otpauth://${e}/${i}?`,n=[];return t.secret&&n.push(`secret=${encodeURIComponent(t.secret)}`),t.issuer&&n.push(`issuer=${encodeURIComponent(t.issuer)}`),t.algorithm&&t.algorithm!=="sha1"&&n.push(`algorithm=${t.algorithm.toUpperCase()}`),t.digits&&t.digits!==6&&n.push(`digits=${t.digits}`),e==="hotp"&&t.counter!==void 0&&n.push(`counter=${t.counter}`),e==="totp"&&t.period!==void 0&&t.period!==30&&n.push(`period=${t.period}`),o+=n.join("&"),o}function x(r){let{issuer:e,label:s,secret:t,algorithm:i="sha1",digits:o=6,period:n=30}=r,c=e?`${e}:${s}`:s;return m({type:"totp",label:c,params:{secret:t,issuer:e,algorithm:i,digits:o,period:n}})}function U(r){let{issuer:e,label:s,secret:t,counter:i=0,algorithm:o="sha1",digits:n=6}=r,c=e?`${e}:${s}`:s;return m({type:"hotp",label:c,params:{secret:t,issuer:e,algorithm:o,digits:n,counter:i}})}export{p as InvalidParameterError,a as InvalidURIError,g as MissingParameterError,u as URIParseError,m as generate,U as generateHOTP,x as generateTOTP,b as parse,b as parseURI};
//# sourceMappingURL=index.js.map