@ztimson/momentum 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  <br />
4
4
 
5
5
  <!-- Logo -->
6
- <img src="public/favicon.svg" alt="Logo" width="200" height="200">
6
+ <img src="server/public/favicon.svg" alt="Logo" width="200" height="200">
7
7
 
8
8
  <!-- Title -->
9
9
  ### Momentum
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export * from './email';
7
7
  export * from './groups';
8
8
  export * from './logger';
9
9
  export * from './momentum';
10
+ export * from './pdf';
10
11
  export * from './settings';
11
12
  export * from './sockets';
12
13
  export * from './static';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC;AACtB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC;AACtB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC"}
package/dist/momentum.cjs CHANGED
@@ -1,4 +1,4 @@
1
- (function(h,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(h=typeof globalThis<"u"?globalThis:h||self,p(h.utils={}))})(this,function(h){"use strict";var Nt=Object.defineProperty;var Ht=(h,p,v)=>p in h?Nt(h,p,{enumerable:!0,configurable:!0,writable:!0,value:v}):h[p]=v;var a=(h,p,v)=>(Ht(h,typeof p!="symbol"?p+"":p,v),v);var p=Object.defineProperty,v=(i,e,t)=>e in i?p(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,l=(i,e,t)=>(v(i,typeof e!="symbol"?e+"":e,t),t);function At(i,e=!1){if(i==null)throw new Error("Cannot clean a NULL value");return Array.isArray(i)?i=i.filter(t=>t!=null):Object.entries(i).forEach(([t,r])=>{(e&&r===void 0||!e&&r==null)&&delete i[t]}),i}function K(i,e){const t=typeof i,r=typeof e;return t!="object"||i==null||r!="object"||e==null?t=="function"&&r=="function"?i.toString()==e.toString():i===e:Object.keys(i).length!=Object.keys(e).length?!1:Object.keys(i).every(s=>K(i[s],e[s]))}function _t(i,e){const t=document.createElement("a");t.href=i,t.download=e,document.body.appendChild(t),t.click(),document.body.removeChild(t)}class f{constructor(){l(this,"listeners",{})}static emit(e,...t){(this.listeners["*"]||[]).forEach(r=>r(e,...t)),(this.listeners[e.toString()]||[]).forEach(r=>r(...t))}static off(e,t){const r=e.toString();this.listeners[r]=(this.listeners[r]||[]).filter(s=>s===t)}static on(e,t){var r;const s=e.toString();return this.listeners[s]||(this.listeners[s]=[]),(r=this.listeners[s])==null||r.push(t),()=>this.off(e,t)}static once(e,t){return new Promise(r=>{const s=this.on(e,(...n)=>{r(n.length==1?n[0]:n),t&&t(...n),s()})})}emit(e,...t){(this.listeners["*"]||[]).forEach(r=>r(e,...t)),(this.listeners[e]||[]).forEach(r=>r(...t))}off(e,t){this.listeners[e]=(this.listeners[e]||[]).filter(r=>r===t)}on(e,t){var r;return this.listeners[e]||(this.listeners[e]=[]),(r=this.listeners[e])==null||r.push(t),()=>this.off(e,t)}once(e,t){return new Promise(r=>{const s=this.on(e,(...n)=>{r(n.length==1?n[0]:n),t&&t(...n),s()})})}}l(f,"listeners",{});const j=class L{constructor(e={}){l(this,"interceptors",{}),l(this,"headers",{}),this.opts=e,this.headers=e.headers||{},e.interceptors&&e.interceptors.forEach(t=>L.addInterceptor(t))}static addInterceptor(e){const t=Object.keys(L.interceptors).length.toString();return L.interceptors[t]=e,()=>{L.interceptors[t]=null}}addInterceptor(e){const t=Object.keys(this.interceptors).length.toString();return this.interceptors[t]=e,()=>{this.interceptors[t]=null}}async request(e={}){var t,r;if(!this.opts.url&&!e.url)throw new Error("URL needs to be set");const s=((t=e.url)!=null&&t.startsWith("http")?e.url:(this.opts.url||"")+(e.url||"")).replace(/([^:]\/)\/+/g,"$1"),n=At({"Content-Type":e.body&&!(e.body instanceof FormData)?"application/json":void 0,...L.headers,...this.headers,...e.headers});return fetch(s,{headers:n,method:e.method||(e.body?"POST":"GET"),body:(r=n["Content-Type"])!=null&&r.startsWith("application/json")&&e.body?JSON.stringify(e.body):e.body}).then(async o=>{var u,c;for(let y of[...Object.values(L.interceptors),...Object.values(this.interceptors)])await new Promise(O=>y(o,()=>O()));if(!o.ok)throw new Error(o.statusText);return!e.skipConverting&&(u=o.headers.get("Content-Type"))!=null&&u.startsWith("application/json")?await o.json():!e.skipConverting&&(c=o.headers.get("Content-Type"))!=null&&c.startsWith("text/plain")?await o.text():o})}};l(j,"interceptors",{}),l(j,"headers",{});let z=j;z.addInterceptor((i,e)=>{if(i.status==200)return e();throw i.status==400?new J(i.statusText):i.status==401?new Q(i.statusText):i.status==403?new X(i.statusText):i.status==404?new Z(i.statusText):i.status==500?new tt(i.statusText):new w(i.statusText,i.status)});class w extends Error{constructor(e,t){super(e),l(this,"_code"),t!=null&&(this._code=t)}get code(){return this._code||this.constructor.code}set code(e){this._code=e}static from(e){const t=Number(e.statusCode)??Number(e.code),r=new this(e.message||e.toString());return Object.assign(r,{stack:e.stack,...e,code:t??void 0})}static instanceof(e){return e.constructor.code!=null}toString(){return this.message||super.toString()}}l(w,"code",500);class J extends w{constructor(e="Bad Request"){super(e)}static instanceof(e){return e.constructor.code==this.code}}l(J,"code",400);class Q extends w{constructor(e="Unauthorized"){super(e)}static instanceof(e){return e.constructor.code==this.code}}l(Q,"code",401);class X extends w{constructor(e="Forbidden"){super(e)}static instanceof(e){return e.constructor.code==this.code}}l(X,"code",403);class Z extends w{constructor(e="Not Found"){super(e)}static instanceof(e){return e.constructor.code==this.code}}l(Z,"code",404);class tt extends w{constructor(e="Internal Server Error"){super(e)}static instanceof(e){return e.constructor.code==this.code}}l(tt,"code",500);const _={CLEAR:"\x1B[0m",BRIGHT:"\x1B[1m",DIM:"\x1B[2m",UNDERSCORE:"\x1B[4m",BLINK:"\x1B[5m",REVERSE:"\x1B[7m",HIDDEN:"\x1B[8m"},x={BLACK:"\x1B[30m",RED:"\x1B[31m",GREEN:"\x1B[32m",YELLOW:"\x1B[33m",BLUE:"\x1B[34m",MAGENTA:"\x1B[35m",CYAN:"\x1B[36m",LIGHT_GREY:"\x1B[37m",GREY:"\x1B[90m",LIGHT_RED:"\x1B[91m",LIGHT_GREEN:"\x1B[92m",LIGHT_YELLOW:"\x1B[93m",LIGHT_BLUE:"\x1B[94m",LIGHT_MAGENTA:"\x1B[95m",LIGHT_CYAN:"\x1B[96m",WHITE:"\x1B[97m"};var T=(i=>(i[i.ERROR=0]="ERROR",i[i.WARN=1]="WARN",i[i.INFO=2]="INFO",i[i.LOG=3]="LOG",i[i.DEBUG=4]="DEBUG",i))(T||{});l(class E extends f{constructor(e){super(),this.namespace=e}pad(e,t,r,s=!1){const n=e.toString(),o=t-n.length;if(o<=0)return n;const u=Array(~~(o/r.length)).fill(r).join("");return s?n+u:u+n}format(...e){const t=new Date;return`${`${t.getFullYear()}-${t.getMonth()+1}-${t.getDate()} ${this.pad(t.getHours().toString(),2,"0")}:${this.pad(t.getMinutes().toString(),2,"0")}:${this.pad(t.getSeconds().toString(),2,"0")}.${this.pad(t.getMilliseconds().toString(),3,"0",!0)}`}${this.namespace?` [${this.namespace}]`:""} ${e.join(" ")}`}debug(...e){if(E.LOG_LEVEL<4)return;const t=this.format(...e);E.emit(4,t),console.debug(x.LIGHT_GREY+t+_.CLEAR)}log(...e){if(E.LOG_LEVEL<3)return;const t=this.format(...e);E.emit(3,t),console.log(_.CLEAR+t)}info(...e){if(E.LOG_LEVEL<2)return;const t=this.format(...e);E.emit(2,t),console.info(x.BLUE+t+_.CLEAR)}warn(...e){if(E.LOG_LEVEL<1)return;const t=this.format(...e);E.emit(1,t),console.warn(x.YELLOW+t+_.CLEAR)}error(...e){if(E.LOG_LEVEL<0)return;const t=this.format(...e);E.emit(0,t),console.error(x.RED+t+_.CLEAR)}},"LOG_LEVEL",4);class $t extends Promise{constructor(e){super((t,r)=>e(s=>t(s),s=>r(s),s=>this.progress=s)),l(this,"listeners",[]),l(this,"_progress",0)}get progress(){return this._progress}set progress(e){e!=this._progress&&(this._progress=e,this.listeners.forEach(t=>t(e)))}onProgress(e){return this.listeners.push(e),this}}class m extends z{constructor(t=location.origin,r={}){r.url=t;super(r);a(this,"emitter",new f);a(this,"_token",null);a(this,"emit",this.emitter.emit.bind(this.emitter));a(this,"off",this.emitter.off.bind(this.emitter));a(this,"on",this.emitter.on.bind(this.emitter));a(this,"once",this.emitter.once.bind(this.emitter));this.url=t,this.opts=r}get token(){return this._token}set token(t){t!=this._token&&(this._token=t,this.headers.Authorization=`Bearer ${t}`,this.emit("TOKEN",t))}request(t){const r=super.request(t).then(s=>(this.emit("RESPONSE",s,t),s)).catch(s=>{throw this.emit("REJECTED",s,t),s});return this.emit("REQUEST",r,t),r}}var k=function(i,e){return k=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var s in r)Object.prototype.hasOwnProperty.call(r,s)&&(t[s]=r[s])},k(i,e)};function $(i,e){if(typeof e!="function"&&e!==null)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");k(i,e);function t(){this.constructor=i}i.prototype=e===null?Object.create(e):(t.prototype=e.prototype,new t)}function G(i){var e=typeof Symbol=="function"&&Symbol.iterator,t=e&&i[e],r=0;if(t)return t.call(i);if(i&&typeof i.length=="number")return{next:function(){return i&&r>=i.length&&(i=void 0),{value:i&&i[r++],done:!i}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function N(i,e){var t=typeof Symbol=="function"&&i[Symbol.iterator];if(!t)return i;var r=t.call(i),s,n=[],o;try{for(;(e===void 0||e-- >0)&&!(s=r.next()).done;)n.push(s.value)}catch(u){o={error:u}}finally{try{s&&!s.done&&(t=r.return)&&t.call(r)}finally{if(o)throw o.error}}return n}function H(i,e,t){if(t||arguments.length===2)for(var r=0,s=e.length,n;r<s;r++)(n||!(r in e))&&(n||(n=Array.prototype.slice.call(e,0,r)),n[r]=e[r]);return i.concat(n||Array.prototype.slice.call(e))}typeof SuppressedError=="function"&&SuppressedError;function b(i){return typeof i=="function"}function et(i){var e=function(r){Error.call(r),r.stack=new Error().stack},t=i(e);return t.prototype=Object.create(Error.prototype),t.prototype.constructor=t,t}var M=et(function(i){return function(t){i(this),this.message=t?t.length+` errors occurred during unsubscription:
2
- `+t.map(function(r,s){return s+1+") "+r.toString()}).join(`
3
- `):"",this.name="UnsubscriptionError",this.errors=t}});function W(i,e){if(i){var t=i.indexOf(e);0<=t&&i.splice(t,1)}}var D=function(){function i(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}return i.prototype.unsubscribe=function(){var e,t,r,s,n;if(!this.closed){this.closed=!0;var o=this._parentage;if(o)if(this._parentage=null,Array.isArray(o))try{for(var u=G(o),c=u.next();!c.done;c=u.next()){var y=c.value;y.remove(this)}}catch(d){e={error:d}}finally{try{c&&!c.done&&(t=u.return)&&t.call(u)}finally{if(e)throw e.error}}else o.remove(this);var O=this.initialTeardown;if(b(O))try{O()}catch(d){n=d instanceof M?d.errors:[d]}var R=this._finalizers;if(R){this._finalizers=null;try{for(var S=G(R),g=S.next();!g.done;g=S.next()){var B=g.value;try{st(B)}catch(d){n=n??[],d instanceof M?n=H(H([],N(n)),N(d.errors)):n.push(d)}}}catch(d){r={error:d}}finally{try{g&&!g.done&&(s=S.return)&&s.call(S)}finally{if(r)throw r.error}}}if(n)throw new M(n)}},i.prototype.add=function(e){var t;if(e&&e!==this)if(this.closed)st(e);else{if(e instanceof i){if(e.closed||e._hasParent(this))return;e._addParent(this)}(this._finalizers=(t=this._finalizers)!==null&&t!==void 0?t:[]).push(e)}},i.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},i.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},i.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&W(t,e)},i.prototype.remove=function(e){var t=this._finalizers;t&&W(t,e),e instanceof i&&e._removeParent(this)},i.EMPTY=function(){var e=new i;return e.closed=!0,e}(),i}(),rt=D.EMPTY;function it(i){return i instanceof D||i&&"closed"in i&&b(i.remove)&&b(i.add)&&b(i.unsubscribe)}function st(i){b(i)?i():i.unsubscribe()}var nt={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},ot={setTimeout:function(i,e){for(var t=[],r=2;r<arguments.length;r++)t[r-2]=arguments[r];return setTimeout.apply(void 0,H([i,e],N(t)))},clearTimeout:function(i){var e=ot.delegate;return((e==null?void 0:e.clearTimeout)||clearTimeout)(i)},delegate:void 0};function Rt(i){ot.setTimeout(function(){throw i})}function ut(){}function P(i){i()}var at=function(i){$(e,i);function e(t){var r=i.call(this)||this;return r.isStopped=!1,t?(r.destination=t,it(t)&&t.add(r)):r.destination=It,r}return e.create=function(t,r,s){return new Y(t,r,s)},e.prototype.next=function(t){this.isStopped||this._next(t)},e.prototype.error=function(t){this.isStopped||(this.isStopped=!0,this._error(t))},e.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},e.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,i.prototype.unsubscribe.call(this),this.destination=null)},e.prototype._next=function(t){this.destination.next(t)},e.prototype._error=function(t){try{this.destination.error(t)}finally{this.unsubscribe()}},e.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},e}(D),xt=Function.prototype.bind;function F(i,e){return xt.call(i,e)}var Dt=function(){function i(e){this.partialObserver=e}return i.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(r){I(r)}},i.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(r){I(r)}else I(e)},i.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){I(t)}},i}(),Y=function(i){$(e,i);function e(t,r,s){var n=i.call(this)||this,o;if(b(t)||!t)o={next:t??void 0,error:r??void 0,complete:s??void 0};else{var u;n&&nt.useDeprecatedNextContext?(u=Object.create(t),u.unsubscribe=function(){return n.unsubscribe()},o={next:t.next&&F(t.next,u),error:t.error&&F(t.error,u),complete:t.complete&&F(t.complete,u)}):o=t}return n.destination=new Dt(o),n}return e}(at);function I(i){Rt(i)}function Pt(i){throw i}var It={closed:!0,next:ut,error:Pt,complete:ut},Ut=function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"}();function qt(i){return i}function Ct(i){return i.length===0?qt:i.length===1?i[0]:function(t){return i.reduce(function(r,s){return s(r)},t)}}var ct=function(){function i(e){e&&(this._subscribe=e)}return i.prototype.lift=function(e){var t=new i;return t.source=this,t.operator=e,t},i.prototype.subscribe=function(e,t,r){var s=this,n=jt(e)?e:new Y(e,t,r);return P(function(){var o=s,u=o.operator,c=o.source;n.add(u?u.call(n,c):c?s._subscribe(n):s._trySubscribe(n))}),n},i.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},i.prototype.forEach=function(e,t){var r=this;return t=ht(t),new t(function(s,n){var o=new Y({next:function(u){try{e(u)}catch(c){n(c),o.unsubscribe()}},error:n,complete:s});r.subscribe(o)})},i.prototype._subscribe=function(e){var t;return(t=this.source)===null||t===void 0?void 0:t.subscribe(e)},i.prototype[Ut]=function(){return this},i.prototype.pipe=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return Ct(e)(this)},i.prototype.toPromise=function(e){var t=this;return e=ht(e),new e(function(r,s){var n;t.subscribe(function(o){return n=o},function(o){return s(o)},function(){return r(n)})})},i.create=function(e){return new i(e)},i}();function ht(i){var e;return(e=i??nt.Promise)!==null&&e!==void 0?e:Promise}function Bt(i){return i&&b(i.next)&&b(i.error)&&b(i.complete)}function jt(i){return i&&i instanceof at||Bt(i)&&it(i)}var kt=et(function(i){return function(){i(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),lt=function(i){$(e,i);function e(){var t=i.call(this)||this;return t.closed=!1,t.currentObservers=null,t.observers=[],t.isStopped=!1,t.hasError=!1,t.thrownError=null,t}return e.prototype.lift=function(t){var r=new dt(this,this);return r.operator=t,r},e.prototype._throwIfClosed=function(){if(this.closed)throw new kt},e.prototype.next=function(t){var r=this;P(function(){var s,n;if(r._throwIfClosed(),!r.isStopped){r.currentObservers||(r.currentObservers=Array.from(r.observers));try{for(var o=G(r.currentObservers),u=o.next();!u.done;u=o.next()){var c=u.value;c.next(t)}}catch(y){s={error:y}}finally{try{u&&!u.done&&(n=o.return)&&n.call(o)}finally{if(s)throw s.error}}}})},e.prototype.error=function(t){var r=this;P(function(){if(r._throwIfClosed(),!r.isStopped){r.hasError=r.isStopped=!0,r.thrownError=t;for(var s=r.observers;s.length;)s.shift().error(t)}})},e.prototype.complete=function(){var t=this;P(function(){if(t._throwIfClosed(),!t.isStopped){t.isStopped=!0;for(var r=t.observers;r.length;)r.shift().complete()}})},e.prototype.unsubscribe=function(){this.isStopped=this.closed=!0,this.observers=this.currentObservers=null},Object.defineProperty(e.prototype,"observed",{get:function(){var t;return((t=this.observers)===null||t===void 0?void 0:t.length)>0},enumerable:!1,configurable:!0}),e.prototype._trySubscribe=function(t){return this._throwIfClosed(),i.prototype._trySubscribe.call(this,t)},e.prototype._subscribe=function(t){return this._throwIfClosed(),this._checkFinalizedStatuses(t),this._innerSubscribe(t)},e.prototype._innerSubscribe=function(t){var r=this,s=this,n=s.hasError,o=s.isStopped,u=s.observers;return n||o?rt:(this.currentObservers=null,u.push(t),new D(function(){r.currentObservers=null,W(u,t)}))},e.prototype._checkFinalizedStatuses=function(t){var r=this,s=r.hasError,n=r.thrownError,o=r.isStopped;s?t.error(n):o&&t.complete()},e.prototype.asObservable=function(){var t=new ct;return t.source=this,t},e.create=function(t,r){return new dt(t,r)},e}(ct),dt=function(i){$(e,i);function e(t,r){var s=i.call(this)||this;return s.destination=t,s.source=r,s}return e.prototype.next=function(t){var r,s;(s=(r=this.destination)===null||r===void 0?void 0:r.next)===null||s===void 0||s.call(r,t)},e.prototype.error=function(t){var r,s;(s=(r=this.destination)===null||r===void 0?void 0:r.error)===null||s===void 0||s.call(r,t)},e.prototype.complete=function(){var t,r;(r=(t=this.destination)===null||t===void 0?void 0:t.complete)===null||r===void 0||r.call(t)},e.prototype._subscribe=function(t){var r,s;return(s=(r=this.source)===null||r===void 0?void 0:r.subscribe(t))!==null&&s!==void 0?s:rt},e}(lt),U=function(i){$(e,i);function e(t){var r=i.call(this)||this;return r._value=t,r}return Object.defineProperty(e.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),e.prototype._subscribe=function(t){var r=i.prototype._subscribe.call(this,t);return!r.closed&&t.next(this._value),r},e.prototype.getValue=function(){var t=this,r=t.hasError,s=t.thrownError,n=t._value;if(r)throw s;return this._throwIfClosed(),n},e.prototype.next=function(t){i.prototype.next.call(this,this._value=t)},e}(lt),pt=(i=>(i[i.DISABLED=0]="DISABLED",i[i.CRON=1]="CRON",i[i.EVENT=2]="EVENT",i[i.GET=3]="GET",i[i.POST=4]="POST",i[i.PATCH=5]="PATCH",i[i.PUT=6]="PUT",i[i.DELETE=7]="DELETE",i))(pt||{});class ft extends f{constructor(t){super();a(this,"api");a(this,"$cache",new U([]));this.api=typeof t=="string"?new m(t):t}get cache(){return this.$cache.value}set cache(t){this.$cache.next(t)}delete(t){return this.api.request({url:`/api/actions/${t}`,method:"DELETE"}).then(()=>{this.cache=this.cache.filter(r=>r._id!=t),this.emit("DELETE",t)})}list(){return this.api.request({url:"/api/actions"}).then(t=>(this.cache=t,this.emit("LIST",t),t))}read(t,r=!1){const s=this.cache.find(n=>n._id==t);return!r&&s?Promise.resolve(s):this.api.request({url:`/api/actions/${t}`}).then(n=>(n&&(this.cache=this.cache.filter(o=>o._id!=t).concat([n])),this.emit("READ",n),n))}run(t,r={}){return this.api.request({url:("/api/actions/run/"+t).replaceAll("//","/"),...r})}runById(t,r={}){const s=typeof t=="string"?t:t._id;return this.api.request({url:"/api/actions/run-by-id/"+s,...r})}update(t){return this.api.request({url:`/api/actions${t._id?`/${t._id}`:""}`,method:"POST",body:t}).then(r=>(r&&(this.cache=this.cache.filter(s=>s._id!=r._id).concat([r])),this.emit("UPDATE",r),r))}}class mt extends f{constructor(t,r){var s;super();a(this,"api");a(this,"storageKey");a(this,"$user",new U(void 0));if(this.opts=r,this.api=typeof t=="string"?new m(t):t,(s=this.opts)!=null&&s.loginUi||(this.opts={...this.opts,loginUi:this.api.url+"/ui/login"}),this.storageKey=`momentum:${new URL(this.api.url).host}`,this.api.on("TOKEN",n=>{var o;(o=this.opts)!=null&&o.persist&&(n?localStorage.setItem(this.storageKey,n):localStorage.removeItem(this.storageKey)),n?this.whoAmI(n,!0):this.user=null}),r!=null&&r.persist){const n=localStorage.getItem(this.storageKey);n?this.api.token=n:this.user=null}else this.user=null}get user(){return this.$user.value}set user(t){if(!K(this.user,t)){const r=t||null;this.$user.next(r),this.emit("USER",r)}}knownHost(t=location.origin){return t.startsWith("/")?Promise.resolve():this.api.request({url:`/api/auth/known-host?host=${encodeURI(new URL(t).origin)}`})}login(t,r){return this.api.request({url:"/api/auth/login",headers:{Authorization:void 0},body:{username:t,password:r}}).then(async s=>(this.api.token=s.token,await this.once("USER")))}loginRedirect(t=location.origin){return new Promise((r,s)=>{var o;const n=window.open(encodeURI(`${(o=this.opts)==null?void 0:o.loginUi}?redirect=postmessage&host=${t}`),"_blank");if(!n)return s("Unable to open login");n.addEventListener("message",u=>{var c;if(!((c=u==null?void 0:u.data)!=null&&c.token))return s("Unknown response from login");this.api.token=u.data.token,r(u.data.token),n.close()})})}logout(){this.api.token=null,this.user=null,this.emit("LOGOUT")}async register(t){const r=await this.api.request({url:"/api/auth/register",body:{...t}});return this.emit("REGISTER",t),r}async reset(t,r){await this.api.request({url:"/api/auth/reset",headers:{Authorization:r?`Bearer ${r}`:void 0},body:{email:r?void 0:t,password:r?t:void 0}}),this.emit(r?"RESET_COMPLETE":"RESET_REQUEST",r||t)}async whoAmI(t,r=!1){t||(t=this.api.token);const s=await this.api.request({url:"/api/auth/whoami",headers:t?{Authorization:`Bearer ${t}`}:void 0});return r&&(this.api.token=t,this.user=(s==null?void 0:s.user)||null,s&&this.emit("LOGIN",s.user)),s}}class yt extends f{constructor(t){super();a(this,"api");this.api=typeof t=="string"?new m(t):t}delete(t,r){return this.api.request({url:`/api/data/${t}/${r}`,method:"DELETE"}).then(()=>this.emit("DELETE",t,r))}get(t,r){return this.api.request({url:`/api/data/${t}${r?`/${r}`:""}`}).then(s=>(this.emit("GET",t,s),s))}raw(t,r,s,n){return this.api.request({url:`/api/data/${t}`,body:{operand:r,query:s,options:n}}).then(o=>(this.emit("RAW",t,o),o))}set(t,r,s=!1){return this.api.request({url:`/api/data/${t}/${r._id||""}`,method:s?"PATCH":"POST",body:r}).then(n=>(this.emit("SET",t,r),n))}}class Et extends f{constructor(t){super();a(this,"api");this.api=typeof t=="string"?new m(t):t}send(t){let r="/api/email";return typeof t.body=="object"&&(r+=`/${t.body.template}`),this.api.request({url:r,body:t}).then(s=>(this.emit("SENT",t),s))}}class gt extends f{constructor(t){super();a(this,"api");this.api=typeof t=="string"?new m(t):t}create(t){return this.api.request({url:`/api/groups/${t.name}`,method:"POST",body:t}).then(r=>(this.emit("CREATE",r),r))}list(){return this.api.request({url:"/api/groups"}).then(t=>(this.emit("LIST",t),t))}read(t){return this.api.request({url:`/api/groups/${t}`}).then(r=>(this.emit("READ",r),r))}update(t){return this.api.request({url:`/api/groups/${t.name}`,method:"PATCH",body:t}).then(r=>(this.emit("UPDATE",r),r))}delete(t){return this.api.request({url:`/api/groups/${t}`,method:"DELETE"}).then(()=>this.emit("DELETE",t))}}class bt{constructor(e,t){a(this,"api");this.api=typeof e=="string"?new m(e):e,t!=null&&t!="NONE"&&(window.addEventListener("error",r=>this.error(r.error.stack)),window.addEventListener("unhandledrejection",async r=>this.error(r.reason.stack)))}buildLog(e,t){return{time:new Date().getTime(),level:e,log:t,ctx:{cores:navigator.hardwareConcurrency,mem:navigator==null?void 0:navigator.deviceMemory,res:[window.innerWidth,window.innerHeight],url:location.href}}}clearClientLogs(){return this.api.request({url:"/api/logs/client",method:"DELETE"})}clearServerLogs(){return this.api.request({url:"/api/logs/server",method:"DELETE"})}clientLogs(e,t){const r=[e?`length=${e}`:void 0,t?`page=${t}`:void 0].filter(s=>!!s).join("&");return this.api.request({url:`/api/logs/client${r?`?${r}`:""}`})}serverLogs(e,t){const r=[e?`length=${e}`:void 0,t?`page=${t}`:void 0].filter(s=>!!s).join("&");return this.api.request({url:`/api/logs/server${r?`?${r}`:""}`})}debug(...e){return this.api.request({url:"/api/logs/client",body:this.buildLog(T.DEBUG,e)}).catch(()=>{})}log(...e){return this.api.request({url:"/api/logs/client",body:this.buildLog(T.LOG,e)}).catch(()=>{})}info(...e){return this.api.request({url:"/api/logs/client",body:this.buildLog(T.INFO,e)}).catch(()=>{})}warn(...e){return this.api.request({url:"/api/logs/client",body:this.buildLog(T.WARN,e)}).catch(()=>{})}error(...e){return this.api.request({url:"/api/logs/client",body:this.buildLog(T.ERROR,e)}).catch(()=>{})}}const C=class C{constructor(e){a(this,"api");a(this,"url");a(this,"connection");a(this,"open",!1);this.api=typeof e=="string"?new m(e):e,this.url=this.api.url.replace("http","ws"),this.api.on("TOKEN",()=>this.connect()),this.api.token||this.connect()}close(){console.debug("Disconnected from Momentum"),this.open=!1,this.connection.close()}connect(e=3){var r;((r=this.connection)==null?void 0:r.readyState)<2&&this.connection.close(),this.connection=new WebSocket(this.url+(this.api.token?`?token=${this.api.token}`:""));const t=setTimeout(()=>{this.open||(this.connection.close(),console.error("Momentum connection timeout"),e>0&&this.connect(e-1))},C.timeout);this.connection.onclose=()=>this.open=!1,this.connection.onmessage=this.handle,this.connection.onopen=()=>{this.open=!0,clearTimeout(t),console.debug("Connected to Momentum")}}handle(...e){console.log(e)}send(e,t){this.connection.send(JSON.stringify({token:this.api.token,channel:e,payload:t}))}};a(C,"timeout",1e4);let q=C;class vt extends f{constructor(t){super();a(this,"api");this.api=typeof t=="string"?new m(t):t}delete(t){const r=(t.startsWith("/api/storage/")?t:"/api/storage/"+t).replaceAll("//","/");return this.api.request({url:r,method:"DELETE"}).then(()=>{this.emit("DELETE",r)})}list(t){const r=(t.startsWith("/api/storage/")?t:"/api/storage/"+t).replaceAll("//","/");return this.api.request({url:r+"?list"}).then(s=>(this.emit("LIST",t,s),s))}open(t,r="_blank"){const s=(t.startsWith("/api/storage/")?t:"/api/storage/"+t).replaceAll(/\/{2,}/g,"/"),n=`${this.api.url}${s}${this.api.token?`?token=${this.api.token}`:""}`;return r?(this.emit("OPEN",t),window.open(n,r)):n}mkdir(t){const r=(t.startsWith("/api/storage/")?t:"/api/storage/"+t).replaceAll(/\/{2,}/g,"/");return this.api.request({url:r+"?directory",method:"POST"})}download(t,r={}){return new $t((s,n,o)=>{const u=("/api/storage/"+t).replaceAll("//","/");this.api.request({...r,url:u,skipConverting:!0}).then(c=>{var d;if(!c.ok)return n(c.statusText);const y=c.headers.get("Content-Length"),O=y?parseInt(y,10):0;let R=[],S=0;const g=(d=c.body)==null?void 0:d.getReader(),B=Tt=>{if(Tt.done){o(1);const A=new Blob(R),V=r.downloadAs||new URL(c.url).pathname.split("/").pop(),Ot=URL.createObjectURL(A);_t(Ot,V.includes(".")?V:V+".zip"),URL.revokeObjectURL(Ot),this.emit("DOWNLOAD",t,A),s(A)}else{const A=Tt.value;R.push(A),S+=A.length,o(S/O),g.read().then(B)}};g==null||g.read().then(B)})})}async upload(t,r=""){if(t||(t=await new Promise(u=>{const c=document.createElement("input");c.type="file",c.accept=(r==null?void 0:r.accept)||"*",c.style.display="none",c.multiple=!!(r!=null&&r.multiple),c.onblur=c.onchange=async()=>{u(Array.from(c.files)),c.remove()},document.body.appendChild(c),c.click()})),!t||Array.isArray(t)&&!t.length)return[];const n=new FormData,o=("/api/storage/"+(typeof r=="string"?r:r==null?void 0:r.path)).replaceAll("//","/");return(Array.isArray(t)?t:[t]).forEach(u=>n.append("file",u)),this.api.request({url:o,body:n}).then(u=>(this.emit("UPLOAD",u),u))}}class wt extends f{constructor(t){super();a(this,"api");a(this,"listed",!1);a(this,"$cache",new U([]));this.api=typeof t=="string"?new m(t):t}get cache(){return this.$cache.value}set cache(t){this.$cache.next(t)}delete(t){return this.api.request({url:`/api/users/${t}`,method:"DELETE"}).then(()=>{this.cache=this.cache.filter(r=>r.username!=t),this.emit("DELETE",t)})}list(t=!1){return!t&&this.listed?Promise.resolve(this.cache):this.api.request({url:"/api/users"}).then(r=>(this.cache=r,this.listed=!0,this.emit("LIST",r),r))}read(t,r=!1){if(!r){const s=this.cache.find(n=>n.username==t);if(s)return Promise.resolve(s)}return this.api.request({url:`/api/users/${t}`}).then(s=>(s&&(this.cache={...this.cache,[s.username]:s}),this.emit("READ",s),s))}update(t){return this.api.request({url:`/api/users/${t.username}`,method:"PATCH",body:t}).then(r=>(r&&(this.cache=this.cache.filter(s=>s.username!=t.username).concat([r])),this.emit("UPDATE",r),r))}}class St extends f{constructor(t){super();a(this,"api");a(this,"$cache",new U({}));this.api=typeof t=="string"?new m(t):t}get cache(){return this.$cache.value}set cache(t){this.$cache.next(t)}list(t=!1){return this.api.request({url:"/api/settings"+(t?"?detailed":"")}).then(r=>(this.cache=t?Object.values(r).reduce((s,n)=>({...s,[n.key]:n.value}),{}):r,this.emit("LIST",r),r))}delete(t){return this.api.request({url:`/api/settings/${t}`,method:"DELETE"}).then(()=>{this.cache={...this.cache,[t]:void 0},this.emit("DELETE",t)})}read(t,r=!1){return!r&&this.cache[t]?Promise.resolve(this.cache[t]):this.api.request({url:`/api/settings/${t}`}).then(s=>(s&&(this.cache={...this.cache,[s.key]:s}),this.emit("READ",s),s))}update(t){return this.api.request({url:`/api/settings/${t.key}`,body:t}).then(r=>(r&&(this.cache={...this.cache,[r.key]:r.value}),this.emit("UPDATE",r),r))}}class Lt extends f{constructor(t){super();a(this,"api");this.api=typeof t=="string"?new m(t):t}delete(t){return this.api.request({url:`/api/static/${t}`,method:"DELETE"}).then(()=>{this.emit("DELETE",t)})}list(t){const r=("/api/static/"+t).replaceAll("//","/");return this.api.request({url:r}).then(s=>(this.emit("LIST",t,s),s))}upload(t,r="/"){const s=new FormData;return(Array.isArray(t)?t:[t]).forEach(n=>s.append("file",n)),this.api.request({url:"/api/static"+r,body:s}).then(n=>(this.emit("UPLOAD",n),n))}}class Gt extends f{constructor(t,r){super();a(this,"api");a(this,"actions");a(this,"auth");a(this,"data");a(this,"email");a(this,"groups");a(this,"logger");a(this,"settings");a(this,"socket");a(this,"static");a(this,"storage");a(this,"users");this.api=new m(t,r==null?void 0:r.api),this.actions=new ft(this.api),this.auth=new mt(this.api,{persist:(r==null?void 0:r.persist)??!0,loginUi:r==null?void 0:r.loginUi}),this.data=new yt(this.api),this.email=new Et(this.api),this.groups=new gt(this.api),this.logger=new bt(this.api,r==null?void 0:r.logLevel),this.settings=new St(this.api),this.static=new Lt(this.api),this.storage=new vt(this.api),this.users=new wt(this.api),r!=null&&r.socket&&(this.socket=new q(this.api)),this.api.on("*",(s,...n)=>this.emit(`API::${s}`,...n)),this.actions.on("*",(s,...n)=>this.emit(`ACTIONS::${s}`,...n)),this.auth.on("*",(s,...n)=>this.emit(`AUTH::${s}`,...n)),this.data.on("*",(s,...n)=>this.emit(`DATA::${s}`,...n)),this.email.on("*",(s,...n)=>this.emit(`EMAIL::${s}`,...n)),this.groups.on("*",(s,...n)=>this.emit(`GROUPS::${s}`,...n)),this.settings.on("*",(s,...n)=>this.emit(`SETTINGS::${s}`,...n)),this.static.on("*",(s,...n)=>this.emit(`STATIC::${s}`,...n)),this.storage.on("*",(s,...n)=>this.emit(`STORAGE::${s}`,...n)),this.users.on("*",(s,...n)=>this.emit(`USERS::${s}`,...n)),this.users.on("*",(s,...n)=>{var o;if(Array.isArray(n[0])){const u=n[0].find(c=>{var y;return c._id==((y=this.auth.user)==null?void 0:y._id)});u&&(this.auth.user=u)}else n[0]._id==((o=this.auth.user)==null?void 0:o._id)&&(this.auth.user=n[0])})}}h.ActionType=pt,h.Actions=ft,h.Api=m,h.Auth=mt,h.Data=yt,h.Email=Et,h.Groups=gt,h.Logger=bt,h.Momentum=Gt,h.Settings=St,h.Socket=q,h.Static=Lt,h.Storage=vt,h.Users=wt,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
1
+ (function(h,y){typeof exports=="object"&&typeof module<"u"?y(exports):typeof define=="function"&&define.amd?define(["exports"],y):(h=typeof globalThis<"u"?globalThis:h||self,y(h.utils={}))})(this,function(h){"use strict";var Ht=Object.defineProperty;var Wt=(h,y,w)=>y in h?Ht(h,y,{enumerable:!0,configurable:!0,writable:!0,value:w}):h[y]=w;var a=(h,y,w)=>(Wt(h,typeof y!="symbol"?y+"":y,w),w);var y=Object.defineProperty,w=(i,e,t)=>e in i?y(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,l=(i,e,t)=>(w(i,typeof e!="symbol"?e+"":e,t),t);function St(i,e=!1){if(i==null)throw new Error("Cannot clean a NULL value");return Array.isArray(i)?i=i.filter(t=>t!=null):Object.entries(i).forEach(([t,r])=>{(e&&r===void 0||!e&&r==null)&&delete i[t]}),i}function K(i,e){const t=typeof i,r=typeof e;return t!="object"||i==null||r!="object"||e==null?t=="function"&&r=="function"?i.toString()==e.toString():i===e:Object.keys(i).length!=Object.keys(e).length?!1:Object.keys(i).every(n=>K(i[n],e[n]))}function z(i,e){const t=document.createElement("a");t.href=i,t.download=e,document.body.appendChild(t),t.click(),document.body.removeChild(t)}class d{constructor(){l(this,"listeners",{})}static emit(e,...t){(this.listeners["*"]||[]).forEach(r=>r(e,...t)),(this.listeners[e.toString()]||[]).forEach(r=>r(...t))}static off(e,t){const r=e.toString();this.listeners[r]=(this.listeners[r]||[]).filter(n=>n===t)}static on(e,t){var r;const n=e.toString();return this.listeners[n]||(this.listeners[n]=[]),(r=this.listeners[n])==null||r.push(t),()=>this.off(e,t)}static once(e,t){return new Promise(r=>{const n=this.on(e,(...s)=>{r(s.length==1?s[0]:s),t&&t(...s),n()})})}emit(e,...t){(this.listeners["*"]||[]).forEach(r=>r(e,...t)),(this.listeners[e]||[]).forEach(r=>r(...t))}off(e,t){this.listeners[e]=(this.listeners[e]||[]).filter(r=>r===t)}on(e,t){var r;return this.listeners[e]||(this.listeners[e]=[]),(r=this.listeners[e])==null||r.push(t),()=>this.off(e,t)}once(e,t){return new Promise(r=>{const n=this.on(e,(...s)=>{r(s.length==1?s[0]:s),t&&t(...s),n()})})}}l(d,"listeners",{});class T extends Error{constructor(e,t){super(e),l(this,"_code"),t!=null&&(this._code=t)}get code(){return this._code||this.constructor.code}set code(e){this._code=e}static from(e){const t=Number(e.statusCode)??Number(e.code),r=new this(e.message||e.toString());return Object.assign(r,{stack:e.stack,...e,code:t??void 0})}static instanceof(e){return e.constructor.code!=null}toString(){return this.message||super.toString()}}l(T,"code",500);class Lt extends T{constructor(e="Bad Request"){super(e)}static instanceof(e){return e.constructor.code==this.code}}l(Lt,"code",400);class Tt extends T{constructor(e="Unauthorized"){super(e)}static instanceof(e){return e.constructor.code==this.code}}l(Tt,"code",401);class Ot extends T{constructor(e="Forbidden"){super(e)}static instanceof(e){return e.constructor.code==this.code}}l(Ot,"code",403);class $t extends T{constructor(e="Not Found"){super(e)}static instanceof(e){return e.constructor.code==this.code}}l($t,"code",404);class At extends T{constructor(e="Internal Server Error"){super(e)}static instanceof(e){return e.constructor.code==this.code}}l(At,"code",500);const A={CLEAR:"\x1B[0m",BRIGHT:"\x1B[1m",DIM:"\x1B[2m",UNDERSCORE:"\x1B[4m",BLINK:"\x1B[5m",REVERSE:"\x1B[7m",HIDDEN:"\x1B[8m"},D={BLACK:"\x1B[30m",RED:"\x1B[31m",GREEN:"\x1B[32m",YELLOW:"\x1B[33m",BLUE:"\x1B[34m",MAGENTA:"\x1B[35m",CYAN:"\x1B[36m",LIGHT_GREY:"\x1B[37m",GREY:"\x1B[90m",LIGHT_RED:"\x1B[91m",LIGHT_GREEN:"\x1B[92m",LIGHT_YELLOW:"\x1B[93m",LIGHT_BLUE:"\x1B[94m",LIGHT_MAGENTA:"\x1B[95m",LIGHT_CYAN:"\x1B[96m",WHITE:"\x1B[97m"};var O=(i=>(i[i.ERROR=0]="ERROR",i[i.WARN=1]="WARN",i[i.INFO=2]="INFO",i[i.LOG=3]="LOG",i[i.DEBUG=4]="DEBUG",i))(O||{});l(class g extends d{constructor(e){super(),this.namespace=e}pad(e,t,r,n=!1){const s=e.toString(),o=t-s.length;if(o<=0)return s;const u=Array(~~(o/r.length)).fill(r).join("");return n?s+u:u+s}format(...e){const t=new Date;return`${`${t.getFullYear()}-${t.getMonth()+1}-${t.getDate()} ${this.pad(t.getHours().toString(),2,"0")}:${this.pad(t.getMinutes().toString(),2,"0")}:${this.pad(t.getSeconds().toString(),2,"0")}.${this.pad(t.getMilliseconds().toString(),3,"0",!0)}`}${this.namespace?` [${this.namespace}]`:""} ${e.join(" ")}`}debug(...e){if(g.LOG_LEVEL<4)return;const t=this.format(...e);g.emit(4,t),console.debug(D.LIGHT_GREY+t+A.CLEAR)}log(...e){if(g.LOG_LEVEL<3)return;const t=this.format(...e);g.emit(3,t),console.log(A.CLEAR+t)}info(...e){if(g.LOG_LEVEL<2)return;const t=this.format(...e);g.emit(2,t),console.info(D.BLUE+t+A.CLEAR)}warn(...e){if(g.LOG_LEVEL<1)return;const t=this.format(...e);g.emit(1,t),console.warn(D.YELLOW+t+A.CLEAR)}error(...e){if(g.LOG_LEVEL<0)return;const t=this.format(...e);g.emit(0,t),console.error(D.RED+t+A.CLEAR)}},"LOG_LEVEL",4);class _t extends Promise{constructor(e){super((t,r)=>e(n=>t(n),n=>r(n),n=>this.progress=n)),l(this,"listeners",[]),l(this,"_progress",0)}get progress(){return this._progress}set progress(e){e!=this._progress&&(this._progress=e,this.listeners.forEach(t=>t(e)))}onProgress(e){return this.listeners.push(e),this}}const B=class L{constructor(e={}){l(this,"interceptors",{}),l(this,"headers",{}),this.opts=e,this.headers=e.headers||{},e.interceptors&&e.interceptors.forEach(t=>L.addInterceptor(t))}static addInterceptor(e){const t=Object.keys(L.interceptors).length.toString();return L.interceptors[t]=e,()=>{L.interceptors[t]=null}}addInterceptor(e){const t=Object.keys(this.interceptors).length.toString();return this.interceptors[t]=e,()=>{this.interceptors[t]=null}}async request(e={}){var t,r;if(!this.opts.url&&!e.url)throw new Error("URL needs to be set");const n=((t=e.url)!=null&&t.startsWith("http")?e.url:(this.opts.url||"")+(e.url||"")).replace(/([^:]\/)\/+/g,"$1"),s=St({"Content-Type":e.body&&!(e.body instanceof FormData)?"application/json":void 0,...L.headers,...this.headers,...e.headers});return fetch(n,{headers:s,method:e.method||(e.body?"POST":"GET"),body:(r=s["Content-Type"])!=null&&r.startsWith("application/json")&&e.body?JSON.stringify(e.body):e.body}).then(async o=>{for(let f of[...Object.values(L.interceptors),...Object.values(this.interceptors)])await new Promise(v=>f(o,()=>v()));const u=await(async()=>{var f,v;return!e.skipConverting&&(f=o.headers.get("Content-Type"))!=null&&f.startsWith("application/json")?await o.json():!e.skipConverting&&(v=o.headers.get("Content-Type"))!=null&&v.startsWith("text/plain")?await o.text():o})();if(o.ok)return u;const c=o.statusText||(typeof u=="string"?u:null);throw c?new Error(c):u})}};l(B,"interceptors",{}),l(B,"headers",{});let Rt=B;class p extends Rt{constructor(t=location.origin,r={}){r.url=t;super(r);a(this,"emitter",new d);a(this,"_token",null);a(this,"emit",this.emitter.emit.bind(this.emitter));a(this,"off",this.emitter.off.bind(this.emitter));a(this,"on",this.emitter.on.bind(this.emitter));a(this,"once",this.emitter.once.bind(this.emitter));this.url=t,this.opts=r}get token(){return this._token}set token(t){t!=this._token&&(this._token=t,this.headers.Authorization=`Bearer ${t}`,this.emit("TOKEN",t))}request(t){const r=super.request(t).then(n=>(this.emit("RESPONSE",n,t),n)).catch(n=>{throw this.emit("REJECTED",n,t),n});return this.emit("REQUEST",r,t),r}}var k=function(i,e){return k=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])},k(i,e)};function _(i,e){if(typeof e!="function"&&e!==null)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");k(i,e);function t(){this.constructor=i}i.prototype=e===null?Object.create(e):(t.prototype=e.prototype,new t)}function G(i){var e=typeof Symbol=="function"&&Symbol.iterator,t=e&&i[e],r=0;if(t)return t.call(i);if(i&&typeof i.length=="number")return{next:function(){return i&&r>=i.length&&(i=void 0),{value:i&&i[r++],done:!i}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function N(i,e){var t=typeof Symbol=="function"&&i[Symbol.iterator];if(!t)return i;var r=t.call(i),n,s=[],o;try{for(;(e===void 0||e-- >0)&&!(n=r.next()).done;)s.push(n.value)}catch(u){o={error:u}}finally{try{n&&!n.done&&(t=r.return)&&t.call(r)}finally{if(o)throw o.error}}return s}function H(i,e,t){if(t||arguments.length===2)for(var r=0,n=e.length,s;r<n;r++)(s||!(r in e))&&(s||(s=Array.prototype.slice.call(e,0,r)),s[r]=e[r]);return i.concat(s||Array.prototype.slice.call(e))}typeof SuppressedError=="function"&&SuppressedError;function b(i){return typeof i=="function"}function J(i){var e=function(r){Error.call(r),r.stack=new Error().stack},t=i(e);return t.prototype=Object.create(Error.prototype),t.prototype.constructor=t,t}var W=J(function(i){return function(t){i(this),this.message=t?t.length+` errors occurred during unsubscription:
2
+ `+t.map(function(r,n){return n+1+") "+r.toString()}).join(`
3
+ `):"",this.name="UnsubscriptionError",this.errors=t}});function M(i,e){if(i){var t=i.indexOf(e);0<=t&&i.splice(t,1)}}var x=function(){function i(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}return i.prototype.unsubscribe=function(){var e,t,r,n,s;if(!this.closed){this.closed=!0;var o=this._parentage;if(o)if(this._parentage=null,Array.isArray(o))try{for(var u=G(o),c=u.next();!c.done;c=u.next()){var f=c.value;f.remove(this)}}catch(m){e={error:m}}finally{try{c&&!c.done&&(t=u.return)&&t.call(u)}finally{if(e)throw e.error}}else o.remove(this);var v=this.initialTeardown;if(b(v))try{v()}catch(m){s=m instanceof W?m.errors:[m]}var R=this._finalizers;if(R){this._finalizers=null;try{for(var S=G(R),E=S.next();!E.done;E=S.next()){var j=E.value;try{Z(j)}catch(m){s=s??[],m instanceof W?s=H(H([],N(s)),N(m.errors)):s.push(m)}}}catch(m){r={error:m}}finally{try{E&&!E.done&&(n=S.return)&&n.call(S)}finally{if(r)throw r.error}}}if(s)throw new W(s)}},i.prototype.add=function(e){var t;if(e&&e!==this)if(this.closed)Z(e);else{if(e instanceof i){if(e.closed||e._hasParent(this))return;e._addParent(this)}(this._finalizers=(t=this._finalizers)!==null&&t!==void 0?t:[]).push(e)}},i.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},i.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},i.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&M(t,e)},i.prototype.remove=function(e){var t=this._finalizers;t&&M(t,e),e instanceof i&&e._removeParent(this)},i.EMPTY=function(){var e=new i;return e.closed=!0,e}(),i}(),Q=x.EMPTY;function X(i){return i instanceof x||i&&"closed"in i&&b(i.remove)&&b(i.add)&&b(i.unsubscribe)}function Z(i){b(i)?i():i.unsubscribe()}var tt={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},et={setTimeout:function(i,e){for(var t=[],r=2;r<arguments.length;r++)t[r-2]=arguments[r];return setTimeout.apply(void 0,H([i,e],N(t)))},clearTimeout:function(i){var e=et.delegate;return((e==null?void 0:e.clearTimeout)||clearTimeout)(i)},delegate:void 0};function Dt(i){et.setTimeout(function(){throw i})}function rt(){}function P(i){i()}var it=function(i){_(e,i);function e(t){var r=i.call(this)||this;return r.isStopped=!1,t?(r.destination=t,X(t)&&t.add(r)):r.destination=qt,r}return e.create=function(t,r,n){return new Y(t,r,n)},e.prototype.next=function(t){this.isStopped||this._next(t)},e.prototype.error=function(t){this.isStopped||(this.isStopped=!0,this._error(t))},e.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},e.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,i.prototype.unsubscribe.call(this),this.destination=null)},e.prototype._next=function(t){this.destination.next(t)},e.prototype._error=function(t){try{this.destination.error(t)}finally{this.unsubscribe()}},e.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},e}(x),xt=Function.prototype.bind;function F(i,e){return xt.call(i,e)}var Pt=function(){function i(e){this.partialObserver=e}return i.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(r){U(r)}},i.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(r){U(r)}else U(e)},i.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){U(t)}},i}(),Y=function(i){_(e,i);function e(t,r,n){var s=i.call(this)||this,o;if(b(t)||!t)o={next:t??void 0,error:r??void 0,complete:n??void 0};else{var u;s&&tt.useDeprecatedNextContext?(u=Object.create(t),u.unsubscribe=function(){return s.unsubscribe()},o={next:t.next&&F(t.next,u),error:t.error&&F(t.error,u),complete:t.complete&&F(t.complete,u)}):o=t}return s.destination=new Pt(o),s}return e}(it);function U(i){Dt(i)}function Ut(i){throw i}var qt={closed:!0,next:rt,error:Ut,complete:rt},It=function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"}();function Ct(i){return i}function jt(i){return i.length===0?Ct:i.length===1?i[0]:function(t){return i.reduce(function(r,n){return n(r)},t)}}var nt=function(){function i(e){e&&(this._subscribe=e)}return i.prototype.lift=function(e){var t=new i;return t.source=this,t.operator=e,t},i.prototype.subscribe=function(e,t,r){var n=this,s=kt(e)?e:new Y(e,t,r);return P(function(){var o=n,u=o.operator,c=o.source;s.add(u?u.call(s,c):c?n._subscribe(s):n._trySubscribe(s))}),s},i.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},i.prototype.forEach=function(e,t){var r=this;return t=st(t),new t(function(n,s){var o=new Y({next:function(u){try{e(u)}catch(c){s(c),o.unsubscribe()}},error:s,complete:n});r.subscribe(o)})},i.prototype._subscribe=function(e){var t;return(t=this.source)===null||t===void 0?void 0:t.subscribe(e)},i.prototype[It]=function(){return this},i.prototype.pipe=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return jt(e)(this)},i.prototype.toPromise=function(e){var t=this;return e=st(e),new e(function(r,n){var s;t.subscribe(function(o){return s=o},function(o){return n(o)},function(){return r(s)})})},i.create=function(e){return new i(e)},i}();function st(i){var e;return(e=i??tt.Promise)!==null&&e!==void 0?e:Promise}function Bt(i){return i&&b(i.next)&&b(i.error)&&b(i.complete)}function kt(i){return i&&i instanceof it||Bt(i)&&X(i)}var Gt=J(function(i){return function(){i(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),ot=function(i){_(e,i);function e(){var t=i.call(this)||this;return t.closed=!1,t.currentObservers=null,t.observers=[],t.isStopped=!1,t.hasError=!1,t.thrownError=null,t}return e.prototype.lift=function(t){var r=new ut(this,this);return r.operator=t,r},e.prototype._throwIfClosed=function(){if(this.closed)throw new Gt},e.prototype.next=function(t){var r=this;P(function(){var n,s;if(r._throwIfClosed(),!r.isStopped){r.currentObservers||(r.currentObservers=Array.from(r.observers));try{for(var o=G(r.currentObservers),u=o.next();!u.done;u=o.next()){var c=u.value;c.next(t)}}catch(f){n={error:f}}finally{try{u&&!u.done&&(s=o.return)&&s.call(o)}finally{if(n)throw n.error}}}})},e.prototype.error=function(t){var r=this;P(function(){if(r._throwIfClosed(),!r.isStopped){r.hasError=r.isStopped=!0,r.thrownError=t;for(var n=r.observers;n.length;)n.shift().error(t)}})},e.prototype.complete=function(){var t=this;P(function(){if(t._throwIfClosed(),!t.isStopped){t.isStopped=!0;for(var r=t.observers;r.length;)r.shift().complete()}})},e.prototype.unsubscribe=function(){this.isStopped=this.closed=!0,this.observers=this.currentObservers=null},Object.defineProperty(e.prototype,"observed",{get:function(){var t;return((t=this.observers)===null||t===void 0?void 0:t.length)>0},enumerable:!1,configurable:!0}),e.prototype._trySubscribe=function(t){return this._throwIfClosed(),i.prototype._trySubscribe.call(this,t)},e.prototype._subscribe=function(t){return this._throwIfClosed(),this._checkFinalizedStatuses(t),this._innerSubscribe(t)},e.prototype._innerSubscribe=function(t){var r=this,n=this,s=n.hasError,o=n.isStopped,u=n.observers;return s||o?Q:(this.currentObservers=null,u.push(t),new x(function(){r.currentObservers=null,M(u,t)}))},e.prototype._checkFinalizedStatuses=function(t){var r=this,n=r.hasError,s=r.thrownError,o=r.isStopped;n?t.error(s):o&&t.complete()},e.prototype.asObservable=function(){var t=new nt;return t.source=this,t},e.create=function(t,r){return new ut(t,r)},e}(nt),ut=function(i){_(e,i);function e(t,r){var n=i.call(this)||this;return n.destination=t,n.source=r,n}return e.prototype.next=function(t){var r,n;(n=(r=this.destination)===null||r===void 0?void 0:r.next)===null||n===void 0||n.call(r,t)},e.prototype.error=function(t){var r,n;(n=(r=this.destination)===null||r===void 0?void 0:r.error)===null||n===void 0||n.call(r,t)},e.prototype.complete=function(){var t,r;(r=(t=this.destination)===null||t===void 0?void 0:t.complete)===null||r===void 0||r.call(t)},e.prototype._subscribe=function(t){var r,n;return(n=(r=this.source)===null||r===void 0?void 0:r.subscribe(t))!==null&&n!==void 0?n:Q},e}(ot),q=function(i){_(e,i);function e(t){var r=i.call(this)||this;return r._value=t,r}return Object.defineProperty(e.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),e.prototype._subscribe=function(t){var r=i.prototype._subscribe.call(this,t);return!r.closed&&t.next(this._value),r},e.prototype.getValue=function(){var t=this,r=t.hasError,n=t.thrownError,s=t._value;if(r)throw n;return this._throwIfClosed(),s},e.prototype.next=function(t){i.prototype.next.call(this,this._value=t)},e}(ot),at=(i=>(i[i.DISABLED=0]="DISABLED",i[i.CRON=1]="CRON",i[i.EVENT=2]="EVENT",i[i.GET=3]="GET",i[i.POST=4]="POST",i[i.PATCH=5]="PATCH",i[i.PUT=6]="PUT",i[i.DELETE=7]="DELETE",i))(at||{});class ct extends d{constructor(t){super();a(this,"api");a(this,"$cache",new q([]));this.api=typeof t=="string"?new p(t):t}get cache(){return this.$cache.value}set cache(t){this.$cache.next(t)}delete(t){return this.api.request({url:`/api/actions/${t}`,method:"DELETE"}).then(()=>{this.cache=this.cache.filter(r=>r._id!=t),this.emit("DELETE",t)})}list(){return this.api.request({url:"/api/actions"}).then(t=>(this.cache=t,this.emit("LIST",t),t))}read(t,r=!1){const n=this.cache.find(s=>s._id==t);return!r&&n?Promise.resolve(n):this.api.request({url:`/api/actions/${t}`}).then(s=>(s&&(this.cache=this.cache.filter(o=>o._id!=t).concat([s])),this.emit("READ",s),s))}run(t,r={}){return this.api.request({url:("/api/actions/run/"+t).replaceAll("//","/"),...r})}runById(t,r={}){const n=typeof t=="string"?t:t._id;return this.api.request({url:"/api/actions/run-by-id/"+n,...r})}update(t){return this.api.request({url:`/api/actions${t._id?`/${t._id}`:""}`,method:"POST",body:t}).then(r=>(r&&(this.cache=this.cache.filter(n=>n._id!=r._id).concat([r])),this.emit("UPDATE",r),r))}}class ht extends d{constructor(t,r){var n;super();a(this,"api");a(this,"storageKey");a(this,"$user",new q(void 0));if(this.opts=r,this.api=typeof t=="string"?new p(t):t,(n=this.opts)!=null&&n.loginUi||(this.opts={...this.opts,loginUi:this.api.url+"/ui/login"}),this.storageKey=`momentum:${new URL(this.api.url).host}`,this.api.on("TOKEN",s=>{var o;(o=this.opts)!=null&&o.persist&&(s?localStorage.setItem(this.storageKey,s):localStorage.removeItem(this.storageKey)),s?this.whoAmI(s,!0):this.user=null}),r!=null&&r.persist){const s=localStorage.getItem(this.storageKey);s?this.api.token=s:this.user=null}else this.user=null}get user(){return this.$user.value}set user(t){if(!K(this.user,t)){const r=t||null;this.$user.next(r),this.emit("USER",r)}}knownHost(t=location.origin){return t.startsWith("/")?Promise.resolve():this.api.request({url:`/api/auth/known-host?host=${encodeURI(new URL(t).origin)}`})}login(t,r){return this.api.request({url:"/api/auth/login",headers:{Authorization:void 0},body:{username:t,password:r}}).then(async n=>(this.api.token=n.token,await this.once("USER")))}loginRedirect(t=location.origin){return new Promise((r,n)=>{var o;const s=window.open(encodeURI(`${(o=this.opts)==null?void 0:o.loginUi}?redirect=postmessage&host=${t}`),"_blank");if(!s)return n("Unable to open login");s.addEventListener("message",u=>{var c;if(!((c=u==null?void 0:u.data)!=null&&c.token))return n("Unknown response from login");this.api.token=u.data.token,r(u.data.token),s.close()})})}logout(){this.api.token=null,this.user=null,this.emit("LOGOUT")}async register(t){const r=await this.api.request({url:"/api/auth/register",body:{...t}});return this.emit("REGISTER",t),r}async reset(t,r){await this.api.request({url:"/api/auth/reset",headers:{Authorization:r?`Bearer ${r}`:void 0},body:{email:r?void 0:t,password:r?t:void 0}}),this.emit(r?"RESET_COMPLETE":"RESET_REQUEST",r||t)}async whoAmI(t,r=!1){t||(t=this.api.token);const n=await this.api.request({url:"/api/auth/whoami",headers:t?{Authorization:`Bearer ${t}`}:void 0});return r&&(this.api.token=t,this.user=(n==null?void 0:n.user)||null,n&&this.emit("LOGIN",n.user)),n}}class lt extends d{constructor(t){super();a(this,"api");this.api=typeof t=="string"?new p(t):t}delete(t,r){return this.api.request({url:`/api/data/${t}/${r}`,method:"DELETE"}).then(()=>this.emit("DELETE",t,r))}get(t,r){return this.api.request({url:`/api/data/${t}${r?`/${r}`:""}`}).then(n=>(this.emit("GET",t,n),n))}raw(t,r,n,s){return this.api.request({url:`/api/data/${t}`,body:{operand:r,query:n,options:s}}).then(o=>(this.emit("RAW",t,o),o))}set(t,r,n=!1){return this.api.request({url:`/api/data/${t}/${r._id||""}`,method:n?"PATCH":"POST",body:r}).then(s=>(this.emit("SET",t,r),s))}}class dt extends d{constructor(t){super();a(this,"api");this.api=typeof t=="string"?new p(t):t}send(t){let r="/api/email";return typeof t.body=="object"&&(r+=`/${t.body.template}`),this.api.request({url:r,body:t}).then(n=>(this.emit("SENT",t),n))}}class pt extends d{constructor(t){super();a(this,"api");this.api=typeof t=="string"?new p(t):t}create(t){return this.api.request({url:`/api/groups/${t.name}`,method:"POST",body:t}).then(r=>(this.emit("CREATE",r),r))}list(){return this.api.request({url:"/api/groups"}).then(t=>(this.emit("LIST",t),t))}read(t){return this.api.request({url:`/api/groups/${t}`}).then(r=>(this.emit("READ",r),r))}update(t){return this.api.request({url:`/api/groups/${t.name}`,method:"PATCH",body:t}).then(r=>(this.emit("UPDATE",r),r))}delete(t){return this.api.request({url:`/api/groups/${t}`,method:"DELETE"}).then(()=>this.emit("DELETE",t))}}class ft{constructor(e,t){a(this,"api");this.api=typeof e=="string"?new p(e):e,t!=null&&t!="NONE"&&(window.addEventListener("error",r=>this.error(r.error.stack)),window.addEventListener("unhandledrejection",async r=>this.error(r.reason.stack)))}buildLog(e,t){return{time:new Date().getTime(),level:e,log:t,ctx:{cores:navigator.hardwareConcurrency,mem:navigator==null?void 0:navigator.deviceMemory,res:[window.innerWidth,window.innerHeight],url:location.href}}}clearClientLogs(){return this.api.request({url:"/api/logs/client",method:"DELETE"})}clearServerLogs(){return this.api.request({url:"/api/logs/server",method:"DELETE"})}clientLogs(e,t){const r=[e?`length=${e}`:void 0,t?`page=${t}`:void 0].filter(n=>!!n).join("&");return this.api.request({url:`/api/logs/client${r?`?${r}`:""}`})}serverLogs(e,t){const r=[e?`length=${e}`:void 0,t?`page=${t}`:void 0].filter(n=>!!n).join("&");return this.api.request({url:`/api/logs/server${r?`?${r}`:""}`})}debug(...e){return this.api.request({url:"/api/logs/client",body:this.buildLog(O.DEBUG,e)}).catch(()=>{})}log(...e){return this.api.request({url:"/api/logs/client",body:this.buildLog(O.LOG,e)}).catch(()=>{})}info(...e){return this.api.request({url:"/api/logs/client",body:this.buildLog(O.INFO,e)}).catch(()=>{})}warn(...e){return this.api.request({url:"/api/logs/client",body:this.buildLog(O.WARN,e)}).catch(()=>{})}error(...e){return this.api.request({url:"/api/logs/client",body:this.buildLog(O.ERROR,e)}).catch(()=>{})}}class mt extends d{constructor(t){super();a(this,"api");this.api=typeof t=="string"?new p(t):t}async handleResponse(t,r){const n=await t.blob();if(r){const s=URL.createObjectURL(n);z(s,r.endsWith(".pdf")?r:r+".pdf"),URL.revokeObjectURL(s)}return this.emit("CREATE",n),n}fromHtml(t,r={}){return this.api.request({url:"/api/pdf",body:{html:t}}).then(n=>this.handleResponse(n,r.download?r.fileName||new Date().toISOString():void 0))}fromUrl(t,r={}){return this.api.request({url:`/api/pdf?url=${t}`}).then(n=>this.handleResponse(n,r.download?r.fileName||new Date().toISOString():void 0))}fromTemplate(t,r,n={}){return this.api.request({url:`/api/pdf/${t}`,body:{data:r}}).then(s=>this.handleResponse(s,n.download?n.fileName||new Date().toISOString():void 0))}}const C=class C{constructor(e){a(this,"api");a(this,"url");a(this,"connection");a(this,"open",!1);this.api=typeof e=="string"?new p(e):e,this.url=this.api.url.replace("http","ws"),this.api.on("TOKEN",()=>this.connect()),this.api.token||this.connect()}close(){console.debug("Disconnected from Momentum"),this.open=!1,this.connection.close()}connect(e=3){var r;((r=this.connection)==null?void 0:r.readyState)<2&&this.connection.close(),this.connection=new WebSocket(this.url+(this.api.token?`?token=${this.api.token}`:""));const t=setTimeout(()=>{this.open||(this.connection.close(),console.error("Momentum connection timeout"),e>0&&this.connect(e-1))},C.timeout);this.connection.onclose=()=>this.open=!1,this.connection.onmessage=this.handle,this.connection.onopen=()=>{this.open=!0,clearTimeout(t),console.debug("Connected to Momentum")}}handle(...e){console.log(e)}send(e,t){this.connection.send(JSON.stringify({token:this.api.token,channel:e,payload:t}))}};a(C,"timeout",1e4);let I=C;class yt extends d{constructor(t){super();a(this,"api");this.api=typeof t=="string"?new p(t):t}delete(t){const r=(t.startsWith("/api/storage/")?t:"/api/storage/"+t).replaceAll("//","/");return this.api.request({url:r,method:"DELETE"}).then(()=>{this.emit("DELETE",r)})}list(t){const r=(t.startsWith("/api/storage/")?t:"/api/storage/"+t).replaceAll("//","/");return this.api.request({url:r+"?list"}).then(n=>(this.emit("LIST",t,n),n))}open(t,r="_blank"){const n=(t.startsWith("/api/storage/")?t:"/api/storage/"+t).replaceAll(/\/{2,}/g,"/"),s=`${this.api.url}${n}${this.api.token?`?token=${this.api.token}`:""}`;return r?(this.emit("OPEN",t),window.open(s,r)):s}mkdir(t){const r=(t.startsWith("/api/storage/")?t:"/api/storage/"+t).replaceAll(/\/{2,}/g,"/");return this.api.request({url:r+"?directory",method:"POST"})}download(t,r={}){return new _t((n,s,o)=>{const u=("/api/storage/"+t).replaceAll("//","/");this.api.request({...r,url:u,skipConverting:!0}).then(c=>{var m;if(!c.ok)return s(c.statusText);const f=c.headers.get("Content-Length"),v=f?parseInt(f,10):0;let R=[],S=0;const E=(m=c.body)==null?void 0:m.getReader(),j=vt=>{if(vt.done){o(1);const $=new Blob(R),V=r.downloadAs||new URL(c.url).pathname.split("/").pop(),wt=URL.createObjectURL($);z(wt,V.includes(".")?V:V+".zip"),URL.revokeObjectURL(wt),this.emit("DOWNLOAD",t,$),n($)}else{const $=vt.value;R.push($),S+=$.length,o(S/v),E.read().then(j)}};E==null||E.read().then(j)})})}async upload(t,r=""){if(t||(t=await new Promise(u=>{const c=document.createElement("input");c.type="file",c.accept=(r==null?void 0:r.accept)||"*",c.style.display="none",c.multiple=!!(r!=null&&r.multiple),c.onblur=c.onchange=async()=>{u(Array.from(c.files)),c.remove()},document.body.appendChild(c),c.click()})),!t||Array.isArray(t)&&!t.length)return[];const s=new FormData,o=("/api/storage/"+(typeof r=="string"?r:r==null?void 0:r.path)).replaceAll("//","/");return(Array.isArray(t)?t:[t]).forEach(u=>s.append("file",u)),this.api.request({url:o,body:s}).then(u=>(this.emit("UPLOAD",u),u))}}class gt extends d{constructor(t){super();a(this,"api");a(this,"listed",!1);a(this,"$cache",new q([]));this.api=typeof t=="string"?new p(t):t}get cache(){return this.$cache.value}set cache(t){this.$cache.next(t)}delete(t){return this.api.request({url:`/api/users/${t}`,method:"DELETE"}).then(()=>{this.cache=this.cache.filter(r=>r.username!=t),this.emit("DELETE",t)})}list(t=!1){return!t&&this.listed?Promise.resolve(this.cache):this.api.request({url:"/api/users"}).then(r=>(this.cache=r,this.listed=!0,this.emit("LIST",r),r))}read(t,r=!1){if(!r){const n=this.cache.find(s=>s.username==t);if(n)return Promise.resolve(n)}return this.api.request({url:`/api/users/${t}`}).then(n=>(n&&(this.cache={...this.cache,[n.username]:n}),this.emit("READ",n),n))}update(t){return this.api.request({url:`/api/users/${t.username}`,method:"PATCH",body:t}).then(r=>(r&&(this.cache=this.cache.filter(n=>n.username!=t.username).concat([r])),this.emit("UPDATE",r),r))}}class Et extends d{constructor(t){super();a(this,"api");a(this,"$cache",new q({}));this.api=typeof t=="string"?new p(t):t}get cache(){return this.$cache.value}set cache(t){this.$cache.next(t)}list(t=!1){return this.api.request({url:"/api/settings"+(t?"?detailed":"")}).then(r=>(this.cache=t?Object.values(r).reduce((n,s)=>({...n,[s.key]:s.value}),{}):r,this.emit("LIST",r),r))}delete(t){return this.api.request({url:`/api/settings/${t}`,method:"DELETE"}).then(()=>{this.cache={...this.cache,[t]:void 0},this.emit("DELETE",t)})}read(t,r=!1){return!r&&this.cache[t]?Promise.resolve(this.cache[t]):this.api.request({url:`/api/settings/${t}`}).then(n=>(n&&(this.cache={...this.cache,[n.key]:n}),this.emit("READ",n),n))}update(t){return this.api.request({url:`/api/settings/${t.key}`,body:t}).then(r=>(r&&(this.cache={...this.cache,[r.key]:r.value}),this.emit("UPDATE",r),r))}}class bt extends d{constructor(t){super();a(this,"api");this.api=typeof t=="string"?new p(t):t}delete(t){return this.api.request({url:`/api/static/${t}`,method:"DELETE"}).then(()=>{this.emit("DELETE",t)})}list(t){const r=("/api/static/"+t).replaceAll("//","/");return this.api.request({url:r}).then(n=>(this.emit("LIST",t,n),n))}upload(t,r="/"){const n=new FormData;return(Array.isArray(t)?t:[t]).forEach(s=>n.append("file",s)),this.api.request({url:"/api/static"+r,body:n}).then(s=>(this.emit("UPLOAD",s),s))}}class Nt extends d{constructor(t,r){super();a(this,"api");a(this,"actions");a(this,"auth");a(this,"data");a(this,"email");a(this,"groups");a(this,"logger");a(this,"pdf");a(this,"settings");a(this,"socket");a(this,"static");a(this,"storage");a(this,"users");this.api=new p(t,r==null?void 0:r.api),this.actions=new ct(this.api),this.auth=new ht(this.api,{persist:(r==null?void 0:r.persist)??!0,loginUi:r==null?void 0:r.loginUi}),this.data=new lt(this.api),this.email=new dt(this.api),this.groups=new pt(this.api),this.logger=new ft(this.api,r==null?void 0:r.logLevel),this.pdf=new mt(this.api),this.settings=new Et(this.api),this.static=new bt(this.api),this.storage=new yt(this.api),this.users=new gt(this.api),r!=null&&r.socket&&(this.socket=new I(this.api)),this.api.on("*",(n,...s)=>this.emit(`API::${n}`,...s)),this.actions.on("*",(n,...s)=>this.emit(`ACTIONS::${n}`,...s)),this.auth.on("*",(n,...s)=>this.emit(`AUTH::${n}`,...s)),this.data.on("*",(n,...s)=>this.emit(`DATA::${n}`,...s)),this.email.on("*",(n,...s)=>this.emit(`EMAIL::${n}`,...s)),this.groups.on("*",(n,...s)=>this.emit(`GROUPS::${n}`,...s)),this.pdf.on("*",(n,...s)=>this.emit(`PDF::${n}`,...s)),this.settings.on("*",(n,...s)=>this.emit(`SETTINGS::${n}`,...s)),this.static.on("*",(n,...s)=>this.emit(`STATIC::${n}`,...s)),this.storage.on("*",(n,...s)=>this.emit(`STORAGE::${n}`,...s)),this.users.on("*",(n,...s)=>this.emit(`USERS::${n}`,...s)),this.users.on("*",(n,...s)=>{var o;if(Array.isArray(s[0])){const u=s[0].find(c=>{var f;return c._id==((f=this.auth.user)==null?void 0:f._id)});u&&(this.auth.user=u)}else s[0]._id==((o=this.auth.user)==null?void 0:o._id)&&(this.auth.user=s[0])})}}h.ActionType=at,h.Actions=ct,h.Api=p,h.Auth=ht,h.Data=lt,h.Email=dt,h.Groups=pt,h.Logger=ft,h.Momentum=Nt,h.Pdf=mt,h.Settings=Et,h.Socket=I,h.Static=bt,h.Storage=yt,h.Users=gt,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
4
4
  //# sourceMappingURL=momentum.cjs.map