import{o as e}from"./rolldown-runtime-iuO8RvI4.js";import{At as t}from"./vendor-all-0-fRSUBfCr.js";import{Ll as s,Sm as r,vm as n,ym as i}from"./shared-0-C4kdVF7Y.js";import{Mi as a}from"./vendor-frame-CwiylG4Y.js";import{jn as c}from"./shared-1-3-6x7RKF.js";import{t as o}from"./c29f92475--ned95yt.js";import{X as u,Z as f}from"./shared-4-6CEyRAHb.js";import{Kn as d,an as l}from"./shared-2-CMUBhYOn.js";var m=e(a());function p(e){const t=()=>{"visible"===document.visibilityState&&e()};return document.addEventListener("visibilitychange",t),window.addEventListener("focus",t),()=>{document.removeEventListener("visibilitychange",t),window.removeEventListener("focus",t)}}var y=r;function h(){const{activeOrganization:e}=s(),t=e?.uuid,r=(0,m.useRef)(0);(0,m.useEffect)(()=>()=>{r.current+=1},[]);const n=(0,m.useRef)(t);return(0,m.useLayoutEffect)(()=>{n.current=t},[t]),(0,m.useCallback)(()=>{const e=++r.current,s=t;return{superseded:()=>e!==r.current||s!==n.current,owns:()=>e===r.current}},[t])}function R(e){const s=e?.invalidateAuth??!1,r=t(),{refresh:a,isRefreshing:R,isRepoListFetching:v}=function(e){const[s,r]=(0,m.useState)(!1),n=l({skipStatus:!1,enabled:!1,...void 0!==e?.repoListTimeoutMs&&{timeoutMs:e.repoListTimeoutMs}}),{forceRefresh:a}=d(),u=h(),f=t(),p=o({predicate:e=>"string"==typeof e.queryKey[0]&&y.includes(e.queryKey[0])});return{refresh:(0,m.useCallback)(async t=>{const s=u();r(!0);try{if(!t?.skipResync){const t=await n.refetch();if(s.superseded())return;if(void 0!==e?.repoListTimeoutMs&&t.error)return void(await f.invalidateQueries({queryKey:[i]},{cancelRefetch:!0}));await c((t.data??n.data)?.repos??[],8,async e=>{s.superseded()||await a(e.repo)})}if(s.superseded())return;const[r]=await Promise.all([n.refetch({cancelRefetch:!t?.skipResync}),f.invalidateQueries({queryKey:[i]},{cancelRefetch:!t?.skipResync})]);return s.superseded()?void 0:r?.data}finally{s.owns()&&r(!1)}},[n,a,u,f,e?.repoListTimeoutMs]),isRefreshing:s,isRepoListFetching:n.isFetching||p>0}}({repoListTimeoutMs:e?.repoListTimeoutMs}),[L,E]=(0,m.useState)(!1),w=(0,m.useCallback)(async e=>{E(!0);try{s&&await r.invalidateQueries({queryKey:[n]},{cancelRefetch:!1});const t=await a(e);j.current?.(t)}finally{E(!1)}},[s,r,a]),g=(0,m.useRef)(w),k=(0,m.useRef)(a);(0,m.useEffect)(()=>{g.current=w,k.current=a},[w,a]);const b=(0,m.useRef)(e?.onInstallComplete),j=(0,m.useRef)(e?.onRefreshed);(0,m.useEffect)(()=>{b.current=e?.onInstallComplete,j.current=e?.onRefreshed},[e?.onInstallComplete,e?.onRefreshed]);const M=(0,m.useRef)(null);return(0,m.useEffect)(()=>p(()=>{const e=M.current;e&&(M.current=null,g.current(e))}),[]),function(e){const t=(0,m.useRef)(e);(0,m.useEffect)(()=>{t.current=e},[e]),(0,m.useEffect)(()=>{const e=f(),s=s=>{"GITHUB_INSTALL_COMPLETE"===s.data.type&&(u(e),t.current(s.data))};return e.addEventListener("message",s),()=>{e.removeEventListener("message",s),e.close()}},[])}(e=>{b.current?.(e),k.current().then(e=>j.current?.(e))}),{arm:(0,m.useCallback)(e=>{const t=e?.skipResync??!1,s=M.current?.skipResync??!0;M.current={skipResync:t&&s}},[]),refresh:w,isRefreshing:R||L,isRepoListFetching:v}}export{h as n,p as r,R as t};