const e=require(`./paths-Bcdg3ma0.cjs`),t=require(`./utils-DyYoKTxM.cjs`),n=require(`./model-auth-B0JUHRHo.cjs`);let r=require(`node:fs`);r=e.p(r),require(`node:crypto`);let i=require(`@whiskeysockets/baileys`),a=require(`qrcode-terminal`);a=e.p(a);let o=Promise.resolve();function s(e,t,n){o=o.then(()=>c(e,t,n)).catch(e=>{n.warn({error:String(e)},`WhatsApp creds save queue error`)})}async function c(e,t,i){try{let t=n.Zs(e),i=n.Xs(e),a=n.qs(t);if(a)try{JSON.parse(a),r.default.copyFileSync(t,i);try{r.default.chmodSync(i,384)}catch{}}catch{}}catch{}try{await Promise.resolve(t());try{r.default.chmodSync(n.Zs(e),384)}catch{}}catch(e){i.warn({error:String(e)},`failed saving WhatsApp creds`)}}async function l(e,r,o={}){let c=t.q(t.K({module:`baileys`},{level:r?`info`:`silent`}),r?`info`:`silent`),l=t.h(o.authDir??n.Ys());await t.a(l);let u=t.K({module:`web-session`});n.Gs(l);let{state:f,saveCreds:p}=await(0,i.useMultiFileAuthState)(l),{version:m}=await(0,i.fetchLatestBaileysVersion)(),h=(0,i.makeWASocket)({auth:{creds:f.creds,keys:(0,i.makeCacheableSignalKeyStore)(f.keys,c)},version:m,logger:c,printQRInTerminal:!1,browser:[`openclaw`,`cli`,n.To],syncFullHistory:!1,markOnlineOnConnect:!1});return h.ev.on(`creds.update`,()=>s(l,p,u)),h.ev.on(`connection.update`,s=>{try{let{connection:c,lastDisconnect:l,qr:u}=s;u&&(o.onQr?.(u),e&&(console.log(`Scan this QR in WhatsApp (Linked Devices):`),a.default.generate(u,{small:!0}))),c===`close`&&d(l?.error)===i.DisconnectReason.loggedOut&&console.error(t.I(`WhatsApp session logged out. Run: ${n.ac(`openclaw channels login`)}`)),c===`open`&&r&&console.log(t.V(`WhatsApp Web connected.`))}catch(e){u.error({error:String(e)},`connection.update handler error`)}}),h.ws&&typeof h.ws.on==`function`&&h.ws.on(`error`,e=>{u.error({error:String(e)},`WebSocket error`)}),h}async function u(e){return new Promise((t,n)=>{let r=e.ev,i=(...e)=>{let a=e[0]??{};a.connection===`open`&&(r.off?.(`connection.update`,i),t()),a.connection===`close`&&(r.off?.(`connection.update`,i),n(a.lastDisconnect??Error(`Connection closed`)))};e.ev.on(`connection.update`,i)})}function d(e){return e?.output?.statusCode??e?.status}function f(e,t=800){try{let n=new WeakSet,r=JSON.stringify(e,(e,t)=>{if(typeof t==`bigint`)return t.toString();if(typeof t==`function`){let e=t.name;return`[Function ${typeof e==`string`&&e.length>0?e:`anonymous`}]`}if(typeof t==`object`&&t){if(n.has(t))return`[Circular]`;n.add(t)}return t},2);return r?r.length>t?`${r.slice(0,t)}…`:r:String(e)}catch{return String(e)}}function p(e){if(!e||typeof e!=`object`)return null;let t=e?.output;if(!t||typeof t!=`object`)return null;let n=t.payload,r=typeof t.statusCode==`number`?t.statusCode:typeof n?.statusCode==`number`?n.statusCode:void 0,i=typeof n?.error==`string`?n.error:void 0,a=typeof n?.message==`string`?n.message:void 0;return!r&&!i&&!a?null:{statusCode:r,error:i,message:a}}function m(e){if(e instanceof Error)return e.message;if(typeof e==`string`)return e;if(!e||typeof e!=`object`)return String(e);let t=p(e)??p(e?.error)??p(e?.lastDisconnect?.error),n=t?.statusCode??d(e),r=e?.code,i=typeof r==`string`||typeof r==`number`?String(r):void 0,a=[t?.message,typeof e?.message==`string`?e.message:void 0,typeof e?.error?.message==`string`?e.error?.message:void 0].filter(e=>!!(e&&e.trim().length>0))[0],o=[];return typeof n==`number`&&o.push(`status=${n}`),t?.error&&o.push(t.error),a&&o.push(a),i&&o.push(`code=${i}`),o.length>0?o.join(` `):f(e)}Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return u}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return m}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return d}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return l}});
