AUI.add("liferay-poller",(e=>{const t=e.Object,n=Math.ceil(Math.random()*Number.MAX_SAFE_INTEGER);let s=!1,i=null,r=!1,l=0,o=0;const c=[1,2,3,4,5,7,10],a=function _getEncryptedUserId(){return i};let u=!1,p=!1;const d=c.length-1,_={},m={browserKey:n,companyId:themeDisplay.getCompanyId(),portletIdsMap:_,startPolling:!0};let f=null;const h={};let R=c[0];const y=[];let g=!1,C=null,U=themeDisplay.getPathContext()+"/poller";const q=U+"/receive",I=U+"/send",S={addListener(e,t,n){h[e]={initialRequest:!0,listener:t,scope:n},s||(s=!0,_receive())},cancelCustomDelay(){f=null},getDelay:()=>(null!==f?R=f:o<=d&&(R=c[o],l++,3===l&&(o++,l=0)),1e3*R),getReceiveUrl:_getReceiveUrl,getSendUrl:_getSendUrl,init(e){this.setEncryptedUserId(e.encryptedUserId),this.setSupportsComet(e.supportsComet)},isSupportsComet:()=>r,processResponse:_processResponse,removeListener(e){e in h&&delete h[e],t.keys(h).length||(s=!1,_cancelRequestTimer())},resume(){g=!1,_createRequestTimer()},setCustomDelay(e){f=null===e?e:e/1e3},setDelay(e){R=e/1e3},setEncryptedUserId(e){i=e},setSupportsComet(e){r=e},setUrl(e){U=e},submitRequest(e,t,n){if(!u&&e in h){for(const e in t)if(Object.prototype.hasOwnProperty.call(t,e)){let n=t[e];n.replace&&(n=n.replace("{","[$OPEN_CURLY_BRACE$]"),n=n.replace("}","[$CLOSE_CURLY_BRACE$]"),t[e]=n)}const s={data:t,portletId:e};n&&(s.chunkId=n),y.push(s),_send()}},suspend(){_cancelRequestTimer(),g=!0},url:U};function _cancelRequestTimer(){clearTimeout(C),C=null}function _createRequestTimer(){_cancelRequestTimer(),s&&(S.isSupportsComet()?_receive():C=setTimeout(_receive,S.getDelay()))}function _getReceiveUrl(){return q}function _getSendUrl(){return I}function _processResponse(e,t){const n=JSON.parse(t.responseText);let s=!1;if(Array.isArray(n)){const e=n.shift();for(let e=0;e<n.length;e++){const t=n[e].payload,i=t.data,r=t.portletId,l=h[r];if(l){const e=_[r];i&&e&&(i.initialRequest=l.initialRequest),l.listener.call(l.scope||S,i,t.chunkId),i&&i.pollerHintHighConnectivity&&(R=c[0],o=0),l.initialRequest&&e&&(s=!0,l.initialRequest=!1)}}"startPolling"in m&&delete m.startPolling,s&&_send(),e.suspendPolling?function _freezeConnection(){u=!0,_cancelRequestTimer()}():_thawConnection()}}function _receive(){if(!g&&!u){m.userId=a(),m.timestamp=(new Date).getTime(),t.each(h,_updatePortletIdsMap);const e=JSON.stringify([m]),n=new URLSearchParams;n.append("pollerRequest",e),Liferay.Util.fetch(_getReceiveUrl(),{body:n,method:"POST"}).then((e=>e.text())).then((e=>{_processResponse(0,{responseText:e})}))}}function _send(){if(s&&!p&&y.length&&!g&&!u){p=!0;const e=y.shift();m.userId=a(),m.timestamp=(new Date).getTime(),t.each(h,_updatePortletIdsMap);const n=JSON.stringify([m].concat(e)),s=new URLSearchParams;s.append("pollerRequest",n),Liferay.Util.fetch(_getSendUrl(),{body:s,method:"POST"}).then((e=>e.text())).then(_sendComplete)}}function _sendComplete(){!function _releaseLock(){p=!1}(),_send()}function _thawConnection(){u=!1,_createRequestTimer()}function _updatePortletIdsMap(e,t){_[t]=e.initialRequest}e.getWin().on("focus",(()=>{m.startPolling=!0,_thawConnection()})),Liferay.Poller=S}),"",{requires:["aui-base","json"]});
//# sourceMappingURL=poller.js.map