import{g as f}from"./request-a636442b.js";import{D as I}from"./drb-toast-helpers-b5930a70.js";var h,m,L;const q={googleClientId:(L=(m=(h=window.Dribbble)==null?void 0:h.JsConfig)==null?void 0:m.user)==null?void 0:L.googleClientId,googleLibraryLoaded:!1,init(){if(!this.googleClientId){console.warn("Google Client ID is not configured");return}window.onGoogleLibraryLoad=()=>{var e,s,a,i;this.googleLibraryLoaded=!0,(a=(s=(e=window.Dribbble)==null?void 0:e.JsConfig)==null?void 0:s.user)!=null&&a.isLoggedIn||(window.google.accounts.id.initialize({client_id:this.googleClientId,callback:this.handleCredentialResponse,ux_mode:"popup"}),document.body.hasAttribute("data-disable-google-one-tap")||window.google.accounts.id.prompt()),(i=window.googleLoadCallbacks)==null||i.forEach(o=>o())}},registerLoadCallback(e){if(typeof e!="function")throw new Error("Callback must be a function");if(this.googleLibraryLoaded){e();return}window.googleLoadCallbacks||(window.googleLoadCallbacks=[]),window.googleLoadCallbacks.push(e)},async handleCredentialResponse({clientId:e,credential:s,state:a}){var l,d,g,c,u,w,b,p;const i=!a,o=(l=document.querySelector(`[data-google-sign-in-button="${a}"]`))==null?void 0:l.closest("[data-screen]"),n="Failed to authenticate with Google.";(g=(d=o==null?void 0:o.screenInstance)==null?void 0:d.formHandler)==null||g.toggleFormSubmitting(!0);try{const t=await fetch("/auth/jwt",{method:"POST",body:JSON.stringify({client_id:e,credential:s,state:a}),headers:{"X-Requested-With":"XMLHttpRequest","Content-Type":"application/json","X-CSRF-Token":f()}});if(!t.ok){let r;throw t.status===422&&(r=await t.text()),new Error(r||n)}if(o)(c=o.screenInstance)==null||c.handleSuccessResponse(t);else{const{redirect_to:r="/"}=await t.json();window.location.href=r}}catch(t){const r=t instanceof Error?t.message:n;(w=(u=o==null?void 0:o.screenInstance)==null?void 0:u.formHandler)==null||w.setServerMessage(r),(p=(b=o==null?void 0:o.screenInstance)==null?void 0:b.formHandler)==null||p.toggleFormSubmitting(!1),console.error(r,t),i&&I.open({title:"Google Sign-In Error",messageHtml:r})}},requestAuthCode(e){if(!this.googleClientId&&!(e!=null&&e.client_id)||!(e!=null&&e.scope)||!(e!=null&&e.callback))throw new Error("`clientId`, `callback`, and `scope` are required to request auth code");this.registerLoadCallback(()=>{var s;(s=window.google.accounts.oauth2.initCodeClient({ux_mode:"popup",client_id:this.googleClientId,...e}))==null||s.requestCode()})}};export{q as G}; //# sourceMappingURL=google-sign-in-8044a0ab.js.map