import{a as e,h as t,l as n,u as r}from"./subsystem-hyY_8Y33.js";import{Ws as i,Za as a,gr as o,xr as s}from"./model-selection-BDkm-Fer.js";import{r as c}from"./logger-Cb-sKDne.js";import{i as l,n as u,t as d}from"./session-CR2Ie5yU.js";import{DisconnectReason as f}from"@whiskeysockets/baileys";async function p(p,m,h=e,g){let _=m??l,v=o({cfg:a(),accountId:g}),y=await d(!0,p,{authDir:v.authDir});c(`Waiting for WhatsApp connection...`,h);try{await _(y),console.log(t(`✅ Linked! Credentials saved for future sends.`))}catch(e){let a=e?.error?.output?.statusCode??e?.output?.statusCode;if(a===515){console.log(r(`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(t(`✅ Linked after restart; web session ready.`));return}finally{setTimeout(()=>e.ws?.close(),500)}}if(a===f.loggedOut)throw await s({authDir:v.authDir,isLegacyAuthDir:v.isLegacyAuthDir,runtime:h}),console.error(n(`WhatsApp reported the session is logged out. Cleared cached web session; please rerun ${i(`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(n(`WhatsApp Web connection ended before fully opening. ${o}`)),Error(o,{cause:e})}finally{setTimeout(()=>{try{y.ws?.close()}catch{}},500)}}export{p as t};
