import{l as d}from"./v_7_4_3_17_17ffe662-3f12-45a2-ae5d-997b41d0d61b10.js";import{a as w}from"./v_7_4_3_17_17ffe662-3f12-45a2-ae5d-997b41d0d61b30.js";import{R as h,N as r,b,I,W as p}from"./v_7_4_3_17_17ffe662-3f12-45a2-ae5d-997b41d0d61b11.js";import{u as f,g as A}from"./v_7_4_3_17_17ffe662-3f12-45a2-ae5d-997b41d0d61b20.js";import{ah as i}from"./v_7_4_3_17_17ffe662-3f12-45a2-ae5d-997b41d0d61b23.js";import{W as m,V as v}from"./v_7_4_3_17_17ffe662-3f12-45a2-ae5d-997b41d0d61b19.js";import"./v_7_4_3_17_17ffe662-3f12-45a2-ae5d-997b41d0d61b.js";import{c as k}from"./v_7_4_3_17_17ffe662-3f12-45a2-ae5d-997b41d0d61b8.js";const g=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 p("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 p("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 p("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}=g.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}=g.getState(),{token:c,active:t,userID:a}=await h("whatsApi")||{},s=await i("API",!0)?!1:t;g.setState({token:c,active:s,userID:a}),s&&o()},R=async o=>{const{active:c,socket:t,connect:a}=g.getState();c&&(t?.close(),t?.disconnect()),g.setState({token:o.token,active:o.active}),o.active&&a()};export{g as a,x as i,R as r};
