var intermediate=function(c){"use strict";var A,L;function M(){var a,u;const e=chrome.runtime.getManifest(),t=(a=e.oauth2)==null?void 0:a.client_id;if(!t)throw new Error("Invalid client ID");const r=new URL("https://accounts.google.com/o/oauth2/auth"),o=chrome.identity.getRedirectURL("oauth2"),n=((u=e.oauth2)==null?void 0:u.scopes)||["https://www.googleapis.com/auth/userinfo.email"];return r.searchParams.set("client_id",t),r.searchParams.set("redirect_uri",o),r.searchParams.set("response_type","token"),r.searchParams.set("scope",n.join(" ")),r.searchParams.set("prompt","select_account"),{url:r}}function R(e){return new Promise((t,r)=>{chrome.identity.launchWebAuthFlow({url:e,interactive:!0},o=>{if(chrome.runtime.lastError){r(chrome.runtime.lastError.message);return}t(o||"")})})}const U={name:"getCookies",handler:async(e,t)=>{const r=await chrome.cookies.get({url:"https://www.overleaf.com",name:"overleaf_session2"}),o=await chrome.cookies.get({url:"https://www.overleaf.com",name:"GCLB"});t({session:r==null?void 0:r.value,gclb:o==null?void 0:o.value})}},I={name:"revokeGoogleAuthToken",handler:async(e,t)=>{await chrome.identity.removeCachedAuthToken({token:e});const r=await fetch(`https://accounts.google.com/o/oauth2/revoke?token=${e}`);t(r.ok)}},k={name:"getGoogleAuthToken",handler:async(e,t)=>{const{url:r}=M(),o=await R(r.href);if(!o)throw new Error("Authentication failed: no redirect URL received");const n=new URLSearchParams(new URL(o).hash.substring(1)),a=n.get("access_token"),u=n.get("error");if(a)t({token:a});else throw u?new Error(`OAuth error: ${u}`):new Error("No access token found in redirect URL")}},_={name:"getManifest",handler:async(e,t)=>{t(chrome.runtime.getManifest())}},S={name:"getUrl",handler:async(e,t)=>{t(chrome.runtime.getURL(e))}},$={name:"getOrCreateSessionId",handler:async(e,t)=>{const o=Date.now();let{sessionData:n}=await chrome.storage.session.get("sessionData");n&&n.timestamp&&((o-n.timestamp)/6e4>30?n=null:(n.timestamp=o,await chrome.storage.session.set({sessionData:n}))),n||(n={session_id:o.toString(),timestamp:o.toString()},await chrome.storage.session.set({sessionData:n})),t(n.session_id)}};(L=(A=chrome==null?void 0:chrome.runtime)==null?void 0:A.onMessage)==null||L.addListener((e,t,r)=>{const n=[U,k,I,_,S,$].find(a=>a.name===e.action);return n&&(async()=>{var a;try{await((a=n.handler)==null?void 0:a.call(n,e.args,r))}catch(u){r({error:u})}})(),!0});const s=[];for(let e=0;e<256;++e)s.push((e+256).toString(16).slice(1));function b(e,t=0){return(s[e[t+0]]+s[e[t+1]]+s[e[t+2]]+s[e[t+3]]+"-"+s[e[t+4]]+s[e[t+5]]+"-"+s[e[t+6]]+s[e[t+7]]+"-"+s[e[t+8]]+s[e[t+9]]+"-"+s[e[t+10]]+s[e[t+11]]+s[e[t+12]]+s[e[t+13]]+s[e[t+14]]+s[e[t+15]]).toLowerCase()}let w;const C=new Uint8Array(16);function N(){if(!w){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");w=crypto.getRandomValues.bind(crypto)}return w(C)}const T={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function O(e,t,r){var n;if(T.randomUUID&&!e)return T.randomUUID();e=e||{};const o=e.random??((n=e.rng)==null?void 0:n.call(e))??N();if(o.length<16)throw new Error("Random bytes length must be >= 16");return o[6]=o[6]&15|64,o[8]=o[8]&63|128,b(o)}const P=5e3;function l(e,t){const r=`paperdebugger:req:${e.name}`,o=`paperdebugger:res:${e.name}`,n=`paperdebugger:err:${e.name}`,a=v=>{var h;const m=v,{seq:i,req:g}=m.detail;i&&(h=chrome.runtime)!=null&&h.id&&chrome.runtime.sendMessage({action:e.name,args:g}).then(d=>{d.error?window.dispatchEvent(new CustomEvent(`${n}/${i}`,{detail:{seq:i,err:d.error}})):window.dispatchEvent(new CustomEvent(`${o}/${i}`,{detail:{seq:i,res:d}}))}).catch(d=>{window.dispatchEvent(new CustomEvent(`${n}/${i}`,{detail:{seq:i,err:d}}))})};return window.addEventListener(r,a),v=>{const m=O();return window.dispatchEvent(new CustomEvent(r,{detail:{seq:m,req:v}})),new Promise((i,g)=>{function h(y){const f=y,{res:p}=f.detail;E(),i(p)}function d(y){const f=y,{err:p}=f.detail;E(),g(p)}function E(){window.removeEventListener(`${o}/${m}`,h),window.removeEventListener(`${n}/${m}`,d)}window.addEventListener(`${o}/${m}`,h),window.addEventListener(`${n}/${m}`,d),t!=null&&t.wait||setTimeout(()=>{E(),g(new Error("request timeout"))},P)})}}const D=l(U),G=l(k,{wait:!0}),H=l(I),q=l(_),V=l(S),F=l($);return c.getCookies=D,c.getGoogleAuthToken=G,c.getManifest=q,c.getOrCreateSessionId=F,c.getUrl=V,c.revokeGoogleAuthToken=H,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"}),c}({});
