var V=Object.defineProperty,Y=Object.defineProperties;var z=Object.getOwnPropertyDescriptors;var j=Object.getOwnPropertySymbols;var Z=Object.prototype.hasOwnProperty,X=Object.prototype.propertyIsEnumerable;var H=i=>{throw TypeError(i)};var F=(i,e,t)=>e in i?V(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,T=(i,e)=>{for(var t in e||(e={}))Z.call(e,t)&&F(i,t,e[t]);if(j)for(var t of j(e))X.call(e,t)&&F(i,t,e[t]);return i},k=(i,e)=>Y(i,z(e));var _=(i,e,t)=>e.has(i)||H("Cannot "+t);var s=(i,e,t)=>(_(i,e,"read from private field"),t?t.call(i):e.get(i)),u=(i,e,t)=>e.has(i)?H("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(i):e.set(i,t),g=(i,e,t,r)=>(_(i,e,"write to private field"),r?r.call(i,t):e.set(i,t),t);var l=(i,e,t)=>new Promise((r,o)=>{var n=p=>{try{h(t.next(p))}catch(f){o(f)}},c=p=>{try{h(t.throw(p))}catch(f){o(f)}},h=p=>p.done?r(p.value):Promise.resolve(p.value).then(n,c);h((t=t.apply(i,e)).next())});import{v4 as ce}from"uuid";import W from"crypto-js/enc-base64";import ee from"crypto-js/enc-hex";import te from"crypto-js/hmac-sha256";import ie from"crypto-js/sha256";import re from"object-hash";import{ofetch as ne}from"ofetch";var E=class extends Error{constructor(t,r){super(r);this.name="LumiError";this.code=t}};var se="6QrJZ7pFCmBZAeIJF7IArvkCz+EtzA0RVcpHkiQIsQyhs7QtCS9P+CueZdHfB2OtJcgX3BbqY9pfpWeAVTqCwQ==";function K(i){return encodeURIComponent(i).replace(/[!'()*]/g,e=>`%${e.charCodeAt(0).toString(16).toUpperCase()}`)}function oe(i){return e=>{let{options:t}=e,r=Math.floor(Date.now()/1e3).toString(),o=Math.random().toString(36).substring(2,15),n=T({},t.query),c=Object.keys(n).sort().map(w=>`${K(w)}=${K(String(n[w]))}`).join("&"),h={"x-timestamp":r,"x-nonce":o},p=Object.keys(h).sort().map(w=>`${w}:${h[w]}`).join(` `),f=t.body&&!(t.body instanceof FormData)?JSON.stringify(t.body):"",I=ie(f).toString(ee),L=[c,p,I].join(` `),d=W.stringify(te(L,i)),C=new Headers(t.headers);Object.entries(h).forEach(([w,G])=>{C.set(w,G)}),C.set("X-Sign",d),t.headers=C}}var B=new Map;function ae(i,e){var n;e.body instanceof FormData&&(e=k(T({},e),{body:Array.from(e.body.entries())}));let t=re([i,e]),r=Date.now(),o=((n=B.get(t))==null?void 0:n.filter(c=>r-c<1e3))||[];if(o.length>=4)throw new E(429,"Too many requests");o.push(r),B.set(t,o)}function a(i,e,t={}){i.auth.accessToken&&(t.headers=T({Authorization:`Bearer ${i.auth.accessToken}`},t.headers)),ae(e,t);let r=i.auth.isAuthenticated;return ne(e,k(T({baseURL:i.config.apiBaseUrl},t),{onRequest:oe(se),onResponse:({response:o})=>{var n;r&&((n=o._data)==null?void 0:n.code)===2100&&i.auth.signOut()}}))}function Q(){var i,e;return(e=(i=document.querySelector('link[rel="icon"]'))==null?void 0:i.href)!=null?e:null}function J(){var i;return(i=document.title)!=null?i:null}function q(i,e,t=localStorage){let r=t.getItem(i),o=e?JSON.stringify(e):null;o?t.setItem(i,o):t.removeItem(i),window.dispatchEvent(new StorageEvent("storage",{key:i,oldValue:r,newValue:o,storageArea:t}))}function N(i,e=localStorage){let t=e.getItem(i);try{return t?JSON.parse(t):null}catch(r){return null}}var y,x,R=class{constructor(e){u(this,y);u(this,x,`lumi-auth-${ce()}`);g(this,y,e),Promise.resolve().then(()=>{this.isAuthenticated&&this.refreshUser()})}get accessToken(){return N("lumi-access-token")}set accessToken(e){q("lumi-access-token",e)}get user(){return N("lumi-user")}set user(e){q("lumi-user",e)}get isAuthenticated(){return!!this.accessToken}signIn(){let r=(window.screen.width-800)/2,o=(window.screen.height-600)/2,n=window.open(s(this,y).config.authOrigin,s(this,x),`width=800,height=600,left=${r},top=${o}`),c;return new Promise((h,p)=>{if(!n)return p(new Error("Open auth window failed"));let f=setInterval(()=>{n.closed&&p(new Error("Auth window closed"))},1e3),I=d=>{n.closed||(n.focus(),d.stopPropagation(),d.preventDefault())},L=({data:d,origin:C,source:w})=>{if(!(C!==s(this,y).config.authOrigin||w!==n))switch(d==null?void 0:d.type){case"lumi-ready":{n.postMessage({type:"lumi-init",data:{projectId:s(this,y).config.projectId,icon:Q(),title:J()}},s(this,y).config.authOrigin);break}case"lumi-sign-in":{if(d.data.projectId!==s(this,y).config.projectId)break;n.close(),window.focus(),this.accessToken=d.data.accessToken,this.user=d.data.user,h(d.data);break}}};window.addEventListener("message",L),document.addEventListener("click",I,!0),c=()=>{clearInterval(f),window.removeEventListener("message",L),document.removeEventListener("click",I,!0)}}).finally(()=>c==null?void 0:c())}signOut(){this.accessToken=null,this.user=null}refreshUser(){return l(this,null,function*(){let e=yield a(s(this,y),"/lm/user/info",{method:"POST"});if(e.code!==200)throw new Error(e.message);return this.user=e.data,e.data})}onAuthChange(e){let t=r=>{(r.key==="lumi-access-token"||r.key==="lumi-user"||r.key===null)&&e({isAuthenticated:this.isAuthenticated,user:this.user})};return window.addEventListener("storage",t),()=>{window.removeEventListener("storage",t)}}};y=new WeakMap,x=new WeakMap;var m,v=class{constructor(e,t){u(this,m);g(this,m,e),this.entityName=t}list(){return l(this,arguments,function*({filter:e,sort:t,limit:r,skip:o}={}){if(r){let n=yield a(s(this,m),this.uri("/find"),{method:"POST",body:{filter:e,sort:t,limit:r,skip:o}});if(n.code!==200)throw new Error(n.message);return n.data}else{let n=yield a(s(this,m),this.uri("/list"),{method:"POST",body:{filter:e,sort:t}});if(n.code!==200)throw new Error(n.message);return{total:n.data.length,list:n.data}}})}get(e){return l(this,null,function*(){let t=yield a(s(this,m),this.uri(`/${e}`),{method:"GET"});if(t.code!==200)throw new Error(t.message);return t.data})}create(e){return l(this,null,function*(){let t=yield a(s(this,m),this.uri(),{method:"POST",body:e});if(t.code!==200)throw new Error(t.message);return t.data})}createMany(e){return l(this,null,function*(){let t=yield a(s(this,m),this.uri("/batch"),{method:"POST",body:e});if(t.code!==200)throw new Error(t.message);return t.data})}update(e,t){return l(this,null,function*(){let r=yield a(s(this,m),this.uri(),{method:"PUT",body:{filter:{_id:e},update:t}});if(r.code!==200)throw new Error(r.message);return r.data})}delete(e){return l(this,null,function*(){let t=yield a(s(this,m),this.uri(`/${e}`),{method:"DELETE"});if(t.code!==200)throw new Error(t.message)})}deleteMany(e){return l(this,null,function*(){let t=yield a(s(this,m),this.uri("/batch-by-ids"),{method:"DELETE",params:{ids:e}});if(t.code!==200)throw new Error(t.message)})}uri(e=""){return`/lm/${s(this,m).config.projectId}/${this.entityName}/documents${e}`}};m=new WeakMap;var A,P=class{constructor(e){u(this,A);return g(this,A,e),new Proxy(this,{get(t,r){return r in t||(t[r]=new v(s(t,A),r)),t[r]}})}};A=new WeakMap;var S,O=class{constructor(e){u(this,S);g(this,S,e)}send(p){return l(this,arguments,function*({to:e,subject:t,fromName:r,html:o,text:n="",replyTo:c,scheduledAt:h}){if(!e||!t||!o&&!n)throw new Error("Failed to send email: Missing required parameters.");typeof e=="string"&&(e=[e]),typeof c=="string"&&(c=[c]);let f=yield a(s(this,S),`/lm/${s(this,S).config.projectId}/email/send`,{method:"POST",body:{to:e,subject:t,fromName:r,html:o,text:n,replyTo:c,scheduledAt:h}});if(f.code!==200)throw new E(f.code,f.message)})}};S=new WeakMap;var b,D=class{constructor(e){u(this,b);g(this,b,e)}upload(e){return l(this,null,function*(){let t=new FormData;e.forEach(o=>{t.append("files",o)});let r=yield a(s(this,b),`/lm/${s(this,b).config.projectId}/file/batch`,{method:"POST",body:t});if(r.code!==200)throw new E(r.code,r.message);return r.data})}delete(e){return l(this,null,function*(){let t=yield a(s(this,b),`/lm/${s(this,b).config.projectId}/file/batch`,{method:"DELETE",body:{fileUrls:e}});if(t.code!==200)throw new E(t.code,t.message)})}};b=new WeakMap;var U,M=class{constructor(e){u(this,U);g(this,U,e),this.email=new O(e),this.file=new D(e)}};U=new WeakMap;var $=class{constructor(e){this.config=e,this.auth=new R(this),this.entities=new P(this),this.tools=new M(this)}};function Ve(i){return new $(i)}export{O as EmailTool,P as EntitiesClient,v as EntityClient,D as FileTool,R as LumiAuthClient,$ as LumiClient,M as ToolsClient,Ve as createClient}; //# sourceMappingURL=index.mjs.map