import{a as e,n as t,t as n}from"./jsx-runtime-CutOxEx1.js";import{l as r}from"./chunk-6CSD65Y2-BjfinoX-.js";import{s as i}from"./authSession-Dulxhbeq.js";import{r as a}from"./norm-f7iXVy-b.js";var o=e(t(),1),s=async()=>({mode:`login`,session:null,pendingLocalToken:null,error:`feature_disabled`,isNewUser:!1,signupGiftPoints:0,token:``,expiresAt:``,user:null,extRelay:``,mergedFrom:``}),c=n(),l=e=>{try{let t=a(new URLSearchParams(window.location.search).get(e));if(t)return t}catch{}try{let t=window.location.hash||``,n=t.indexOf(`?`);if(n>=0)return a(new URLSearchParams(t.slice(n+1)).get(e))}catch{}return``};function u(){let e=r(),[t,n]=(0,o.useState)(`processing`),[u,d]=(0,o.useState)(``),[f,p]=(0,o.useState)(``);return(0,o.useEffect)(()=>{let t=!0;return(async()=>{try{if(a(l(`provider`)).toLowerCase()===`wechat`)throw Error(`该登录方式已下线，请返回登录页使用手机号验证码登录。`);let r=await s();if(!t)return;if(r.mode===`login`){let a=r.isNewUser?Math.max(0,Number(r.signupGiftPoints||0)||0):0;if(!r.token||!r.expiresAt||!r.user?.id)throw Error(`登录失败：会话数据不完整`);if(r.extRelay){n(`success`),d(a>0?`注册成功，已赠送 ${a} 积分体验额度，正在同步到插件…`:`登录成功，正在同步到插件…`);try{let e=String(sessionStorage.getItem(`jdt_ext_bridge_nonce_v1`)||``).trim();window.postMessage({type:`JDT_AUTH_SESSION`,extId:r.extRelay,nonce:e,session:{token:r.token,expiresAt:r.expiresAt,user:{id:r.user?.id,email:r.user?.email,phone:r.user?.phone}}},window.location.origin)}catch{}p(`chrome-extension://${r.extRelay}/index.html#/me`);return}await i({token:r.token,expiresAt:r.expiresAt,user:{id:r.user.id,email:r.user.email,phone:r.user.phone}}),n(`success`),d(a>0?`注册成功，已赠送 ${a} 积分体验额度，正在跳转…`:`登录成功，正在跳转…`),setTimeout(()=>{t&&e(`/tools`)},a>0?1200:800)}else{if(r.extRelay){n(`success`),d(r.mergedFrom?`绑定成功，已合并账号 ${r.mergedFrom}，正在返回插件…`:`绑定成功，正在返回插件…`),p(`chrome-extension://${r.extRelay}/index.html#/me`);try{window.postMessage({type:`JDT_AUTH_ACK`,ok:!0},window.location.origin)}catch{}return}n(`success`),d(r.mergedFrom?`绑定成功，已合并账号 ${r.mergedFrom}`:`绑定成功`),setTimeout(()=>{t&&e(`/tools`)},1200)}}catch(e){if(!t)return;n(`error`),d(String(e?.message||`回调处理失败`))}})(),()=>{t=!1}},[e]),(0,o.useEffect)(()=>{let e=e=>{try{if(e.source!==window||String(e.origin||``)!==window.location.origin||String(e.data?.type||``).trim()!==`JDT_AUTH_ACK`||!e.data?.ok)return;d(`已同步到插件，你可以关闭此页面。`),window.setTimeout(()=>{try{window.close()}catch{}},800)}catch{}};return window.addEventListener(`message`,e),()=>window.removeEventListener(`message`,e)},[]),(0,c.jsx)(`div`,{className:`w-full min-h-screen bg-gray-50 flex items-center justify-center`,children:(0,c.jsx)(`div`,{className:`w-full max-w-sm text-center px-6`,children:t===`processing`?(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)(`div`,{className:`mx-auto w-8 h-8 border-2 border-gray-300 border-t-gray-900 rounded-full animate-spin`}),(0,c.jsx)(`div`,{className:`mt-4 text-sm text-gray-600`,children:`正在处理登录…`})]}):t===`success`?(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)(`div`,{className:`mx-auto w-10 h-10 rounded-full bg-gray-100 border border-gray-300 flex items-center justify-center text-gray-900 text-lg font-bold`,children:`✓`}),(0,c.jsx)(`div`,{className:`mt-3 text-sm font-medium text-gray-900`,children:u}),f?(0,c.jsx)(`a`,{href:f,className:`mt-4 inline-flex h-9 px-4 rounded-lg bg-gray-900 text-white text-xs font-medium hover:bg-black transition-colors items-center justify-center`,children:`如果未同步，点此返回插件`}):null]}):(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)(`div`,{className:`mx-auto w-10 h-10 rounded-full bg-rose-50 border border-rose-200 flex items-center justify-center text-rose-600 text-lg font-bold`,children:`!`}),(0,c.jsx)(`div`,{className:`mt-3 text-sm font-medium text-gray-900`,children:`处理失败`}),(0,c.jsx)(`div`,{className:`mt-2 text-xs text-gray-500 leading-relaxed`,children:u}),(0,c.jsx)(`button`,{type:`button`,onClick:()=>e(`/auth`),className:`mt-5 h-9 px-5 rounded-lg bg-gray-900 text-white text-xs font-medium hover:bg-black transition-colors`,children:`返回登录页`})]})})})}export{u as default};