(()=>{function e(e,r=3e5){return!e?.idToken||!e?.expiresAt||Number(e.expiresAt)-Date.now()<=r}function r(e){return["INVALID_REFRESH_TOKEN","TOKEN_EXPIRED","USER_DISABLED","USER_NOT_FOUND"].includes(String(e||""))}window.LiveSubtitleFirebaseAuth={AUTH_STORAGE_KEY:"liveSubtitleAuth",publicAuth:function(e){return e?.idToken&&e?.uid?{uid:e.uid,email:e.email||"",idToken:e.idToken,refreshToken:e.refreshToken||"",expiresAt:e.expiresAt||0,isAnonymous:!0===e.isAnonymous||"anonymous"===e.providerId,providerId:e.providerId||"",anonymousTrialReady:!0===e.anonymousTrialReady,anonymousTrialCredits:Math.max(0,Math.round(Number(e.anonymousTrialCredits||0)))}:null},refreshSession:async function(n){if(!n?.refreshToken)throw new Error("缺少 refresh token，請重新登入。");if(!e(n))return n;const o=await fetch("https://securetoken.googleapis.com/v1/token?key=AIzaSyCGbS_67-COo9Ex4KvzeRjbRrniM_qr5B8",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({grant_type:"refresh_token",refresh_token:n.refreshToken})}),t=await async function(e){const n=await e.json().catch((()=>({})));if(!e.ok){const o=n?.error?.message||`Firebase request failed: ${e.status}`,t=new Error(function(e){return{INVALID_REFRESH_TOKEN:"登入已過期，請重新登入。",TOKEN_EXPIRED:"登入已過期，請重新登入。",USER_DISABLED:"此帳號已停用。",USER_NOT_FOUND:"找不到此 Firebase 會員。"}[e]||e}(o));throw t.firebaseCode=o,t.permanentAuthFailure=r(o),t}return n}(o);return{...n,uid:t.user_id||n.uid,idToken:t.id_token,refreshToken:t.refresh_token||n.refreshToken,expiresAt:(i=t.expires_in,Date.now()+1e3*Number(i||3600)),updatedAt:(new Date).toISOString()};var i},isTokenExpiring:e,isPermanentAuthError:function(e){return!!e&&(!0===e.permanentAuthFailure||r(e.firebaseCode||e.code||e.message||""))}}})();
