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