require(`./paths-Bcdg3ma0.cjs`);const e=require(`./model-auth-BEMdAUrd.cjs`),t=require(`./logger-M7L0LfHQ.cjs`),n=require(`./session-BhwX6_4n.cjs`);let r=require(`@whiskeysockets/baileys`);async function i(i,a,o=t.M,s){let c=a??n.i,l=e.Gn({cfg:e.Oa(),accountId:s}),u=await n.t(!0,i,{authDir:l.authDir});t.r(`Waiting for WhatsApp connection...`,o);try{await c(u),console.log(t.V(`✅ Linked! Credentials saved for future sends.`))}catch(a){let s=a?.error?.output?.statusCode??a?.output?.statusCode;if(s===515){console.log(t.L(`WhatsApp asked for a restart after pairing (code 515); creds are saved. Restarting connection once…`));try{u.ws?.close()}catch{}let e=await n.t(!1,i,{authDir:l.authDir});try{await c(e),console.log(t.V(`✅ Linked after restart; web session ready.`));return}finally{setTimeout(()=>e.ws?.close(),500)}}if(s===r.DisconnectReason.loggedOut)throw await e.Xn({authDir:l.authDir,isLegacyAuthDir:l.isLegacyAuthDir,runtime:o}),console.error(t.I(`WhatsApp reported the session is logged out. Cleared cached web session; please rerun ${e.ic(`openclaw channels login`)} and scan the QR again.`)),Error(`Session logged out; cache cleared. Re-run login.`,{cause:a});let d=n.n(a);throw console.error(t.I(`WhatsApp Web connection ended before fully opening. ${d}`)),Error(d,{cause:a})}finally{setTimeout(()=>{try{u.ws?.close()}catch{}},500)}}Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return i}});
