import{l as P}from"./v_7_4_2_16_364f475f-21c1-4d89-bf17-70efc8c405b79.js";import{a as d}from"./v_7_4_2_16_364f475f-21c1-4d89-bf17-70efc8c405b732.js";import{R as w,N as u,h as b,I,g as p}from"./v_7_4_2_16_364f475f-21c1-4d89-bf17-70efc8c405b710.js";import{u as f}from"./v_7_4_2_16_364f475f-21c1-4d89-bf17-70efc8c405b719.js";import{av as c,s as h}from"./v_7_4_2_16_364f475f-21c1-4d89-bf17-70efc8c405b723.js";import{W as m,V as v}from"./v_7_4_2_16_364f475f-21c1-4d89-bf17-70efc8c405b718.js";import{c as k}from"./v_7_4_2_16_364f475f-21c1-4d89-bf17-70efc8c405b77.js";const g=k((o,r)=>({active:!1,socket:null,token:"",userID:"",connect:async()=>{const{token:e,userID:s,ouvintes:n}=r(),a=P(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}=r();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}=h.getState(),{active:n,connect:a,disconnect:t,token:i}=r();if(!i){I({type:"Info",message:e.youNeedGenerateToken,position:"top-right"});return}n?t():await c("API",!0)?u("API",e.btnPremium,s.checkout):a()},getToken:async()=>{const e=f.getState().language,{active:s,token:n,connect:a}=r();if(n&&!await new Promise(A=>{b({content:`${e.prestesASobrescreverTokenAtual} ${e.desejaContinuar}`,confirmText:e.confirm,submit:()=>A(!0)})}))return;s||await a();const{socket:t}=r();t.removeAllListeners("capture-token");const i=await m.Conn("getMyUserId");t.emit("set-user",i,n),t.on("capture-token",async l=>{I({type:"Success",message:e.tokenGenerateSucess,position:"top-right"}),await p("whatsApi",{active:!0,token:l,userID:i}),o({token:l,active:!0,userID:i})})},ouvintes:e=>{const s=f.getState().language,{label:n}=h.getState(),{disconnect:a}=g.getState();e.on("sendText",async t=>{await c("API",!0)?(u("API",s.btnPremium,n.checkout),a()):m.Chat("sendTxt",{id:t.phone,msg:t.message})}),e.on("sendImage",async t=>{await c("API",!0)?(u("API",s.btnPremium,n.checkout),a()):m.Chat("sendImage",{id:t.phone,base64:t.base64,msg:t.message})}),e.on("sendVideo",async t=>{await c("API",!0)?(u("API",s.btnPremium,n.checkout),a()):m.Chat("sendVideo",{id:t.phone,base64:t.base64,msg:t.message})}),e.on("sendDoc",async t=>{await c("API",!0)?(u("API",s.btnPremium,n.checkout),a()):m.Chat("sendDoc",{id:t.phone,base64:t.base64,name:t.name})}),e.on("sendAudio",async t=>{await c("API",!0)?(u("API",s.btnPremium,n.checkout),a()):m.Chat("sendAudio",{id:t.phone,base64:t.base64})}),e.on("get-etiquetas",async t=>{if(await c("API",!0))u("API",s.btnPremium,n.checkout),a(),t([]);else{const i=await m.Labels("getAllLabels");t(i)}}),e.on("addOrRemoveLabels",async(t,i)=>{if(await c("API",!0))u("API",s.btnPremium,n.checkout),a(),i([]);else{const l=await m.Labels("addOrRemoveLabels",t);i(l)}}),e.on("createNote",async t=>{await c("API",!0)?(u("API",s.btnPremium,n.checkout),a()):d.getState().createNoteSocket(t.userID,t.text,t.base64)})}})),W=async()=>{const{connect:o}=g.getState(),{token:r,active:e,userID:s}=await w("whatsApi")||{},a=await c("API",!0)?!1:e;g.setState({token:r,active:a,userID:s}),a&&o()},x=async o=>{const{active:r,socket:e,connect:s}=g.getState();r&&(e==null||e.close(),e==null||e.disconnect()),g.setState({token:o.token,active:o.active}),o.active&&s()};export{g as a,W as i,x as r};
