import{l as d}from"./v_7_4_2_1_59f2e4f7-dc00-41f3-9942-b1c61b935c999.js";import{a as b}from"./v_7_4_2_1_59f2e4f7-dc00-41f3-9942-b1c61b935c9932.js";import{R as P,N as r,b as w,I,a as p}from"./v_7_4_2_1_59f2e4f7-dc00-41f3-9942-b1c61b935c9910.js";import{u as f}from"./v_7_4_2_1_59f2e4f7-dc00-41f3-9942-b1c61b935c9919.js";import{a3 as i,q as A}from"./v_7_4_2_1_59f2e4f7-dc00-41f3-9942-b1c61b935c9923.js";import{W as m,V as v}from"./v_7_4_2_1_59f2e4f7-dc00-41f3-9942-b1c61b935c9918.js";import{c as k}from"./v_7_4_2_1_59f2e4f7-dc00-41f3-9942-b1c61b935c997.js";const l=k((o,c)=>({active:!1,socket:null,token:"",userID:"",connect:async()=>{const{token:e,userID:s,ouvintes:n}=c(),a=d(v.webSocket["api-whatsapp"],{path:"/ws/socket.io",transports:["websocket"]});a.open(),n(a),o({active:!0,socket:a}),await p("whatsApi",{active:!0,token:e,userID:s}),a.on("connect",()=>{o({active:!0}),a.emit("update-user-socket",s,e)}),a.on("disconnect",()=>{o({active:!1})})},disconnect:async()=>{const{socket:e,token:s,userID:n}=c();e==null||e.close(),e==null||e.disconnect(),o({active:!1,socket:null}),await p("whatsApi",{active:!1,token:s,userID:n})},toggleSwitch:async()=>{const e=f.getState().language,{label:s}=A.getState(),{active:n,connect:a,disconnect:t,token:u}=c();if(!u){I({type:"Info",message:e.youNeedGenerateToken,position:"top-right"});return}n?t():await i("API",!0)?r("API",e.btnPremium,s.checkout):a()},getToken:async()=>{const e=f.getState().language,{active:s,token:n,connect:a}=c();if(n&&!await new Promise(h=>{w({content:`${e.prestesASobrescreverTokenAtual} ${e.desejaContinuar}`,confirmText:e.confirm,submit:()=>h(!0)})}))return;s||await a();const{socket:t}=c();t.removeAllListeners("capture-token");const u=await m.Conn("getMyUserId");t.emit("set-user",u,n),t.on("capture-token",async g=>{I({type:"Success",message:e.tokenGenerateSucess,position:"top-right"}),await p("whatsApi",{active:!0,token:g,userID:u}),o({token:g,active:!0,userID:u})})},ouvintes:e=>{const s=f.getState().language,{label:n}=A.getState(),{disconnect:a}=l.getState();e.on("sendText",async t=>{await i("API",!0)?(r("API",s.btnPremium,n.checkout),a()):m.Chat("sendTxt",{id:t.phone,msg:t.message})}),e.on("sendImage",async t=>{await i("API",!0)?(r("API",s.btnPremium,n.checkout),a()):m.Chat("sendImage",{id:t.phone,base64:t.base64,msg:t.message})}),e.on("sendVideo",async t=>{await i("API",!0)?(r("API",s.btnPremium,n.checkout),a()):m.Chat("sendVideo",{id:t.phone,base64:t.base64,msg:t.message})}),e.on("sendDoc",async t=>{await i("API",!0)?(r("API",s.btnPremium,n.checkout),a()):m.Chat("sendDoc",{id:t.phone,base64:t.base64,name:t.name})}),e.on("sendAudio",async t=>{await i("API",!0)?(r("API",s.btnPremium,n.checkout),a()):m.Chat("sendAudio",{id:t.phone,base64:t.base64})}),e.on("get-etiquetas",async()=>{if(await i("API",!0))r("API",s.btnPremium,n.checkout),a();else{const t=await m.Labels("getAllLabels");e.emit("send-etiquetas",t)}}),e.on("addOrRemoveLabels",async t=>{if(await i("API",!0))r("API",s.btnPremium,n.checkout),a();else{const u=await m.Labels("addOrRemoveLabels",t);u.error&&e.emit("addOrRemoveLabelsError",{error:u.error})}}),e.on("createNote",async t=>{await i("API",!0)?(r("API",s.btnPremium,n.checkout),a()):b.getState().createNoteSocket(t.userID,t.text,t.base64)})}})),W=async()=>{const{connect:o}=l.getState(),{token:c,active:e,userID:s}=await P("whatsApi")||{},a=await i("API",!0)?!1:e;l.setState({token:c,active:a,userID:s}),a&&o()},R=async o=>{const{active:c,socket:e,connect:s}=l.getState();c&&(e==null||e.close(),e==null||e.disconnect()),l.setState({token:o.token,active:o.active}),o.active&&s()};export{l as a,W as i,R as r};
