import{d as O,r as j,l as g}from"./jwt-decode.esm-C7p7KYmD.js";import{u as P}from"./organizationSwitcher-Cqv_Kzfl.js";import{C as u}from"./router-KqhyGitc.js";import{a as y}from"./project-CR7-nfeN.js";import{T as I}from"./tables-BD2MpLEl.js";(function(){try{var i=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},t=new Error().stack;t&&(i._sentryDebugIds=i._sentryDebugIds||{},i._sentryDebugIds[t]="4e8eac19-b650-4e60-b6c5-36a0b58ea06c",i._sentryDebugIdIdentifier="sentry-dbid-4e8eac19-b650-4e60-b6c5-36a0b58ea06c")}catch{}})();class C{async list(t){return(await u.get(`organizations/${t}/folders`)).sort((d,b)=>d.name.localeCompare(b.name))}async create(t){return u.post(`organizations/${t.organizationId}/folders`,{name:t.name,memberIds:t.memberIds})}async checkNameAvailability(t,l){return u.post(`organizations/${t}/folders/name-available`,{folderName:l})}async delete(t,l){return u.delete(`organizations/${t}/folders/${l}`)}async update(t,l){return u.patch(`organizations/${t}/folders/${l.id}/`,{name:l.name,memberIds:l.memberIds})}}const G=O("cascader",()=>{const i=P(),t=j({folders:{},projects:{},tables:{}}),l=j([]),d=(e,o,a)=>({value:`__header_action_${o}_${a||"root"}`,label:g.translate(e),disabled:!1,isLeaf:!0,level:"header",class:"cascader-header-action"}),b=(e,o)=>{const r=[...e].sort((s,c)=>s.name.localeCompare(c.name)).map(s=>({value:s.id,label:s.name,isLeaf:!0,level:"table"}));return[d("i18n_breadcrumb_tables","table",o),...r]},z=(e,o,a,r)=>{const c=e.filter(n=>n.folderId===a).sort((n,f)=>n.name.localeCompare(f.name)).map(n=>{const f=t.value.tables[n.id],_=r&&!!f;return{value:n.id,label:n.name,isLeaf:!r,level:"project",children:_?b(f,n.id):void 0}});return[d("i18n_breadcrumb_projects","project",a),...c]},v=(e,o,a)=>{const r=t.value.projects[e]||[],c=[...o].sort((n,f)=>n.name.localeCompare(f.name)).map(n=>{const _=r.filter(w=>w.folderId===n.id).length>0;return{value:n.id,label:n.name,isLeaf:!1,level:"folder",children:_?z(r,e,n.id,a):[d("i18n_breadcrumb_projects","project",n.id)]}});return[d("i18n_breadcrumb_folders","folder",e),...c]},h=(e=!1)=>{const a=[...i.organizations].sort((r,s)=>r.name.localeCompare(s.name)).map(r=>{const s=t.value.folders[r.id],c=!!s;return{value:r.id,label:r.name,isLeaf:!1,level:"org",children:c?v(r.id,s,e):void 0}});return[d("i18n_breadcrumb_organizations","organization"),...a]},m=async e=>{if(t.value.folders[e])return;const o=new C,[a,r]=await Promise.all([o.list(e),y.list(e)]);t.value.folders[e]=a,t.value.projects[e]=r},p=async e=>{if(t.value.tables[e])return;const o=await I.list(e);t.value.tables[e]=o};return{orgStore:i,cache:t,cascaderOptions:l,loadData:async(e,o)=>{const a=e[e.length-1];if(a.level==="org"){const r=a.value;if(t.value.folders[r])return;a.loading=!0;try{await m(r),a.children=v(r,t.value.folders[r],o)}catch(s){console.error("Failed to load folders for org",r,s),a.children=[{value:"__error_loading",label:g.translate("i18n_breadcrumb_load_error"),isLeaf:!0,disabled:!0}]}finally{a.loading=!1}}else if(a.level==="folder")(!a.children||a.children.length<=1)&&(a.isLeaf=!0);else if(a.level==="project"&&o){const r=a.value;if(t.value.tables[r])return;a.loading=!0;try{await p(r),a.children=b(t.value.tables[r],r)}catch(s){console.error("Failed to load tables for project",r,s),a.children=[{value:"__error_loading",label:g.translate("i18n_breadcrumb_load_error"),isLeaf:!0,disabled:!0}]}finally{a.loading=!1}}l.value=[...l.value]},initialize:async(e,o,a=!1)=>{i.organizations.length===0&&await i.fetchOrganizations(),e&&await m(e),o&&a&&await p(o),l.value=h(a)},initializeFromProjectId:async(e,o=!1,a,r)=>{try{i.organizations.length===0&&await i.fetchOrganizations();let s,c;if(a&&r)s=a,c=r;else{const n=await y.get(e);s=n.organizationId,c=n.folderId}return await m(s),o&&await p(e),l.value=h(o),{organizationId:s,folderId:c}}catch(s){return console.error("Failed to initialize cascader from projectId",e,s),null}},refreshOptions:(e=!1)=>{l.value=h(e)},invalidateCache:(e,o)=>{o?delete t.value[e][o]:t.value[e]={}},loadOrgData:m,loadProjectTables:p,parseHeaderAction:e=>{if(!e.startsWith("__header_action_"))return null;const a=e.replace("__header_action_","").split("_"),r=a[0],s=a.slice(1).join("_")||"root";return{type:r,parentId:s}},findProjectInCache:e=>{for(const[o,a]of Object.entries(t.value.projects)){const r=a.find(s=>s.id===e);if(r)return{organizationId:o,folderId:r.folderId}}return null}}});export{C as F,G as u};
//# sourceMappingURL=cascaderStore-BvBcLa9w.js.map
