import{i as e}from"./rolldown-runtime-B1FJdls4.js";import{m as t}from"./vendor-dnd-C1cICjTT.js";import{n}from"./axisEvents-Dhd0R1o7.js";var r=e(t(),1),i=new Map,a=new Map,o=new Map,s=1e4,c={backend:0,frontend:1,home:2};function l(e){return[...e].sort((e,t)=>{if(e.source===`manifest`!=(t.source===`manifest`))return e.source===`manifest`?-1:1;if(e.source===`manifest`&&t.source===`manifest`){let n=c[e.manifest_role??`home`]??99,r=c[t.manifest_role??`home`]??99;if(n!==r)return n-r}return e.is_favorite===t.is_favorite?e.is_primary===t.is_primary?e.pinned===t.pinned?t.last_opened_at-e.last_opened_at:e.pinned?-1:1:e.is_primary?-1:1:e.is_favorite?-1:1})}function u(e){let t=e?i.get(e):void 0,[c,u]=(0,r.useState)(t??[]),[d,f]=(0,r.useState)(t!==void 0),p=(0,r.useRef)(e),m=(0,r.useCallback)((e,t)=>{let n=l(t);i.set(e,n),p.current===e&&u(n)},[]);return(0,r.useEffect)(()=>{if(p.current=e,!e){u([]),f(!0);return}let t=i.get(e);if(t!==void 0){u(t),f(!0);return}let n=!1;return fetch(`/api/projects/browsers?project_path=${encodeURIComponent(e)}`).then(e=>e.ok?e.json():{browsers:[]}).then(t=>{if(n)return;let r=l(t.browsers??[]);i.set(e,r),u(r),f(!0)}).catch(()=>{n||f(!0)}),()=>{n=!0}},[e]),(0,r.useEffect)(()=>{let e=n(`mc:browser-created`,e=>{if(!e)return;let t=i.get(e.project_path)??[];t.some(t=>t.id===e.id)||m(e.project_path,[...t,e])}),t=n(`mc:browser-updated`,e=>{if(!e)return;let t=(i.get(e.project_path)??[]).map(t=>t.id===e.id?e:t);m(e.project_path,t)}),r=n(`mc:browser-deleted`,e=>{let t=e?.id,n=e?.project_path;!t||!n||m(n,(i.get(n)??[]).filter(e=>e.id!==t))});return()=>{e(),t(),r()}},[m]),{browsers:c,loaded:d,create:(0,r.useCallback)(async t=>{if(!e)return null;let n=await fetch(`/api/projects/browsers`,{method:`POST`,headers:{"content-type":`application/json`},body:JSON.stringify({project_path:e,...t})});return n.ok?(await n.json()).browser:null},[e]),update:(0,r.useCallback)(async(e,t)=>{let n=await fetch(`/api/projects/browsers/${encodeURIComponent(e)}`,{method:`PATCH`,headers:{"content-type":`application/json`},body:JSON.stringify(t)});return n.ok?(await n.json()).browser:null},[]),remove:(0,r.useCallback)(async e=>{let t,n=-1,r=null;for(let[a,o]of i.entries()){let i=o.findIndex(t=>t.id===e);if(i!==-1){t=o[i],n=i,r=a,m(a,o.filter(t=>t.id!==e));break}}if((await fetch(`/api/projects/browsers/${encodeURIComponent(e)}`,{method:`DELETE`}).catch(()=>null))?.ok)return!0;if(t&&r){let a=i.get(r)??[];if(!a.some(t=>t.id===e)){let e=[...a];e.splice(Math.max(0,n),0,t),m(r,e)}}return!1},[m]),touch:(0,r.useCallback)(async e=>{await fetch(`/api/projects/browsers/${encodeURIComponent(e)}`,{method:`PATCH`,headers:{"content-type":`application/json`},body:JSON.stringify({last_opened_at:Date.now()})}).catch(()=>{})},[]),sweepCold:(0,r.useCallback)(async t=>{if(!e)return 0;let n=await fetch(`/api/projects/browsers/sweep-cold`,{method:`POST`,headers:{"content-type":`application/json`},body:JSON.stringify({project_path:e,attached_urls:t??[]})}).catch(()=>null);return n?.ok?(await n.json()).removed_count??0:0},[e]),reconcileManifest:(0,r.useCallback)(async()=>{if(!e)return null;let t=a.get(e);if(t&&Date.now()-t.at<s)return m(e,t.value.browsers),t.value;let n=o.get(e);n||(n=fetch(`/api/projects/browsers/reconcile`,{method:`POST`,headers:{"content-type":`application/json`},body:JSON.stringify({project_path:e})}).then(async t=>{if(!t.ok)return null;let n=await t.json();return a.set(e,{at:Date.now(),value:n}),n}).catch(()=>null),o.set(e,n));let r=await n;return o.get(e)===n&&o.delete(e),r?(Array.isArray(r.browsers)&&m(e,r.browsers),r):null},[e,m])}}export{u as t};