import{I as e,L as t,M as n,V as r,r as i}from"./logger-D0y9iaZX.js";import{Za as a,gr as o,qs as s,xr as c}from"./model-selection-R1b_J3k7.js";import{i as l,n as u,t as d}from"./session-DdbOamLI.js";import{DisconnectReason as f}from"@whiskeysockets/baileys";async function p(p,m,h=n,g){let _=m??l,v=o({cfg:a(),accountId:g}),y=await d(!0,p,{authDir:v.authDir});i(`Waiting for WhatsApp connection...`,h);try{await _(y),console.log(r(`✅ Linked! Credentials saved for future sends.`))}catch(n){let i=n?.error?.output?.statusCode??n?.output?.statusCode;if(i===515){console.log(t(`WhatsApp asked for a restart after pairing (code 515); creds are saved. Restarting connection once…`));try{y.ws?.close()}catch{}let e=await d(!1,p,{authDir:v.authDir});try{await _(e),console.log(r(`✅ Linked after restart; web session ready.`));return}finally{setTimeout(()=>e.ws?.close(),500)}}if(i===f.loggedOut)throw await c({authDir:v.authDir,isLegacyAuthDir:v.isLegacyAuthDir,runtime:h}),console.error(e(`WhatsApp reported the session is logged out. Cleared cached web session; please rerun ${s(`openclaw channels login`)} and scan the QR again.`)),Error(`Session logged out; cache cleared. Re-run login.`,{cause:n});let a=u(n);throw console.error(e(`WhatsApp Web connection ended before fully opening. ${a}`)),Error(a,{cause:n})}finally{setTimeout(()=>{try{y.ws?.close()}catch{}},500)}}export{p as t};
