UNPKG

@sirenapp/js-sdk

Version:

JavaScript middleware designed to streamline interaction for managing and displaying in-app notifications seamlessly

2 lines (1 loc) 24.3 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).test_notification={})}(this,(function(t){"use strict";var e=function(t,r){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])},e(t,r)};var r,n=function(){return n=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},n.apply(this,arguments)};function o(t,e,r,n){return new(r||(r=Promise))((function(o,i){function a(t){try{c(n.next(t))}catch(t){i(t)}}function s(t){try{c(n.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,s)}c((n=n.apply(t,e||[])).next())}))}function i(t,e){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(s){return function(c){return function(s){if(r)throw new TypeError("Generator is already executing.");for(;i&&(i=0,s[0]&&(a=0)),a;)try{if(r=1,n&&(o=2&s[0]?n.return:s[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,s[1])).done)return o;switch(n=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return a.label++,{value:s[1],done:!1};case 5:a.label++,n=s[1],s=[0];continue;case 7:s=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){a=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){a.label=s[1];break}if(6===s[0]&&a.label<o[1]){a.label=o[1],o=s;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(s);break}o[2]&&a.ops.pop(),a.trys.pop();continue}s=e.call(t,a)}catch(t){s=[6,t],n=0}finally{r=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}"function"==typeof SuppressedError&&SuppressedError;var a,s,c,u,l="https://api.trysiren.io/";!function(t){t.GET="GET",t.POST="POST",t.PATCH="PATCH",t.DELETE="DELETE"}(a||(a={})),function(t){t.MARK_AS_READ="MARK_AS_READ",t.MARK_AS_DELETED="MARK_AS_DELETED"}(s||(s={})),function(t){t.SILENT="silent",t.INFO="info",t.WARN="warn",t.ERROR="error",t.FATAL="fatal"}(c||(c={})),function(t){t.PENDING="PENDING",t.SUCCESS="SUCCESS",t.FAILED="FAILED"}(u||(u={}));var d,h={INVALID_CREDENTIALS:{code:"INVALID_CREDENTIALS",message:"Failed to authenticate given credentials"},MISSING_PARAMETER:{code:"MISSING_PARAMETER",message:"Missing required parameter"},UNAUTHORIZED_OPERATION:{code:"UNAUTHORIZED_OPERATION",message:"This operation require valid credentials"},INVALID_ERROR_FUNCTION:{code:"INVALID_ERROR_FUNCTION",message:"Invalid error function"},GENERIC_API_ERROR:{code:"API_ERROR",message:"Something went wrong"},AUTHENTICATION_PENDING:{code:"AUTHENTICATION_PENDING",message:"Authentication in progress"},INVALID_CALLBACK_FUNCTION:{code:"INVALID_CALLBACK_FUNCTION",message:"Invalid callback function"}};!function(t){t.ERROR="ERROR",t.CONFIG_ERROR="CONFIG_ERROR",t.NETWORK_ERROR="NETWORK_ERROR"}(d||(d={}));var f;!function(t){t.FETCH_COUNT="FETCH_COUNT",t.FETCH_LIST="FETCH_LIST",t.BULK_DELETE="BULK_DELETE",t.BULK_READ="BULK_READ",t.DELETE_BY_ID="DELETE_BY_ID",t.READ_BY_ID="READ_BY_ID",t.MARK_AS_VIEWED="MARK_AS_VIEWED",t.VERIFY_TOKEN="VERIFY_TOKEN"}(f||(f={}));var p,y=((r={})[f.FETCH_LIST]={code:"NOTIFICATION_FETCH_FAILED",message:"Failed to fetch notifications"},r[f.FETCH_COUNT]={code:"UNVIEWED_COUNT_FETCH_FAILED",message:"Failed to fetch unviewed notifications count"},r[f.READ_BY_ID]={code:"MARK_AS_READ_FAILED",message:"Failed to mark notification as read"},r[f.DELETE_BY_ID]={code:"DELETE_FAILED",message:"Failed to delete notification"},r[f.MARK_AS_VIEWED]={code:"MARK_ALL_AS_VIEWED_FAILED",message:"Failed to mark notification as viewed"},r[f.BULK_DELETE]={code:"BULK_DELETE_FAILED",message:"Bulk deletion of notifications failed"},r[f.BULK_READ]={code:"MARK_ALL_AS_READ_FAILED",message:"Failed to mark all notifications as read"},r[f.VERIFY_TOKEN]={code:"AUTHENTICATION_FAILED",message:"Failed to authenticate given credentials"},r);!function(t){t.NOTIFICATION="NOTIFICATIONS",t.UNVIEWED_COUNT="UNVIEWED_COUNT"}(p||(p={}));var E=function(t){function r(e,r,n){var o=t.call(this,r)||this;return o.getError=function(){return o.errorObject},o.name=d[e],o.code=n,o.errorObject={Type:o.name,Code:o.code,Message:o.message},console.error(o.errorObject),o.getError=o.getError.bind(o),o}return function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}(r,t),r}(Error),v="undefined"!=typeof globalThis&&globalThis||"undefined"!=typeof self&&self||"undefined"!=typeof global&&global||{},A="URLSearchParams"in v,R="Symbol"in v&&"iterator"in Symbol,I="FileReader"in v&&"Blob"in v&&function(){try{return new Blob,!0}catch(t){return!1}}(),b="FormData"in v,_="ArrayBuffer"in v;if(_)var T=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],m=ArrayBuffer.isView||function(t){return t&&T.indexOf(Object.prototype.toString.call(t))>-1};function w(t){if("string"!=typeof t&&(t=String(t)),/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(t)||""===t)throw new TypeError('Invalid character in header field name: "'+t+'"');return t.toLowerCase()}function O(t){return"string"!=typeof t&&(t=String(t)),t}function N(t){var e={next:function(){var e=t.shift();return{done:void 0===e,value:e}}};return R&&(e[Symbol.iterator]=function(){return e}),e}function g(t){this.map={},t instanceof g?t.forEach((function(t,e){this.append(e,t)}),this):Array.isArray(t)?t.forEach((function(t){if(2!=t.length)throw new TypeError("Headers constructor: expected name/value pair to be length 2, found"+t.length);this.append(t[0],t[1])}),this):t&&Object.getOwnPropertyNames(t).forEach((function(e){this.append(e,t[e])}),this)}function D(t){if(!t._noBody)return t.bodyUsed?Promise.reject(new TypeError("Already read")):void(t.bodyUsed=!0)}function C(t){return new Promise((function(e,r){t.onload=function(){e(t.result)},t.onerror=function(){r(t.error)}}))}function L(t){var e=new FileReader,r=C(e);return e.readAsArrayBuffer(t),r}function F(t){if(t.slice)return t.slice(0);var e=new Uint8Array(t.byteLength);return e.set(new Uint8Array(t)),e.buffer}function S(){return this.bodyUsed=!1,this._initBody=function(t){var e;this.bodyUsed=this.bodyUsed,this._bodyInit=t,t?"string"==typeof t?this._bodyText=t:I&&Blob.prototype.isPrototypeOf(t)?this._bodyBlob=t:b&&FormData.prototype.isPrototypeOf(t)?this._bodyFormData=t:A&&URLSearchParams.prototype.isPrototypeOf(t)?this._bodyText=t.toString():_&&I&&((e=t)&&DataView.prototype.isPrototypeOf(e))?(this._bodyArrayBuffer=F(t.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):_&&(ArrayBuffer.prototype.isPrototypeOf(t)||m(t))?this._bodyArrayBuffer=F(t):this._bodyText=t=Object.prototype.toString.call(t):(this._noBody=!0,this._bodyText=""),this.headers.get("content-type")||("string"==typeof t?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type?this.headers.set("content-type",this._bodyBlob.type):A&&URLSearchParams.prototype.isPrototypeOf(t)&&this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8"))},I&&(this.blob=function(){var t=D(this);if(t)return t;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(new Blob([this._bodyArrayBuffer]));if(this._bodyFormData)throw new Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))}),this.arrayBuffer=function(){if(this._bodyArrayBuffer){var t=D(this);return t||(ArrayBuffer.isView(this._bodyArrayBuffer)?Promise.resolve(this._bodyArrayBuffer.buffer.slice(this._bodyArrayBuffer.byteOffset,this._bodyArrayBuffer.byteOffset+this._bodyArrayBuffer.byteLength)):Promise.resolve(this._bodyArrayBuffer))}if(I)return this.blob().then(L);throw new Error("could not read as ArrayBuffer")},this.text=function(){var t,e,r,n,o,i=D(this);if(i)return i;if(this._bodyBlob)return t=this._bodyBlob,e=new FileReader,r=C(e),n=/charset=([A-Za-z0-9_-]+)/.exec(t.type),o=n?n[1]:"utf-8",e.readAsText(t,o),r;if(this._bodyArrayBuffer)return Promise.resolve(function(t){for(var e=new Uint8Array(t),r=new Array(e.length),n=0;n<e.length;n++)r[n]=String.fromCharCode(e[n]);return r.join("")}(this._bodyArrayBuffer));if(this._bodyFormData)throw new Error("could not read FormData body as text");return Promise.resolve(this._bodyText)},b&&(this.formData=function(){return this.text().then(P)}),this.json=function(){return this.text().then(JSON.parse)},this}g.prototype.append=function(t,e){t=w(t),e=O(e);var r=this.map[t];this.map[t]=r?r+", "+e:e},g.prototype.delete=function(t){delete this.map[w(t)]},g.prototype.get=function(t){return t=w(t),this.has(t)?this.map[t]:null},g.prototype.has=function(t){return this.map.hasOwnProperty(w(t))},g.prototype.set=function(t,e){this.map[w(t)]=O(e)},g.prototype.forEach=function(t,e){for(var r in this.map)this.map.hasOwnProperty(r)&&t.call(e,this.map[r],r,this)},g.prototype.keys=function(){var t=[];return this.forEach((function(e,r){t.push(r)})),N(t)},g.prototype.values=function(){var t=[];return this.forEach((function(e){t.push(e)})),N(t)},g.prototype.entries=function(){var t=[];return this.forEach((function(e,r){t.push([r,e])})),N(t)},R&&(g.prototype[Symbol.iterator]=g.prototype.entries);var U=["CONNECT","DELETE","GET","HEAD","OPTIONS","PATCH","POST","PUT","TRACE"];function B(t,e){if(!(this instanceof B))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');var r,n,o=(e=e||{}).body;if(t instanceof B){if(t.bodyUsed)throw new TypeError("Already read");this.url=t.url,this.credentials=t.credentials,e.headers||(this.headers=new g(t.headers)),this.method=t.method,this.mode=t.mode,this.signal=t.signal,o||null==t._bodyInit||(o=t._bodyInit,t.bodyUsed=!0)}else this.url=String(t);if(this.credentials=e.credentials||this.credentials||"same-origin",!e.headers&&this.headers||(this.headers=new g(e.headers)),this.method=(r=e.method||this.method||"GET",n=r.toUpperCase(),U.indexOf(n)>-1?n:r),this.mode=e.mode||this.mode||null,this.signal=e.signal||this.signal||function(){if("AbortController"in v)return(new AbortController).signal}(),this.referrer=null,("GET"===this.method||"HEAD"===this.method)&&o)throw new TypeError("Body not allowed for GET or HEAD requests");if(this._initBody(o),!("GET"!==this.method&&"HEAD"!==this.method||"no-store"!==e.cache&&"no-cache"!==e.cache)){var i=/([?&])_=[^&]*/;if(i.test(this.url))this.url=this.url.replace(i,"$1_="+(new Date).getTime());else{this.url+=(/\?/.test(this.url)?"&":"?")+"_="+(new Date).getTime()}}}function P(t){var e=new FormData;return t.trim().split("&").forEach((function(t){if(t){var r=t.split("="),n=r.shift().replace(/\+/g," "),o=r.join("=").replace(/\+/g," ");e.append(decodeURIComponent(n),decodeURIComponent(o))}})),e}function k(t,e){if(!(this instanceof k))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');if(e||(e={}),this.type="default",this.status=void 0===e.status?200:e.status,this.status<200||this.status>599)throw new RangeError("Failed to construct 'Response': The status provided (0) is outside the range [200, 599].");this.ok=this.status>=200&&this.status<300,this.statusText=void 0===e.statusText?"":""+e.statusText,this.headers=new g(e.headers),this.url=e.url||"",this._initBody(t)}B.prototype.clone=function(){return new B(this,{body:this._bodyInit})},S.call(B.prototype),S.call(k.prototype),k.prototype.clone=function(){return new k(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new g(this.headers),url:this.url})},k.error=function(){var t=new k(null,{status:200,statusText:""});return t.ok=!1,t.status=0,t.type="error",t};var x=[301,302,303,307,308];k.redirect=function(t,e){if(-1===x.indexOf(e))throw new RangeError("Invalid status code");return new k(null,{status:e,headers:{location:t}})};var M=v.DOMException;try{new M}catch(t){(M=function(t,e){this.message=t,this.name=e;var r=Error(t);this.stack=r.stack}).prototype=Object.create(Error.prototype),M.prototype.constructor=M}function j(t,e){return new Promise((function(r,n){var o=new B(t,e);if(o.signal&&o.signal.aborted)return n(new M("Aborted","AbortError"));var i=new XMLHttpRequest;function a(){i.abort()}if(i.onload=function(){var t,e,n={statusText:i.statusText,headers:(t=i.getAllResponseHeaders()||"",e=new g,t.replace(/\r?\n[\t ]+/g," ").split("\r").map((function(t){return 0===t.indexOf("\n")?t.substr(1,t.length):t})).forEach((function(t){var r=t.split(":"),n=r.shift().trim();if(n){var o=r.join(":").trim();try{e.append(n,o)}catch(t){console.warn("Response "+t.message)}}})),e)};0===o.url.indexOf("file://")&&(i.status<200||i.status>599)?n.status=200:n.status=i.status,n.url="responseURL"in i?i.responseURL:n.headers.get("X-Request-URL");var a="response"in i?i.response:i.responseText;setTimeout((function(){r(new k(a,n))}),0)},i.onerror=function(){setTimeout((function(){n(new TypeError("Network request failed"))}),0)},i.ontimeout=function(){setTimeout((function(){n(new TypeError("Network request timed out"))}),0)},i.onabort=function(){setTimeout((function(){n(new M("Aborted","AbortError"))}),0)},i.open(o.method,function(t){try{return""===t&&v.location.href?v.location.href:t}catch(e){return t}}(o.url),!0),"include"===o.credentials?i.withCredentials=!0:"omit"===o.credentials&&(i.withCredentials=!1),"responseType"in i&&(I?i.responseType="blob":_&&(i.responseType="arraybuffer")),e&&"object"==typeof e.headers&&!(e.headers instanceof g||v.Headers&&e.headers instanceof v.Headers)){var s=[];Object.getOwnPropertyNames(e.headers).forEach((function(t){s.push(w(t)),i.setRequestHeader(t,O(e.headers[t]))})),o.headers.forEach((function(t,e){-1===s.indexOf(e)&&i.setRequestHeader(e,t)}))}else o.headers.forEach((function(t,e){i.setRequestHeader(e,t)}));o.signal&&(o.signal.addEventListener("abort",a),i.onreadystatechange=function(){4===i.readyState&&o.signal.removeEventListener("abort",a)}),i.send(void 0===o._bodyInit?null:o._bodyInit)}))}j.polyfill=!0,v.fetch||(v.fetch=j,v.Headers=g,v.Request=B,v.Response=k);var V=function(t,e,r){var n,o,i,a=r?(n=t,o=r,i=Object.entries(o).map((function(t){return"".concat(encodeURIComponent(t[0]),"=").concat(encodeURIComponent(t[1]))})).join("&"),n.includes("?")?"".concat(n,"&").concat(i):"".concat(n,"?").concat(i)):t;return"undefined"!=typeof window&&"fetch"in window?window.fetch(a,e):j(a,e)};var K=function(t,e,r){return new E(t,e,r).getError()},H=function(t){return t?y[t]:h.GENERIC_API_ERROR};function G(t,e){var r=this,s=t.headers,u=void 0===s?[]:s,d=t.errorLevel,h=void 0===d?c.WARN:d,f=t.method,p=void 0===f?a.GET:f,y=t.path,E=t.queryParams,v=t.token,A=t.onError,R=t.operation,I=n({method:p,mode:"cors",cache:"default",credentials:"same-origin",headers:n({Accept:"application/json, text/plain, */*",Authorization:"Bearer ".concat(v),"Content-Type":p===a.POST||p===a.PATCH?"application/json":"text/plain","Access-Control-Allow-Origin":"*"},u),redirect:"follow",referrerPolicy:"no-referrer-when-downgrade"},e&&(p===a.POST||p===a.PATCH)&&{body:JSON.stringify(e)}),b="".concat(l).concat(y);return V(b,I,E).then((function(t){return o(r,void 0,void 0,(function(){var e,r,n,o,a,s,c;return i(this,(function(i){switch(i.label){case 0:return[4,t.json()];case 1:return e=i.sent(),t.ok?[2,{data:e.data,error:null,meta:null!==(a=e.meta)&&void 0!==a?a:null}]:(r=H(R),n=r.code,o=r.message,[2,{data:null,error:W((null===(s=null==e?void 0:e.error)||void 0===s?void 0:s.message)||o,h,(null===(c=null==e?void 0:e.error)||void 0===c?void 0:c.errorCode)||n,A)}])}}))}))})).catch((function(){var e=H(R),r=e.code,n=e.message;return{data:null,error:W(t.errorMessage||n,h,r,A)}}))}function W(t,e,r,n){switch(e){case c.SILENT:return null;case c.INFO:case c.WARN:return console[e](t),null;case c.ERROR:var o=new E(d.ERROR,t,r).getError();return n&&n(o),o;default:return null}}function q(t,e){return G(n(n({},t),{method:a.GET}),e)}function Y(t,e){return G(n(n({},t),{method:a.PATCH}),e)}var z,Z=function(t,e){return q({path:"api/v2/in-app/recipients/".concat(e),token:t,errorLevel:c.ERROR,operation:f.FETCH_COUNT})},J=function(t,e,r){return q({path:"api/v2/in-app/recipients/".concat(e,"/notifications"),token:t,errorLevel:c.ERROR,queryParams:r,operation:f.FETCH_LIST})},X=((z={})[s.MARK_AS_READ]=f.BULK_READ,z[s.MARK_AS_DELETED]=f.BULK_DELETE,z),$=function(t,e,r){var o,i="api/v2/in-app/recipients/".concat(e,"/notifications/bulk-update"),u=r.category,l=r.isRead,d=function(t,e){var r={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(r[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)e.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(t,n[o])&&(r[n[o]]=t[n[o]])}return r}(r,["category","isRead"]);u&&(i="".concat(i,"?category=").concat(u)),void 0!==l&&(i=i.includes("?")?"".concat(i,"&isRead=").concat(l):"".concat(i,"?isRead=").concat(l));var h=function(t,e){return G(n(n({},t),{method:a.POST}),e)}({path:i,token:t,errorLevel:c.ERROR,operation:X[null!==(o=r.operation)&&void 0!==o?o:s.MARK_AS_READ]},d);return h},Q=function(t,e,r){var o,i,s="api/v2/in-app/recipients/".concat(e,"/notifications/").concat(r);return o={path:s,token:t,errorLevel:c.ERROR,operation:f.DELETE_BY_ID},G(n(n({},o),{method:a.DELETE}),i)},tt=function(){function t(t){var e,r,n,o,i=t.token,a=t.recipientId,s=t.onError,c=t.actionCallbacks;this.latestNotification=null;var u=function(t,e,r,n){return r&&"function"==typeof r?t&&"string"==typeof t&&e&&"string"==typeof e?(null==n?void 0:n.onEventReceive)&&"function"!=typeof n.onEventReceive?(K(d.CONFIG_ERROR,h.INVALID_CALLBACK_FUNCTION.message,h.INVALID_CALLBACK_FUNCTION.code),{data:null,error:h.INVALID_CALLBACK_FUNCTION.message,code:h.INVALID_CALLBACK_FUNCTION.code}):{data:{token:t,recipientId:e,onError:r},error:null,code:null}:(K(d.CONFIG_ERROR,h.INVALID_CREDENTIALS.message,h.INVALID_CREDENTIALS.code),{data:null,error:h.INVALID_CREDENTIALS.message,code:h.INVALID_CREDENTIALS.code}):{data:null,error:h.INVALID_ERROR_FUNCTION.message,code:h.INVALID_ERROR_FUNCTION.code}}(i,a,s,c);if(null===u.data)throw new E(d.CONFIG_ERROR,null!==(e=u.error)&&void 0!==e?e:"",null!==(r=u.code)&&void 0!==r?r:"");this.token=(null===(n=u.data)||void 0===n?void 0:n.token)||"",this.recipientId=(null===(o=u.data)||void 0===o?void 0:o.recipientId)||"",this.actionCallbacks=c,this.bindMethods()}return t.prototype.bindMethods=function(){this.fetchUnviewedNotificationsCount=this.fetchUnviewedNotificationsCount.bind(this),this.fetchAllNotifications=this.fetchAllNotifications.bind(this),this.startRealTimeFetch=this.startRealTimeFetch.bind(this),this.stopRealTimeFetch=this.stopRealTimeFetch.bind(this),this.markAsReadById=this.markAsReadById.bind(this),this.markAsReadByDate=this.markAsReadByDate.bind(this),this.deleteById=this.deleteById.bind(this),this.deleteByDate=this.deleteByDate.bind(this),this.markAllAsViewed=this.markAllAsViewed.bind(this)},t.prototype.fetchUnviewedNotificationsCount=function(){return o(this,void 0,void 0,(function(){var t;return i(this,(function(e){switch(e.label){case 0:return[4,Z(this.token,this.recipientId)];case 1:return(t=e.sent()).data?[2,{data:{unviewedCount:t.data.totalUnviewed},error:null}]:[2,{data:null,error:t.error}]}}))}))},t.prototype.fetchAllNotifications=function(t){return o(this,void 0,void 0,(function(){var e,r,n;return i(this,(function(o){switch(o.label){case 0:return[4,J(this.token,this.recipientId,t)];case 1:return(e=o.sent())&&(null===(r=null==e?void 0:e.data)||void 0===r?void 0:r.length)&&(null===(n=null==e?void 0:e.data)||void 0===n?void 0:n.length)>0&&(this.latestNotification=e.data[0]),[2,e]}}))}))},t.prototype.startRealTimeFetch=function(t){var e=t.eventType,r=t.params;switch(e){case p.NOTIFICATION:this.startRealTimeNotificationFetch(null!=r?r:{});break;case p.UNVIEWED_COUNT:this.startRealTimeUnviewedCountFetch()}},t.prototype.stopRealTimeFetch=function(t){switch(t){case p.NOTIFICATION:this.stopRealTimeNotificationFetch();break;case p.UNVIEWED_COUNT:this.stopRealTimeUnviewedCountFetch()}},t.prototype.markAsReadById=function(t){return o(this,void 0,void 0,(function(){var e;return i(this,(function(r){switch(r.label){case 0:return t?[4,(n=this.token,o=this.recipientId,i=t,Y({path:"api/v2/in-app/recipients/".concat(o,"/notifications/").concat(i),token:n,errorLevel:c.ERROR,operation:f.READ_BY_ID},{isRead:!0,isDelivered:!1}))]:[3,2];case 1:return e=r.sent(),[3,3];case 2:e=this.emitMissingParameterError(),r.label=3;case 3:return[2,e]}var n,o,i}))}))},t.prototype.markAsReadByDate=function(t){return o(this,void 0,void 0,(function(){var e,r,n,o,a;return i(this,(function(i){switch(i.label){case 0:return r=(e=t||{}).startDate,n=e.category,r?(a={until:r,operation:s.MARK_AS_READ},n&&(a.category=n),[4,$(this.token,this.recipientId,a)]):[3,2];case 1:return o=i.sent(),[3,3];case 2:o=this.emitMissingParameterError(),i.label=3;case 3:return[2,o]}}))}))},t.prototype.deleteById=function(t){return o(this,void 0,void 0,(function(){var e;return i(this,(function(r){switch(r.label){case 0:return t?[4,Q(this.token,this.recipientId,t)]:[3,2];case 1:return e=r.sent(),[3,3];case 2:e=this.emitMissingParameterError(),r.label=3;case 3:return[2,e]}}))}))},t.prototype.deleteByDate=function(t){return o(this,void 0,void 0,(function(){var e,r,n,o,a,c;return i(this,(function(i){switch(i.label){case 0:return r=(e=t||{}).startDate,n=e.isRead,o=e.category,r?(c={until:r,operation:s.MARK_AS_DELETED},void 0!==n&&(c.isRead=n),o&&(c.category=o),[4,$(this.token,this.recipientId,c)]):[3,2];case 1:return a=i.sent(),[3,3];case 2:a=this.emitMissingParameterError(),i.label=3;case 3:return[2,a]}}))}))},t.prototype.markAllAsViewed=function(t){return o(this,void 0,void 0,(function(){var e;return i(this,(function(r){switch(r.label){case 0:return t?[4,(n=this.token,o=this.recipientId,i={lastOpenedAt:t},Y({path:"api/v2/in-app/recipients/".concat(o),token:n,errorLevel:c.ERROR,operation:f.MARK_AS_VIEWED},i))]:[3,2];case 1:return e=r.sent(),[3,3];case 2:e=this.emitMissingParameterError(),r.label=3;case 3:return[2,e]}var n,o,i}))}))},t.prototype.startRealTimeNotificationFetch=function(t){var e,r,a,s=this,c=null!==(a=null!==(e=t.start)&&void 0!==e?e:null===(r=this.latestNotification)||void 0===r?void 0:r.createdAt)&&void 0!==a?a:(new Date).toISOString();this.notificationFetchIntervalId=window.setInterval((function(){return o(s,void 0,void 0,(function(){var e,r,o;return i(this,(function(i){switch(i.label){case 0:return[4,J(this.token,this.recipientId,n(n({},t),c&&{start:(a=c,s=new Date(a),s.setMilliseconds(s.getMilliseconds()+1),s.toISOString())}))];case 1:return(e=i.sent())&&(null===(r=e.data)||void 0===r?void 0:r.length)&&e.data.length>0&&(c=e.data[0].createdAt),(null===(o=this.actionCallbacks)||void 0===o?void 0:o.onEventReceive)&&this.actionCallbacks.onEventReceive(e,p.NOTIFICATION),[2]}var a,s}))}))}),5e3)},t.prototype.stopRealTimeNotificationFetch=function(){clearInterval(this.notificationFetchIntervalId)},t.prototype.startRealTimeUnviewedCountFetch=function(){var t=this;this.unViewedCountFetchIntervalId=window.setInterval((function(){return o(t,void 0,void 0,(function(){var t,e;return i(this,(function(r){switch(r.label){case 0:return[4,Z(this.token,this.recipientId)];case 1:return t=r.sent(),(null===(e=this.actionCallbacks)||void 0===e?void 0:e.onEventReceive)&&this.actionCallbacks.onEventReceive({data:t.data},p.UNVIEWED_COUNT),[2]}}))}))}),5e3)},t.prototype.stopRealTimeUnviewedCountFetch=function(){clearInterval(this.unViewedCountFetchIntervalId)},t.prototype.emitMissingParameterError=function(){return{data:null,error:K(d.ERROR,h.MISSING_PARAMETER.message,h.MISSING_PARAMETER.code)}},t}();window&&(window.Siren=tt),t.Siren=tt}));