import{a as t,p as i}from"./p-80da0c3d.js";var e=undefined&&undefined.__awaiter||function(t,i,e,n){function s(t){return t instanceof e?t:new e((function(i){i(t)}))}return new(e||(e=Promise))((function(e,o){function r(t){try{d(n.next(t))}catch(t){o(t)}}function c(t){try{d(n["throw"](t))}catch(t){o(t)}}function d(t){t.done?e(t.value):s(t.value).then(r,c)}d((n=n.apply(t,i||[])).next())}))};const n="klevu-moi-session";function s(i={}){var n,s,c;return e(this,void 0,void 0,(function*(){const e=((n=i.settings)===null||n===void 0?void 0:n.configOverride)||t.getDefault();let f=[];let h={klevuApiKey:e.apiKey,sessionId:"",mode:i.mode,url:i.url,productId:i.productId,pqaWidgetId:i.pqaWidgetId};const a=yield r();let l;let v;let m=[];let p=false;const g=i.productId||i.url;if(a&&a.context){switch(i.mode){case undefined:h.sessionId=a.context.sessionId;if(a.MOI){f=a.MOI.messages;l=a.MOI.menu;v=a.MOI.genericOptions;m=a.MOI.feedbacks;if((s=i.settings)===null||s===void 0?void 0:s.alwaysStartConversation){p=a.MOI.messages.length===0}}break;case"PQA":h.sessionId=a.context.sessionId;if(g&&a.PQA&&a.PQA[g]){f=a.PQA[g].messages;l=a.PQA[g].menu;v=a.PQA[g].genericOptions;m=a.PQA[g].feedbacks;if((c=i.settings)===null||c===void 0?void 0:c.alwaysStartConversation){p=a.PQA[g].messages.length===0}}}}else{p=true}if(p){const t=yield d({context:h},e);if(!(t===null||t===void 0?void 0:t.data[0].context)){throw new Error("No context found")}const i=u(t);h=i.context;f=i.messages;l=i.menu;v=i.genericOptions}return new o({feedbacks:m,messages:f,menu:l,genericOptions:v},i,h,e)}))}class o{constructor(t,i,e,n){var s;this.messages=t.messages;this.menu=t.menu;this.genericOptions=t.genericOptions;this.feedbacks=(s=t.feedbacks)!==null&&s!==void 0?s:[];this.options=i;this.context=e;this.config=n;if(this.context.url&&this.context.productId){throw new Error("Cannot set both url and productId for PQA")}this.save()}query(t,i){var n,s,o,r,c,f,h;return e(this,void 0,void 0,(function*(){if(t.message){this.messages=[...(n=this.messages)!==null&&n!==void 0?n:[],{local:{message:t.message}}];(o=(s=this.options).onMessage)===null||o===void 0?void 0:o.call(s)}const e=yield d(Object.assign({context:this.context},t),this.config,i);if(!e){throw new Error("No response from MOI")}for(const t of e.data){if("actions"in t){for(const i of t.actions.actions){const t=(c=(r=this.options).onAction)===null||c===void 0?void 0:c.call(r,i);if(t===false){continue}if(i.type==="purgeHistory"){this.clear()}else if(i.type==="redirectToUrl"&&i.context.link){if(this.options.onRedirect){this.options.onRedirect(i.context.link)}else{window.location.href=i.context.link}}}}}const{messages:a,genericOptions:l,menu:v,context:m}=u(e);this.messages=[...this.messages,...a];(h=(f=this.options).onMessage)===null||h===void 0?void 0:h.call(f);this.genericOptions=l;this.menu=v;this.context=m;this.save();return e}))}clear(){var t,i;this.messages=[];this.feedbacks=[];this.save();(i=(t=this.options).onMessage)===null||i===void 0?void 0:i.call(t)}save(){const t=this.context.productId||this.context.url;switch(this.context.mode){case undefined:c({context:this.context,MOI:{menu:this.menu,genericOptions:this.genericOptions,messages:this.messages,feedbacks:this.feedbacks}});break;case"PQA":if(!t){throw new Error("Cannot save PQA session without url or productId")}c({context:this.context,PQA:{[t]:{menu:this.menu,genericOptions:this.genericOptions,messages:this.messages,feedbacks:this.feedbacks}}})}}addFeedback(t,i,n){return e(this,void 0,void 0,(function*(){const e=this.feedbacks.find((i=>i.id===t));if(e){e.thumbs=i;e.reason=n}else{this.feedbacks.push({id:t,thumbs:i,reason:n})}return this.query({feedback:{messageId:t,thumbs:i.toUpperCase(),reason:n}},"feedback")}))}}function r(){const t=localStorage.getItem(n);if(!t){return undefined}return JSON.parse(t)}function c(t){const i=localStorage.getItem(n);let e={};if(i){e=JSON.parse(i)}const s=t.context.productId||t.context.url;switch(t.context.mode){case undefined:e.context=t.context;e.MOI=t.MOI;break;case"PQA":e.context=t.context;if(!e.PQA){e.PQA={}}if(!s||!t.PQA){throw new Error("No url, productId or PQA session")}e.PQA[s]=t.PQA[s];break}localStorage.setItem(n,JSON.stringify(e))}function d(t,n,s="send"){return e(this,void 0,void 0,(function*(){return yield i(`${n.moiApiUrl}chat/${s}`,t)}))}function u(t){const i=[];let e=undefined;let n=undefined;const s=t.data[0].context;for(const s of t.data){"message"in s&&i.push(s);"filter"in s&&i.push(s);"productData"in s&&i.push(s);if("genericOptions"in s){e=s.genericOptions}if("menuOptions"in s){n=s.menuOptions}}return{context:s,messages:i,menu:n,genericOptions:e}}export{s};
//# sourceMappingURL=p-5c6c0a89.js.map