import{l as d}from"./v_7_4_3_14_49a76bb0-8cf2-4908-a016-18478b76458110.js";import{a as w}from"./v_7_4_3_14_49a76bb0-8cf2-4908-a016-18478b76458130.js";import{R as h,N as r,b,I,W as g}from"./v_7_4_3_14_49a76bb0-8cf2-4908-a016-18478b76458111.js";import{u as f,h as A}from"./v_7_4_3_14_49a76bb0-8cf2-4908-a016-18478b76458120.js";import{ar as i}from"./v_7_4_3_14_49a76bb0-8cf2-4908-a016-18478b76458123.js";import{W as m,V as v}from"./v_7_4_3_14_49a76bb0-8cf2-4908-a016-18478b76458119.js";import"./v_7_4_3_14_49a76bb0-8cf2-4908-a016-18478b764581.js";import{c as k}from"./v_7_4_3_14_49a76bb0-8cf2-4908-a016-18478b7645818.js";const p=k((o,c)=>({active:!1,socket:null,token:"",userID:"",connect:async()=>{const{token:t,userID:a,ouvintes:n}=c(),s=d(v.webSocket["api-whatsapp"],{path:"/ws/socket.io",transports:["websocket"]});s.open(),n(s),o({active:!0,socket:s}),await g("whatsApi",{active:!0,token:t,userID:a}),s.on("connect",()=>{o({active:!0}),s.emit("update-user-socket",a,t)}),s.on("disconnect",()=>{o({active:!1})})},disconnect:async()=>{const{socket:t,token:a,userID:n}=c();t?.close(),t?.disconnect(),o({active:!1,socket:null}),await g("whatsApi",{active:!1,token:a,userID:n})},toggleSwitch:async()=>{const t=f.getState().language,{active:a,connect:n,disconnect:s,token:e}=c();if(!e){I({type:"Info",message:t.youNeedGenerateToken,position:"top-right"});return}a?s():await i("API",!0)?r("API",t.btnPremium,A()):n()},getToken:async()=>{const t=f.getState().language,{active:a,token:n,connect:s}=c();if(n&&!await new Promise(P=>{b({content:`${t.prestesASobrescreverTokenAtual} ${t.desejaContinuar}`,confirmText:t.confirm,submit:()=>P(!0)})}))return;a||await s();const{socket:e}=c();e.removeAllListeners("capture-token");const u=await m.Conn("getMyUserId");e.emit("set-user",u,n),e.on("capture-token",async l=>{I({type:"Success",message:t.tokenGenerateSucess,position:"top-right"}),await g("whatsApi",{active:!0,token:l,userID:u}),o({token:l,active:!0,userID:u})})},ouvintes:t=>{const a=f.getState().language,n=A(),{disconnect:s}=p.getState();t.on("sendText",async e=>{await i("API",!0)?(r("API",a.btnPremium,n),s()):m.Chat("sendTxt",{id:e.phone,msg:e.message})}),t.on("sendImage",async e=>{await i("API",!0)?(r("API",a.btnPremium,n),s()):m.Chat("sendImage",{id:e.phone,base64:e.base64,msg:e.message})}),t.on("sendVideo",async e=>{await i("API",!0)?(r("API",a.btnPremium,n),s()):m.Chat("sendVideo",{id:e.phone,base64:e.base64,msg:e.message})}),t.on("sendDoc",async e=>{await i("API",!0)?(r("API",a.btnPremium,n),s()):m.Chat("sendDoc",{id:e.phone,base64:e.base64,name:e.name})}),t.on("sendAudio",async e=>{await i("API",!0)?(r("API",a.btnPremium,n),s()):m.Chat("sendAudio",{id:e.phone,base64:e.base64})}),t.on("get-etiquetas",async e=>{if(await i("API",!0))r("API",a.btnPremium,n),s(),e([]);else{const u=await m.Labels("getAllLabels");e(u)}}),t.on("addOrRemoveLabels",async(e,u)=>{if(await i("API",!0))r("API",a.btnPremium,n),s(),u([]);else{const l=await m.Labels("addOrRemoveLabels",e);u(l)}}),t.on("createNote",async e=>{await i("API",!0)?(r("API",a.btnPremium,n),s()):w.getState().createNoteSocket(e.userID,e.text,e.base64)})}})),x=async()=>{const{connect:o}=p.getState(),{token:c,active:t,userID:a}=await h("whatsApi")||{},s=await i("API",!0)?!1:t;p.setState({token:c,active:s,userID:a}),s&&o()},R=async o=>{const{active:c,socket:t,connect:a}=p.getState();c&&(t?.close(),t?.disconnect()),p.setState({token:o.token,active:o.active}),o.active&&a()};export{p as a,x as i,R as r};
