(function(u,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(u=typeof globalThis<"u"?globalThis:u||self,h(u["ff-request"]={}))})(this,function(u){"use strict";var Ae=Object.defineProperty;var Ce=(u,h,b)=>h in u?Ae(u,h,{enumerable:!0,configurable:!0,writable:!0,value:b}):u[h]=b;var y=(u,h,b)=>(Ce(u,typeof h!="symbol"?h+"":h,b),b),ve=(u,h,b)=>{if(!h.has(u))throw TypeError("Cannot "+b)};var i=(u,h,b)=>(ve(u,h,"read from private field"),b?b.call(u):h.get(u)),w=(u,h,b)=>{if(h.has(u))throw TypeError("Cannot add the same private member more than once");h instanceof WeakSet?h.add(u):h.set(u,b)},N=(u,h,b,V)=>(ve(u,h,"write to private field"),V?V.call(u,b):h.set(u,b),b);var L,z,J,D,ee,te,Y,re,ne,Q,W,K,k,se,$,I,q,ie,X;function h(r){return r&&r.__esModule&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r}var b={};(function(r){(function(e){e([],function(){var t=-1,n=-2,s=-3,o=-4,f=-5,j=function(g,l){l=l||{};var E=l.verbose||!1;E&&console.log("Normalize the JSON Object"),g=typeof g=="string"?this.JSON.parse(g):g,E&&console.log("Creating a empty dictionary");var d={strings:[],integers:[],floats:[]};E&&console.log("Creating the AST");var p=function x(c){E&&console.log("Calling recursiveAstBuilder with "+this.JSON.stringify(c));var T=typeof c;if(c===null)return{type:"null",index:s};if(typeof c>"u")return{type:"undefined",index:f};if(c instanceof Array){var _=["@"];for(var O in c)c.hasOwnProperty(O)&&_.push(x(c[O]));return _}if(T==="object"){var _=["$"];for(var F in c)c.hasOwnProperty(F)&&(_.push(x(F)),_.push(x(c[F])));return _}if(c==="")return{type:"empty",index:o};if(T==="string"){var S=G.call(d.strings,c);return S==-1&&(d.strings.push(C(c)),S=d.strings.length-1),{type:"strings",index:S}}if(T==="number"&&c%1===0){var S=G.call(d.integers,c);return S==-1&&(d.integers.push(M(c)),S=d.integers.length-1),{type:"integers",index:S}}if(T==="number"){var S=G.call(d.floats,c);return S==-1&&(d.floats.push(c),S=d.floats.length-1),{type:"floats",index:S}}if(T==="boolean")return{type:"boolean",index:c?t:n};throw new Error("Unexpected argument of type "+typeof c)}(g),P=d.strings.length,H=d.integers.length;d.floats.length,E&&console.log("Parsing the dictionary");var U=d.strings.join("|");return U+="^"+d.integers.join("|"),U+="^"+d.floats.join("|"),E&&console.log("Parsing the structure"),U+="^"+function x(c){if(E&&console.log("Calling a recursiveParser with "+this.JSON.stringify(c)),c instanceof Array){var T=c.shift();for(var _ in c)c.hasOwnProperty(_)&&(T+=x(c[_])+"|");return(T[T.length-1]==="|"?T.slice(0,-1):T)+"]"}var O=c.type,F=c.index;if(O==="strings")return M(F);if(O==="integers")return M(P+F);if(O==="floats")return M(P+H+F);if(O==="boolean")return c.index;if(O==="null")return s;if(O==="undefined")return f;if(O==="empty")return o;throw new TypeError("The item is alien!")}(p),E&&console.log("Ending parser"),l.debug?{dictionary:d,ast:p,packed:U}:U},a=function(g,l){l=l||{};var E=g.split("^");l.verbose&&console.log("Building dictionary");var d=[],p=E[0];if(p!==""){p=p.split("|"),l.verbose&&console.log("Parse the strings dictionary");for(var P=0,H=p.length;Pi(this,ee).call(this,t),ownKeys:()=>Object.keys(i(this,D).getPrototypeOf()||{}),getPrototypeOf:()=>i(this,z)?this:this.data,getOwnPropertyDescriptor:()=>({configurable:!0,enumerable:!0,writable:!0,value:i(this,D).getPrototypeOf()})});w(this,ee,e=>e==="$echoMsg"?i(this,Y):e==="msg"?i(this,te):e==="then"||e==="resp"?(N(this,J,!0),N(this,z,e==="resp"),i(this,re)):e==="catch"?i(this,ne):e==="IS_ECHO_MSG"?i(this,J):Reflect.get(this,e));w(this,te,(e=n=>n,t=!1)=>(i(this,Y).call(this),N(this,z,t),Promise.resolve(e==null?void 0:e(i(this,D).getPrototypeOf()))));w(this,Y,()=>{i(this,J)&&(N(this,J,!1),pe.onMsg(this.code,this.message))});w(this,re,e=>[0,1].includes(this.code)?Promise.resolve(e==null?void 0:e(i(this,D).getPrototypeOf())):Promise.reject(i(this,L)));w(this,ne,e=>Promise.resolve(e(i(this,L))));return this.code=e,this.message=t,this.data=n,this.url=s,this.res=o,N(this,L,new Proxy(this,i(this,D)))}}L=new WeakMap,z=new WeakMap,J=new WeakMap,D=new WeakMap,ee=new WeakMap,te=new WeakMap,Y=new WeakMap,re=new WeakMap,ne=new WeakMap;const ae=(r,e,t,n="/",s="")=>new oe(r,e,t,n,s);var le=["utf8","utf-8","unicode-1-1-utf-8"];function ye(r){if(le.indexOf(r)<0&&typeof r<"u"&&r!=null)throw new RangeError("Invalid encoding type. Only utf-8 is supported");this.encoding="utf-8",this.encode=function(e){if(typeof e!="string")throw new TypeError("passed argument must be of tye string");var t=unescape(encodeURIComponent(e)),n=new Uint8Array(t.length);const s=t.split("");for(let o=0;o"u")return"";var n=typeof t<"u"&&n in t?t.stream:!1;if(typeof n!="boolean")throw new TypeError("stream option must be boolean");if(ArrayBuffer.isView(e)){var s=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),o=new Array(s.length);for(let f=0;f{let e=5381;for(let t=0;t>>0).toString(16)},he=(r,e)=>r.replace(new RegExp(`^${e}+|${e}+$`,"g"),""),de=r=>be.encode(r||""),Te=r=>Ee.decode(new DataView(r),{}),Oe=62,Z=30,ge=31,Pe=63;class Se{constructor(e){y(this,"encodeTable",[]);y(this,"decodeMap",new Uint8Array(256).fill(255));if(e.length!==Oe)throw new Error("Encoding alphabet must be 62 characters long");if(e.includes(` `)||e.includes("\r"))throw new Error("Encoding alphabet contains newline character");this.encodeTable=e.split(""),this.decodeMap=new Uint8Array(256).fill(255);for(let t=0;t>3;e===0&&(t-=1,e=8);let n=this.src[t]>>8-e;return e<6&&t>0&&(n|=this.src[t-1]<0;){let n=6,s=this.get6bits();(s&Z)===Z&&((this.pos>6||s>ge)&&(n=5),s&=ge),t.push(i(this,Q).encodeTable[s].charCodeAt(0)),this.pos-=n}return t}}Q=new WeakMap;class _e{constructor(e){w(this,W,null);N(this,W,e)}decode(e){const t=new Uint8Array(Math.ceil(e.length*6/8)+1);let n=t.length,s=0,o=0;for(let f=0;f=8&&(n--,t[n]=o&255,s%=8,o>>=8)}return s>0&&(n--,t[n]=o&255),t.slice(n)}}W=new WeakMap,Promise.withResolvers||(Promise.withResolvers=function(){let r,e;return{promise:new Promise((n,s)=>{r=n,e=s}),resolve:r,reject:e}});class ce{constructor(e,t,n){w(this,K,void 0);w(this,k,void 0);w(this,se,void 0);w(this,$,{"#":new Map,$:new Map});w(this,I,new Map);w(this,q,new Set);y(this,"init",(e,t,n)=>{N(this,se,e),N(this,K,n),N(this,k,new Se(t)),i(this,ie).call(this)});w(this,ie,()=>{!i(this,q).size||!i(this,K)||(i(this,q).forEach(e=>i(this,X).call(this,...e)),i(this,q).clear())});w(this,X,(e,t,n,s=!1)=>{if(!i(this,K))i(this,q).add([e,t,n,s]);else{const{url:o,method:f,...j}=n||{};i(this,K).call(this,{url:o,method:f,...j}).then(a=>{if(![0,1].includes(a.code))throw ae(a.code,a.msg,a.data,o,a==null?void 0:a.res);return[a.code,a.msg,a.data,o,a==null?void 0:a.res]}).then(a=>((t=="#"||f=="GET")&&i(this,$)[t].set(e,a),t!="#"&&f=="GET"&&!s&&setTimeout(()=>{i(this,$)[t].delete(e)},300),a)).then(a=>{var C;return(C=i(this,I).get(e))==null?void 0:C.forEach((R,M,B)=>{R&&R[0](a),delete B[M]})}).catch(a=>{var C;i(this,$)[t].delete(e),(C=i(this,I).get(e))==null||C.forEach((R,M,B)=>{R&&R[1](a instanceof oe?a:ae(-1,a,null,o)),delete B[M]})}).finally(()=>i(this,I).delete(e))}});y(this,"request",({url:e="/",method:t="GET",...n},s=!1)=>{var M;e=he(e,"/");const o=t.toUpperCase(),f=fe(JSON.stringify([e,o,n])),j=(M=e==null?void 0:e.includes)!=null&&M.call(e,"/_/")?"#":"$",{promise:a,resolve:C,reject:R}=Promise.withResolvers();return i(this,$)[j].has(f)?(console.log("cache",e),C(i(this,$)[j].get(f))):i(this,I).has(f)?i(this,I).get(f).push([C,R]):(i(this,I).set(f,[[C,R]]),i(this,X).call(this,f,j,{url:e,method:o,...n},s)),new Proxy(a,{get:(B,G)=>(...g)=>B.then(l=>ae(...l)[G](...g)).catch(l=>{if(!l instanceof oe)throw l;return typeof l[G]=="function"?l[G](...g):l[G]})})});y(this,"get",(e,t={})=>this.request({url:e,method:"GET",params:t}));y(this,"post",(e,t={},n={})=>this.request({url:e,method:"POST",data:t,params:n}));y(this,"put",(e,t={},n={})=>this.request({url:e,method:"PUT",data:t,params:n}));y(this,"del",(e,t={},n={})=>this.request({url:e,method:"DELETE",data:t,params:n}));y(this,"download",(e,t)=>{});y(this,"cache",(e,t={})=>this.request({url:e,method:"GET",params:t},!0));y(this,"list",(e,t={})=>this.get(`/api/${e}/${this.encode(t)}`));y(this,"getAppInfo",()=>{});y(this,"refreshCache",(e=!1)=>{e&&i(this,$)["#"].clear(),i(this,$).$.clear()});y(this,"decode",(e="",t={})=>{if(e===void 0||e===""||e===null)return t;try{const n=i(this,k).decode(e);return V.unpack(n)}catch{return console.warn("core.decode",e),t}});y(this,"encode",(e={})=>{const t=JSON.stringify(e,(n,s)=>s===void 0?null:s);return i(this,k).encode(V.pack(t))});this.init(e,t,n)}}K=new WeakMap,k=new WeakMap,se=new WeakMap,$=new WeakMap,I=new WeakMap,q=new WeakMap,ie=new WeakMap,X=new WeakMap;const pe=ce;ce.onUnhandledRejection=r=>{var e,t,n,s,o,f;r!=null&&r.IS_ECHO_MSG?((e=r==null?void 0:r.$echoMsg)==null||e.call(r),(t=r==null?void 0:r.preventDefault)==null||t.call(r)):(n=r.reason)!=null&&n.IS_ECHO_MSG&&((o=(s=r.reason)==null?void 0:s.$echoMsg)==null||o.call(s),(f=r==null?void 0:r.preventDefault)==null||f.call(r))},ce.onMsg=(r,e)=>[0,1].includes(r)?console.log(e):console.warn(e),u.default=pe,u.signature=fe,u.str2uint8array=de,u.trim=he,u.uint8array2str=Te,Object.defineProperties(u,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});