import{s as e}from"./chunk-Bj-mKKzh.js";import{Wo as t}from"./app-server-manager-signals-Yn0WSnoY.js";import{n}from"./jsx-runtime-CiQ1k8xo.js";import{lt as r}from"./vscode-api-DjORcpSo.js";import{i}from"./app-server-manager-hooks-Ps3s58Nc.js";import{n as a}from"./use-is-copilot-api-available-Cju6TzB2.js";import{t as o}from"./use-global-state-baOKTuK6.js";var s=e(n(),1),c=(0,s.createContext)(void 0),l=(0,s.createContext)(null),u=r(),d=2e3,f=new WeakMap;function p(e,t){let n=(0,u.c)(14),{isCopilotApiAvailable:r,useCopilotAuthIfAvailable:i,shouldUseWindowsStartupAuthTimeout:a,onLogout:o}=t,[c,l]=(0,s.useState)(e!=null),[f,p]=(0,s.useState)(null),v,b;n[0]===e?(v=n[1],b=n[2]):(v=()=>{if(e==null){p(null),l(!1);return}p(null),l(!0)},b=[e],n[0]=e,n[1]=v,n[2]=b),(0,s.useEffect)(v,b);let x,S;n[3]!==r||n[4]!==e||n[5]!==o||n[6]!==a||n[7]!==i?(x=()=>{if(e==null)return;let t=!1,n=!1,s=null,c=()=>{g(e).then(e=>{n=!0,s!=null&&clearTimeout(s),!t&&(l(!1),p(y(e,{isCopilotApiAvailable:r,useCopilotAuthIfAvailable:i})))}).catch(()=>{n=!0,s!=null&&clearTimeout(s),t||(l(!1),p(h))})};a&&(s=setTimeout(()=>{t||n||(l(!1),p(m))},d)),c();let u=e=>{p(t=>e.authMethod==null&&t?.authMethod!=null?(o?.(),_()):t==null?e.authMethod==null?t:{..._(),authMethod:e.authMethod}:{...t,authMethod:e.authMethod??null}),c()};return e.addAuthStatusCallback(u),()=>{t=!0,s!=null&&clearTimeout(s),e.removeAuthStatusCallback(u)}},S=[e,r,o,a,i],n[3]=r,n[4]=e,n[5]=o,n[6]=a,n[7]=i,n[8]=x,n[9]=S):(x=n[8],S=n[9]),(0,s.useEffect)(x,S);let C;n[10]===Symbol.for(`react.memo_cache_sentinel`)?(C=e=>{p(t=>({...t??_(),authMethod:e}))},n[10]=C):C=n[10];let w;return n[11]!==f||n[12]!==c?(w={isLoading:c,authState:f,setAuthMethod:C},n[11]=f,n[12]=c,n[13]=w):w=n[13],w}function m(e){return e??_()}function h(e){return e??_()}function g(e){let t=f.get(e);if(t!=null)return t;let n=e.getAccount().finally(()=>{f.delete(e)});return f.set(e,n),n}function _(){return{openAIAuth:null,authMethod:null,requiresAuth:!0,email:null,planAtLogin:null}}function v(e){if(e==null)return null;switch(e.type){case`apiKey`:return`apikey`;case`amazonBedrock`:return null;case`chatgpt`:return`chatgpt`}}function y(e,t){let n=v(e.account),r=t.useCopilotAuthIfAvailable&&t.isCopilotApiAvailable?`copilot`:e.account?.type===`amazonBedrock`?`amazonBedrock`:n;return{openAIAuth:n,authMethod:r,requiresAuth:r===`copilot`||(e.requiresOpenaiAuth??!0),email:e.account?.type===`chatgpt`?e.account.email:null,planAtLogin:e.account?.type===`chatgpt`?e.account.planType:null}}function b(){let e=(0,s.useContext)(c);if(!e)throw Error(`useAuth must be used within AuthProvider`);return e}function x(e){return S(i(e))}function S(e){let n=(0,u.c)(13),r;n[0]===e?r=n[1]:(r=e?.getHostId(),n[0]=e,n[1]=r);let i=r===t,s=a(),{data:c,isLoading:l}=o(`use-copilot-auth-if-available`),d=i?s:!1,f=i&&!l?c??!1:!1,m=i&&document.documentElement.dataset.codexOs===`win32`,h;n[2]!==d||n[3]!==f||n[4]!==m?(h={isCopilotApiAvailable:d,useCopilotAuthIfAvailable:f,shouldUseWindowsStartupAuthTimeout:m},n[2]=d,n[3]=f,n[4]=m,n[5]=h):h=n[5];let{authState:g,isLoading:_,setAuthMethod:v}=p(e,h);if(e==null)return null;let y;n[6]===g?y=n[7]:(y=g??{openAIAuth:null,authMethod:null,requiresAuth:!0,email:null,planAtLogin:null},n[6]=g,n[7]=y);let b=y,x=i?s:!1,S;return n[8]!==_||n[9]!==b||n[10]!==v||n[11]!==x?(S={...b,isLoading:_,isCopilotApiAvailable:x,accountId:null,userId:null,computeResidency:null,setAuthMethod:v},n[8]=_,n[9]=b,n[10]=v,n[11]=x,n[12]=S):S=n[12],S}function C(){let e=(0,s.useContext)(l);if(!e)throw Error(`useUpdateAuthNonce must be used within AuthNonceProvider`);return e}export{l as a,p as i,x as n,c as o,C as r,b as t};
//# sourceMappingURL=use-auth-CI-WraL2.js.map