import{s as e}from"./chunk-Bj-mKKzh.js";import{br as t,xr as n}from"./src-BOCweS1p.js";import{n as r}from"./jsx-runtime-CiQ1k8xo.js";import{A as i,I as a,lt as o,n as s,r as c,s as l,u,v as d}from"./vscode-api-DjORcpSo.js";import{v as f}from"./persisted-signal-CweW-bgN.js";import{t as p}from"./codex-analytics-attribution-Cl49ikQo.js";var m=o(),h=e(r(),1);function g(e){let t=(0,m.c)(26),{turnId:n,cwd:r,hostId:i,openPath:a}=e,o;t[0]!==r||t[1]!==i||t[2]!==a?(o={cwd:r,hostId:i,isQueryEnabled:!1,openPath:a},t[0]=r,t[1]=i,t[2]=a,t[3]=o):o=t[3];let s=b(o),{data:c}=s,l=c?.targets,u;t[4]!==r||t[5]!==i||t[6]!==a||t[7]!==l||t[8]!==n?(u={turnId:n,cwd:r,hostId:i,openPath:a,targets:l},t[4]=r,t[5]=i,t[6]=a,t[7]=l,t[8]=n,t[9]=u):u=t[9];let d=y(u),f;t[10]!==r||t[11]!==a?(f=v({cwd:r,openPath:a}),t[10]=r,t[11]=a,t[12]=f):f=t[12];let p=f,h=c?.preferredTarget??null,g;t[13]===c?.targets?g=t[14]:(g=c?.targets??[],t[13]=c?.targets,t[14]=g);let _;t[15]===c?.availableTargets?_=t[16]:(_=c?.availableTargets??[],t[15]=c?.availableTargets,t[16]=_);let x=c?.mode,S=c!=null,C;return t[17]!==p||t[18]!==d||t[19]!==s.isFetching||t[20]!==h||t[21]!==g||t[22]!==_||t[23]!==x||t[24]!==S?(C={canLoadTargets:p,preferredTarget:h,targets:g,availableTargets:_,mode:x,hasLoadedTargets:S,isLoadingTargets:s.isFetching,open:d},t[17]=p,t[18]=d,t[19]=s.isFetching,t[20]=h,t[21]=g,t[22]=_,t[23]=x,t[24]=S,t[25]=C):C=t[25],C}function _(e){let t=(0,m.c)(23),{cwd:n,delayMs:r,hostId:i,openPath:a}=e,o=r===void 0?0:r,s;t[0]!==n||t[1]!==a?(s=v({cwd:n,openPath:a}),t[0]=n,t[1]=a,t[2]=s):s=t[2];let c=s,l=o<=0,u;t[3]!==n||t[4]!==i||t[5]!==a||t[6]!==l?(u={cwd:n,hostId:i,isQueryEnabled:l,openPath:a},t[3]=n,t[4]=i,t[5]=a,t[6]=l,t[7]=u):u=t[7];let{data:d,refetch:f}=b(u),p;t[8]===f?p=t[9]:(p=()=>{f()},t[8]=f,t[9]=p);let g=(0,h.useEffectEvent)(p),_;t[10]!==c||t[11]!==d||t[12]!==o||t[13]!==g||t[14]!==l?(_=()=>{if(l||!c||d!=null)return;let e=window.setTimeout(g,o);return()=>{window.clearTimeout(e)}},t[10]=c,t[11]=d,t[12]=o,t[13]=g,t[14]=l,t[15]=_):_=t[15];let y;return t[16]!==c||t[17]!==n||t[18]!==d||t[19]!==o||t[20]!==a||t[21]!==l?(y=[c,n,d,o,a,l],t[16]=c,t[17]=n,t[18]=d,t[19]=o,t[20]=a,t[21]=l,t[22]=y):y=t[22],(0,h.useEffect)(_,y),null}function v({cwd:e,openPath:t}){return!!(e??t)}function y(e){let t=(0,m.c)(10),{turnId:n,cwd:r,hostId:o,openPath:s,targets:u}=e,d=i(),h=l(`open-file`),g=a(f),_=a(p),v;return t[0]!==r||t[1]!==o||t[2]!==h||t[3]!==s||t[4]!==g||t[5]!==d||t[6]!==_||t[7]!==u||t[8]!==n?(v=(e,t)=>{let{appPath:i,openMode:a,persistPreferred:l,line:f,column:p,path:m}=t,v=m??s??r;v&&(u?.find(t=>t.target===e&&(t.appPath??null)===(i??null))?.kind===`editor`&&_!=null&&n!=null&&g.submitCodexAnalyticsEvent?.({action:`open_in_ide`,eventKind:`action`,metadata:{target:e},threadId:_.threadId,turnId:n}),l&&r&&d.setQueryData(c(`open-in-targets`,{cwd:r,hostId:o,path:s}),t=>t&&{...t,preferredTarget:e,targets:t.targets.map(t=>({...t,default:t.target===e?!0:void 0}))}),h.mutate({path:v,cwd:r??null,target:e,...i==null?{}:{appPath:i},...f==null?{}:{line:f},...p==null?{}:{column:p},...a==null?{}:{openMode:a},...l&&r?{persistPreferredTargetPath:r}:{},...o==null?{}:{hostId:o}}))},t[0]=r,t[1]=o,t[2]=h,t[3]=s,t[4]=g,t[5]=d,t[6]=_,t[7]=u,t[8]=n,t[9]=v):v=t[9],v}function b(e){let t=(0,m.c)(13),{cwd:n,hostId:r,isQueryEnabled:a,openPath:o}=e,s=i(),l,f,p;t[0]!==n||t[1]!==r||t[2]!==o?(l={cwd:n,hostId:r,path:o},p=d,f=c(`open-in-targets`,l),t[0]=n,t[1]=r,t[2]=o,t[3]=l,t[4]=f,t[5]=p):(l=t[3],f=t[4],p=t[5]);let h;t[6]!==l||t[7]!==s?(h=()=>x({params:l,queryClient:s}),t[6]=l,t[7]=s,t[8]=h):h=t[8];let g=a&&!!(n??o),_;return t[9]!==f||t[10]!==h||t[11]!==g?(_={queryKey:f,queryFn:h,enabled:g,staleTime:u.ONE_MINUTE},t[9]=f,t[10]=h,t[11]=g,t[12]=_):_=t[12],p(_)}async function x({params:e,queryClient:t}){if(S(e.path)){let n=await s(`open-in-targets`,{params:{...e,nativeBrowserDiscovery:`known`}});n.targets.some(e=>e.kind===`native`&&e.appPath!=null)&&t.setQueryData(c(`open-in-targets`,e),n)}return s(`open-in-targets`,{params:e})}function S(e){if(e==null)return!1;if(t(e)||n(e))return!0;try{let t=new URL(e);return t.protocol===`http:`||t.protocol===`https:`}catch{return!1}}export{g as n,_ as t};
//# sourceMappingURL=use-target-apps-DeqdTS88.js.map