import{E as e,P as t,j as n,v as r}from"./subsystem-BQ1073Mu.js";import{c as i,y as a}from"./utils-D1PlBTFH.js";import{Ml as o}from"./model-selection-p21GBPS6.js";import{t as s}from"./command-format-BbDT1tlm.js";import{Q as c,X as l,et as u,nt as d,tt as f}from"./plugins-1aNlpDX7.js";import p from"node:fs";import"node:crypto";import{DisconnectReason as m,fetchLatestBaileysVersion as h,makeCacheableSignalKeyStore as g,makeWASocket as _,useMultiFileAuthState as v}from"@whiskeysockets/baileys";import y from"qrcode-terminal";let b=Promise.resolve();function x(e,t,n){b=b.then(()=>S(e,t,n)).catch(e=>{n.warn({error:String(e)},`WhatsApp creds save queue error`)})}async function S(e,t,n){try{let t=d(e),n=f(e),r=c(t);if(r)try{JSON.parse(r),p.copyFileSync(t,n);try{p.chmodSync(n,384)}catch{}}catch{}}catch{}try{await Promise.resolve(t());try{p.chmodSync(d(e),384)}catch{}}catch(e){n.warn({error:String(e)},`failed saving WhatsApp creds`)}}async function C(c,d,f={}){let p=t(n({module:`baileys`},{level:d?`info`:`silent`}),d?`info`:`silent`),b=a(f.authDir??u());await i(b);let S=n({module:`web-session`});l(b);let{state:C,saveCreds:w}=await v(b),{version:E}=await h(),D=_({auth:{creds:C.creds,keys:g(C.keys,p)},version:E,logger:p,printQRInTerminal:!1,browser:[`openclaw`,`cli`,o],syncFullHistory:!1,markOnlineOnConnect:!1});return D.ev.on(`creds.update`,()=>x(b,w,S)),D.ev.on(`connection.update`,t=>{try{let{connection:n,lastDisconnect:i,qr:a}=t;a&&(f.onQr?.(a),c&&(console.log(`Scan this QR in WhatsApp (Linked Devices):`),y.generate(a,{small:!0}))),n===`close`&&T(i?.error)===m.loggedOut&&console.error(r(`WhatsApp session logged out. Run: ${s(`openclaw channels login`)}`)),n===`open`&&d&&console.log(e(`WhatsApp Web connected.`))}catch(e){S.error({error:String(e)},`connection.update handler error`)}}),D.ws&&typeof D.ws.on==`function`&&D.ws.on(`error`,e=>{S.error({error:String(e)},`WebSocket error`)}),D}async function w(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 T(e){return e?.output?.statusCode??e?.status}function E(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 D(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 O(e){if(e instanceof Error)return e.message;if(typeof e==`string`)return e;if(!e||typeof e!=`object`)return String(e);let t=D(e)??D(e?.error)??D(e?.lastDisconnect?.error),n=t?.statusCode??T(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(` `):E(e)}export{w as i,O as n,T as r,C as t};
