import{bs as f,aL as i,ah as t,aN as w,aM as h,cn as p}from"./C6MRUCyE.js";const l=async(s=!1)=>{const e=f("user",()=>{});return(!e.value||s||e.value&&Date.now()-e.value.lastUpdated>3e5)&&(e.value=await y()),e},y=async()=>{const s=(await i()).value,e={collections:[],follows:[],subscriptions:[],lastUpdated:0};if(s.user&&s.user.id)try{const o={Authorization:s.token},[n,a,c]=await Promise.all([t(`user/${s.user.id}/follows`,{headers:o},!0),t(`user/${s.user.id}/collections`,{apiVersion:3,headers:o},!0),t("billing/subscriptions",{internal:!0,headers:o},!0)]);e.collections=a,e.follows=n,e.subscriptions=c,e.lastUpdated=Date.now()}catch(o){console.error(o)}return e},v=async()=>{const s=(await i()).value,e=(await l()).value;if(s.user&&s.user.id)try{e.collections=await t(`user/${s.user.id}/collections`,{apiVersion:3})}catch(o){console.error(o)}},$=async()=>{const s=(await i()).value,e=(await l()).value;if(s.user&&s.user.id)try{e.follows=await t(`user/${s.user.id}/follows`)}catch(o){console.error(o)}},b=async()=>{const s=(await i()).value,e=(await l()).value;if(s.user&&s.user.id)try{e.projects=await t(`user/${s.user.id}/projects`),e.projectsV3=await t(`user/${s.user.id}/projects`,{apiVersion:3})}catch(o){console.error(o)}},E=async(s,e)=>{const o=(await l()).value;await v();const n=s.id,a=o.collections.find(r=>r.id===n);if(!a)throw new Error("This collection was not found. Has it been deleted?");const u=!a.projects.includes(e)?[...a.projects,e]:[...a.projects].filter(r=>r!==e),d=o.collections.findIndex(r=>r.id===a.id);d>=0&&(o.collections[d].projects=u),await t(`collection/${s.id}`,{method:"PATCH",body:{new_projects:u},apiVersion:3})},T=async s=>{const e=(await l()).value;e.follows.find(o=>o.id===s.id)?(e.follows=e.follows.filter(o=>o.id!==s.id),s.followers--,setTimeout(()=>{t(`project/${s.id}/follow`,{method:"DELETE"})})):(e.follows=e.follows.concat(s),s.followers++,setTimeout(()=>{t(`project/${s.id}/follow`,{method:"POST"})}))},U=async()=>{await t("auth/email/resend_verify",{method:"POST"}),await i(),w()},j=async()=>{h();const s=await i();try{await t(`session/${s.value.token}`,{method:"DELETE"})}catch{}await i("none"),p("auth-token").value=null,w()};export{l as a,T as b,v as c,$ as d,b as i,j as l,U as r,E as u};