import{l as h}from"./v_7_4_0_1_4b8cc392-a410-45f6-b662-85abdad9c03e9.js";import{a as b}from"./v_7_4_0_1_4b8cc392-a410-45f6-b662-85abdad9c03e31.js";import{R as P,N as r,b as w,I,a as f}from"./v_7_4_0_1_4b8cc392-a410-45f6-b662-85abdad9c03e10.js";import{u as p,a as A}from"./v_7_4_0_1_4b8cc392-a410-45f6-b662-85abdad9c03e19.js";import{_ as i}from"./v_7_4_0_1_4b8cc392-a410-45f6-b662-85abdad9c03e22.js";import{W as m,V as v}from"./v_7_4_0_1_4b8cc392-a410-45f6-b662-85abdad9c03e18.js";import{c as y}from"./v_7_4_0_1_4b8cc392-a410-45f6-b662-85abdad9c03e7.js";const l=y((o,c)=>({active:!1,socket:null,token:"",userID:"",connect:async()=>{const{token:e,userID:s,ouvintes:n}=c(),a=h(v.webSocket["api-whatsapp"],{transports:["websocket"]});a.open(),n(a),o({active:!0,socket:a}),await f("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 f("whatsApi",{active:!1,token:s,userID:n})},toggleSwitch:async()=>{const e=p.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=p.getState().language,{active:s,token:n,connect:a}=c();if(n&&!await new Promise(d=>{w({content:`${e.prestesASobrescreverTokenAtual} ${e.desejaContinuar}`,confirmText:e.confirm,submit:()=>d(!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 f("whatsApi",{active:!0,token:g,userID:u}),o({token:g,active:!0,userID:u})})},ouvintes:e=>{const s=p.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};
