import{o as e}from"./rolldown-runtime-iuO8RvI4.js";import{Ch as r,Eu as t,Hm as s,K_ as a,gy as o}from"./shared-0-C4kdVF7Y.js";import{Mi as n,ki as i}from"./vendor-frame-CwiylG4Y.js";import{Au as u,Fu as l,Lu as d,Mh as m,Mu as g,Qf as c,ku as f}from"./shared-1-3-6x7RKF.js";import{nn as h,rn as w}from"./vendor-all-2-BJdkbLSj.js";import{m as p}from"./c029a409d-B2djD7sn.js";import{t as S}from"./c9a88b974-CGUx29Y3.js";var _=e(n()),k=i();function v({onStatusChange:e,onMintingChange:n}){const i=w(),{addError:v}=m(),{status:C,domain:b,source:y}=S(),E=s("Mutation error"),M=r(),L=t(),[j,A]=(0,_.useState)(!1),x=(0,_.useRef)(!0);(0,_.useLayoutEffect)(()=>{e?.(C)},[C,e]),(0,_.useLayoutEffect)(()=>(x.current=!0,()=>{x.current=!1}),[]),(0,_.useLayoutEffect)(()=>()=>{e?.("disabled"),n?.(!1)},[e,n]);const O=(0,_.useCallback)(async()=>{if(!b)return;A(!0),n?.(!0);let e,r=!1;try{const t=L.get("returnTo"),s=t&&p(t)?t:null;s&&M.set(a.RETURN_TO,s,{maxAgeSeconds:3600}),u();const m=new URLSearchParams({client:"desktop_app",email_domain:b});s&&m.set("return_to",s);const g=await E(`/api/enterprise_auth/sso_url_for_domain?${m.toString()}`,{method:"GET",signal:AbortSignal.timeout(15e3)});if(!x.current)return;if(!g.url||!g.url.startsWith("https://"))throw new Error("No SSO URL in response");if(!o?.doAuthInBrowser)throw new Error("Auth.doAuthInBrowser unavailable in this build");if("mdm"===y)l(b);else{f();const e=new URL(g.url).searchParams.get("state");if(!e)throw new Error("SSO authorize URL missing CSRF state");d(e),r=!0}n?.(!1),await Promise.race([o.doAuthInBrowser(g.url).then(()=>!1),new Promise(r=>{e=setTimeout(()=>r(!0),3e5)})])&&x.current&&v(i.formatMessage({defaultMessage:"Sign-in is taking longer than expected. If the sign-in window isn\u2019t responding, close it and try again, or use Sign in another way.",id:"BvkzBKvp0E"}),{errorContext:{tags:{source:"desktop_enterprise_sso_watchdog"}},messageForLogging:"enterprise SSO ASWebAuth did not resolve within watchdog window"})}catch(t){if(f(),r&&g(),!x.current)return;v(i.formatMessage({defaultMessage:"Couldn\u2019t start SSO. Try again, or sign in with your work email.",id:"mv8MJb3+CT"}),{error:t instanceof Error?t:void 0,errorContext:{tags:{source:"desktop_enterprise_sso"}},messageForLogging:"enterprise SSO click-time mint or open failed"})}finally{clearTimeout(e),x.current&&A(!1),n?.(!1)}},[b,y,E,v,i,M,L,n]);return"disabled"===C||"unavailable"===C?null:(0,k.jsx)(c,{variant:"primary",size:"lg",className:"w-full gap-2",onClick:()=>{O()},loading:j||"loading"===C,disabled:j||"loading"===C,children:"lastLogin"===y&&b?(0,k.jsx)(h,{defaultMessage:"Continue with {domain}",id:"GPqvF5CJOg",values:{domain:b}}):(0,k.jsx)(h,{defaultMessage:"Continue with SSO",id:"qank7+0V6w"})})}export{v as DesktopEnterpriseSsoButton};