import{Kn as e}from"./src-BOCweS1p.js";import"./app-server-manager-signals-Yn0WSnoY.js";import{A as t,at as n,g as r,n as i,r as a}from"./vscode-api-DjORcpSo.js";import{n as o}from"./react-0SBwfqBm.js";import{t as s}from"./request-CBZ5m5yv.js";import{t as c}from"./invalidate-queries-and-broadcast-CivBKbpC.js";import{a as l}from"./apps-queries-D95oCTjw.js";import{t as u}from"./codex-api-error-C0Z_xLe-.js";async function d({hostId:t,queryClient:n,refreshAppsList:r}){let o=n.getQueryData([`apps`,`list`,t]),s=await r();return f({previousApps:o,nextApps:s})?(await i(`ambient-suggestions-refresh`,{params:{hostId:t,projectRoot:e(`~`),mode:`first-plugin-connect`}}),await Promise.all([n.invalidateQueries({queryKey:a(`ambient-suggestions`)}),n.invalidateQueries({queryKey:a(`ambient-suggestions-refresh`)})]),s):s}function f({previousApps:e,nextApps:t}){return e!=null&&!e.some(p)&&t.some(p)}function p(e){return e.isAccessible&&e.isEnabled}var m=n({});function h(){let[e,t]=o(m);return{claimAppConnectOAuthCallback:n=>{let r=v(n);if(r==null)return!1;let i=e[r]??null;return i==null||i.claimed?!1:(t(e=>{let t=e[r]??null;return t==null||t.claimed?e:{...e,[r]:{...t,claimed:!0}}}),!0)},clearPendingAppConnect:({oauthState:e,appId:n})=>{if(e!=null){t(t=>{if(!(e in t))return t;let{[e]:n,...r}=t;return r});return}n!=null&&t(e=>Object.entries(e).filter(([,e])=>e.appId!==n).reduce((e,[t,n])=>(e[t]=n,e),{}))},getPendingAppConnectForCallbackUrl:t=>{let n=v(t);return n==null?null:e[n]??null},isAppConnectPending:t=>Object.values(e).some(e=>e.appId===t),markAppConnectOAuthPending:({app:e,hostId:n,redirectUrl:r,returnTo:i,resumeTarget:a={kind:`apps-tab`}})=>{let o=v(r);o!=null&&t(t=>({...t,[o]:{appId:e.id,appName:e.name,claimed:!1,hostId:n??`local`,oauthState:o,returnTo:i,resumeTarget:a}}))}}}function g(){let e=t(),n=c(),{clearPendingAppConnect:i,getPendingAppConnectForCallbackUrl:a}=h();return async({fullRedirectUrl:t})=>{let o=t.trim(),c=o.length===0?null:a(o),f=c?.hostId??`local`,p=c?.oauthState??v(o)??void 0;if(o.length===0)return i({oauthState:p}),{kind:`missing-callback-data`};try{return{kind:`success`,appName:(await s.safePost(`/aip/connectors/links/oauth/callback`,{requestBody:{full_redirect_url:o}})).link?.name?.trim()||c?.appName||`App`}}catch(e){r.error(`Failed to finish app OAuth callback`,{safe:{},sensitive:{error:e}});let t=(e instanceof Error?u(e):null)?.message.trim();return t?{kind:`request-failed`,message:t}:{kind:`request-failed`}}finally{await Promise.allSettled([d({hostId:f,queryClient:e,refreshAppsList:async()=>l({hostId:f,queryClient:e})}),n([`mcp-settings`]),n([`mcp-settings`,`app-connect`])]),i({oauthState:p})}}}function _(e){if(typeof e!=`object`||!e)return null;let t=Reflect.get(e,`fullRedirectUrl`),n=Reflect.get(e,`returnTo`);return typeof t!=`string`||n!=null&&typeof n!=`string`?null:{fullRedirectUrl:t,returnTo:n??void 0}}function v(e){return y(e,`state`)}function y(e,t){try{return new URL(e).searchParams.get(t)?.trim()||null}catch{return null}}export{d as i,h as n,g as r,_ as t};
//# sourceMappingURL=app-connect-oauth-BbS4dGUT.js.map