import{E as e,I as t,L as n,M as r,V as i}from"./utils-BW1LBoG1.js";import{Bs as a,Fs as o,Wi as s,tc as c}from"./model-auth-BFP3sjhH.js";import{i as l,n as u,t as d}from"./session-Bl5MuO-x.js";import{DisconnectReason as f}from"@whiskeysockets/baileys";async function p(p,m,h=r,g){let _=m??l,v=o({cfg:s(),accountId:g}),y=await d(!0,p,{authDir:v.authDir});e(`Waiting for WhatsApp connection...`,h);try{await _(y),console.log(i(`✅ Linked! Credentials saved for future sends.`))}catch(e){let r=e?.error?.output?.statusCode??e?.output?.statusCode;if(r===515){console.log(n(`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(i(`✅ Linked after restart; web session ready.`));return}finally{setTimeout(()=>e.ws?.close(),500)}}if(r===f.loggedOut)throw await a({authDir:v.authDir,isLegacyAuthDir:v.isLegacyAuthDir,runtime:h}),console.error(t(`WhatsApp reported the session is logged out. Cleared cached web session; please rerun ${c(`openclaw channels login`)} and scan the QR again.`)),Error(`Session logged out; cache cleared. Re-run login.`,{cause:e});let o=u(e);throw console.error(t(`WhatsApp Web connection ended before fully opening. ${o}`)),Error(o,{cause:e})}finally{setTimeout(()=>{try{y.ws?.close()}catch{}},500)}}export{p as t};
