import{d as w,r as g,l as m}from"./jwt-decode.esm-CuqABuF0.js";import{u as P}from"./organizationSwitcher-C3nwECnr.js";import"./router-C-Pd0gHD.js";import{a as j}from"./project-D3XwUmzI.js";import{T as z}from"./tables-BHu5fUrG.js";import{F as C}from"./folder-Bovb-6rt.js";(function(){try{var n=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},s=new Error().stack;s&&(n._sentryDebugIds=n._sentryDebugIds||{},n._sentryDebugIds[s]="8f2bfbd0-3a10-4655-96b4-02a1d0853e49",n._sentryDebugIdIdentifier="sentry-dbid-8f2bfbd0-3a10-4655-96b4-02a1d0853e49")}catch{}})();const q=w("cascader",()=>{const n=P(),s=g({folders:{},projects:{},tables:{}}),c=g([]),f=(e,t,a)=>({value:`__header_action_${t}_${a||"root"}`,label:m.translate(e),disabled:!1,isLeaf:!0,level:"header",class:"cascader-header-action"}),_=(e,t)=>{const r=[...e].sort((o,i)=>o.name.localeCompare(i.name)).map(o=>({value:o.id,label:o.name,isLeaf:!0,level:"table"}));return[f("i18n_breadcrumb_tables","table",t),...r]},y=(e,t,a,r)=>{const i=e.filter(l=>l.folderId===a).sort((l,d)=>l.name.localeCompare(d.name)).map(l=>{const d=s.value.tables[l.id],h=r&&!!d;return{value:l.id,label:l.name,isLeaf:!r,level:"project",children:h?_(d,l.id):void 0}});return[f("i18n_breadcrumb_projects","project",a),...i]},v=(e,t,a)=>{const r=s.value.projects[e]||[],i=[...t].sort((l,d)=>l.name.localeCompare(d.name)).map(l=>{const h=r.filter(O=>O.folderId===l.id).length>0;return{value:l.id,label:l.name,isLeaf:!1,level:"folder",children:h?y(r,e,l.id,a):[f("i18n_breadcrumb_projects","project",l.id)]}});return[f("i18n_breadcrumb_folders","folder",e),...i]},p=(e=!1)=>{const a=[...n.organizations].sort((r,o)=>r.name.localeCompare(o.name)).map(r=>{const o=s.value.folders[r.id],i=!!o;return{value:r.id,label:r.name,isLeaf:!1,level:"org",children:i?v(r.id,o,e):void 0}});return[f("i18n_breadcrumb_organizations","organization"),...a]},u=async e=>{if(s.value.folders[e])return;const t=new C,[a,r]=await Promise.all([t.list(e),j.list(e)]);s.value.folders[e]=a,s.value.projects[e]=r},b=async e=>{if(s.value.tables[e])return;const t=await z.list(e);s.value.tables[e]=t};return{orgStore:n,cache:s,cascaderOptions:c,loadData:async(e,t)=>{const a=e[e.length-1];if(a.level==="org"){const r=a.value;if(s.value.folders[r])return;a.loading=!0;try{await u(r),a.children=v(r,s.value.folders[r],t)}catch(o){console.error("Failed to load folders for org",r,o),a.children=[{value:"__error_loading",label:m.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"&&t){const r=a.value;if(s.value.tables[r])return;a.loading=!0;try{await b(r),a.children=_(s.value.tables[r],r)}catch(o){console.error("Failed to load tables for project",r,o),a.children=[{value:"__error_loading",label:m.translate("i18n_breadcrumb_load_error"),isLeaf:!0,disabled:!0}]}finally{a.loading=!1}}c.value=[...c.value]},initialize:async(e,t,a=!1)=>{n.organizations.length===0&&await n.fetchOrganizations(),e&&await u(e),t&&a&&await b(t),c.value=p(a)},initializeFromProjectId:async(e,t=!1,a,r)=>{try{n.organizations.length===0&&await n.fetchOrganizations();let o,i;if(a&&r)o=a,i=r;else{const l=await j.get(e);o=l.organizationId,i=l.folderId}return await u(o),t&&await b(e),c.value=p(t),{organizationId:o,folderId:i}}catch(o){return console.error("Failed to initialize cascader from projectId",e,o),null}},refreshOptions:(e=!1)=>{c.value=p(e)},invalidateCache:(e,t)=>{t?delete s.value[e][t]:s.value[e]={}},loadOrgData:u,loadProjectTables:b,parseHeaderAction:e=>{if(!e.startsWith("__header_action_"))return null;const a=e.replace("__header_action_","").split("_"),r=a[0],o=a.slice(1).join("_")||"root";return{type:r,parentId:o}},findProjectInCache:e=>{for(const[t,a]of Object.entries(s.value.projects)){const r=a.find(o=>o.id===e);if(r)return{organizationId:t,folderId:r.folderId}}return null}}});export{q as u};
//# sourceMappingURL=cascaderStore-CChdaozc.js.map
