import{F as t}from"./p-3478f368.js";class s{_endpoint;_market;_language;_currentUrl;fetchHelper=new t("PnChatService");_startChatPath=`/api/sfchat/start-chat`;_fetchChatMessagesPath=`/api/sfchat/get-conversation-entries`;_sendTextPath=`/api/sfchat/send-text-message`;_sendChoicePath=`/api/sfchat/send-choice-message`;_updateLastMessagePath=`/api/sfchat/update-last-message-id`;_hasNewMessagesPath=`/api/sfchat/has-new-messages`;constructor(t,s,e){this._endpoint=this.formatEndpoint(t);this._market=s;this._language=e;this._currentUrl=window.location.href}async startChat(t,s){let e={market:this._market,language:this._language,chatIdString:t,itemId:s,currentUrl:this._currentUrl};const a=this._endpoint+this._startChatPath;const i=await this.fetchHelper.fetchJson(a,this.getRequestInit(e),false);return this.handleResponse(i,t)}async fetchMessages(t,s){if(!t){return s}let e={market:this._market,language:this._language,chatIdString:t};const a=this._endpoint+this._fetchChatMessagesPath;const i=await this.fetchHelper.fetchJson(a,this.getRequestInit(e),false);if(!i){return s}return this.handleResponse(i,s)}async sendMessage(t,s,e,a,i){let h;switch(i){case"Choice":h=await this.sendChoiceMessage(t,s,e,a);break;default:h=await this.sendTextMessage(t,s,e,a);break}if(!h){return a}if(h.includes(",")){h=h.substring(h.lastIndexOf(",")+1)}return h}async updateLastMessageId(t,s){let e={chatIdString:t,latestMessageIdString:s};const a=this._endpoint+this._updateLastMessagePath;await this.fetchHelper.fetchJson(a,this.getRequestInit(e),false)}async hasNewMessages(t){let s={language:this._language,market:this._market,itemId:t,chatIdString:"",currentUrl:this._currentUrl};const e=this._endpoint+this._hasNewMessagesPath;let a=await this.fetchHelper.fetchJson(e,this.getRequestInit(s),false);if(!a){return false}return JSON.parse(a)}async sendTextMessage(t,s,e,a){if(!e||!t){return a}let i={market:this._market,language:this._language,chatIdString:e,content:t,itemId:s,currentUrl:this._currentUrl};const h=this._endpoint+this._sendTextPath;const n=await this.fetchHelper.fetchJson(h,this.getRequestInit(i),false);return this.handleResponse(n,a)?.id??a}async sendChoiceMessage(t,s,e,a){if(!e||!t){return a}let i={market:this._market,language:this._language,chatIdString:e,content:t,itemId:s,currentUrl:this._currentUrl};const h=this._endpoint+this._sendChoicePath;const n=await this.fetchHelper.fetchJson(h,this.getRequestInit(i),false);return this.handleResponse(n,a)?.id??a}formatEndpoint(t){return t.lastIndexOf("/")===t.length-1?t.substring(0,t.length-1):t}handleResponse(t,s,e=false){if(!t){return s}let a=JSON.parse(t);if(!a||e&&!Array.isArray(a)){return s}return a}getRequestInit(t,s="POST",e="cors",a="application/json"){return{mode:e,method:s,headers:{"Content-Type":a,CustomValidateAntiForgeryToken:"01001101010001010100111101010111"},body:JSON.stringify(t)}}GetLastMessageId(t,s){return t?.at(-1)?.messageId??s}}export{s as P};
//# sourceMappingURL=p-6bec8d72.js.map