import{d as e,m as t,s as n,t as r,y as i}from"./hooks.module-5xe-uC6c.js";import{F as a,P as o,jt as s}from"./content_renderer-CoENEE63.js";import{t as c}from"./jsxRuntime.module--xb8gfv5.js";import{D as l,E as u,Q as d,U as f}from"./hooks-CSTgmcXO.js";import{t as p}from"./Icon-rHGhZ_GT.js";import{t as m}from"./NoteAutocomplete-CVTb6kKc.js";e();async function h(e,t,n,r=-1,i=0){let a=[],o=e.getChildBranches(),s=!1,c=o.length;e.type===`search`&&(r=0);for(let e of o){let o=await e.getNote();if(!o||!n&&o.isArchived)continue;let l={},u={};for(let{name:e,type:n}of t)n===`relation`?u[e]=o.getRelationValue(e):l[e]=o.getLabelValue(e);let d={iconClass:o.getIcon(),noteId:o.noteId,title:o.title,labels:l,relations:u,isArchived:o.isArchived,branchId:e.branchId,colorClass:o.getColorClass()};if(o.hasChildren()&&(r<0||i<r)){let{definitions:e,rowNumber:a}=await h(o,t,n,r,i+1);d._children=e,s=!0,c+=a}a.push(d)}return{definitions:a,hasSubtree:s,rowNumber:c}}function g(e){let t=[],n=e.getAttributes().filter(e=>e.isDefinition());for(let e of n){let n=e.getDefinition();if(n.multiplicity!==`single`){console.warn(`Multiple values are not supported for now`);continue}let[r,i]=s(e.name);if(e.type!==`label`){console.warn(`Relations are not supported for now`);continue}let a=n.labelType||`text`;r===`relation`&&(a=`relation`),t.push({name:i,title:n.promotedAlias,type:a})}return t}var _={text:{editor:`input`},textarea:{editor:`textarea`,formatter:`textarea`,editorParams:{shiftEnterSubmit:!0}},boolean:{formatter:`tickCross`,editor:`tickCross`},date:{editor:`date`},datetime:{editor:`datetime`},number:{editor:`number`,sorter:`number`},time:{editor:`input`},url:{formatter:`link`,editor:`input`},color:{editor:`input`,formatter:`color`,editorParams:{elementAttributes:{type:`color`}}},relation:{editor:S(w),formatter:x(C)}};function v({info:e,movableRows:t,existingColumnData:n,rowNumberHint:r,position:a}){let o=[{title:`#`,headerSort:!1,hozAlign:`center`,resizable:!1,frozen:!0,rowHandle:t,width:b(r,t),formatter:x(({cell:e,formatterParams:t})=>c(`div`,{children:[t.movableRows&&c(i,{children:[c(`span`,{class:`bx bx-dots-vertical-rounded`}),` `]}),e.getRow().getPosition(!0)]})),formatterParams:{movableRows:t}},{field:`noteId`,title:`Note ID`,formatter:x(({cell:e})=>c(`code`,{children:e.getValue()})),visible:!1},{field:`title`,title:`Title`,editor:`input`,formatter:x(({cell:e})=>{let{noteId:t,iconClass:n,colorClass:r}=e.getRow().getData();return c(`span`,{className:`reference-link ${r}`,"data-href":`#root/${t}`,children:[c(p,{icon:n}),` `,e.getValue()]})}),width:400}],s=new Set;for(let{name:t,title:n,type:r}of e){let e=`${r===`relation`?`relations`:`labels`}.${t}`;s.has(e)||(o.push({field:e,title:n??t,editor:`input`,rowHandle:!1,..._[r??`text`]}),s.add(e))}return n&&(o=y(o,n,a)),o}function y(e,t,n){let r=new Map(e.map(e=>[e.field,e])),i=t.filter(e=>e.field&&r.has(e.field)||e.title===`#`).map(e=>{let t=r.get(e.field);return e.resizable!==!1&&e.width!==void 0&&(t.width=e.width),e.visible!==void 0&&(t.visible=e.visible),t}),a=new Set(i.map(e=>e.field)),o=e.filter(e=>!a.has(e.field)),s=n===void 0?i.length:Math.min(Math.max(n,0),i.length);return[...i.slice(0,s),...o,...i.slice(s)]}function b(e,t){let n=16*(e.toString().length||1);return t&&(n+=32),n}function x(e){return(t,n,r)=>o(null,c(e,{cell:t,formatterParams:n}))[0]}function S(e){return(t,n,r,i,a)=>o(null,c(e,{cell:t,success:r,cancel:i,editorParams:a}))[0]}function C({cell:e}){let r=e.getValue(),[o,s]=n(r?a.getNoteFromCache(r):null);return t(()=>{!r||o?.noteId===r||a.getNote(r).then(s)},[r]),c(`span`,{className:`reference-link ${o?.getColorClass()}`,"data-href":`#root/${r}`,children:o&&c(i,{children:[c(p,{icon:o?.getIcon()}),` `,o.title]})})}function w({cell:e,success:n}){let i=r(null);return t(()=>i.current?.focus()),c(m,{inputRef:i,noteId:e.getValue(),opts:{allowCreatingNotes:!0,hideAllButtons:!0},noteIdChanged:n})}e();function T(e,r,i,o,s){let[c]=l(e,`maxNestingDepth`),[p]=u(e,`includeArchived`),[m,_]=n(),[y,b]=n(),[x,S]=n(),[C]=u(e,`sorted`),[w,T]=n(!1);async function E(){let t=g(e);await a.getNotes(r);let{definitions:n,hasSubtree:l,rowNumber:u}=await h(e,t,p,c);_(v({info:t,movableRows:w,existingColumnData:i?.tableData?.columns,rowNumberHint:u,position:o?.current??void 0})),b(n),S(l),s()}return t(()=>{E()},[e,r,c,w]),f(`entitiesReloaded`,({loadResults:t})=>{if(glob.device!==`print`){if(t.getAttributeRows().find(t=>t.type===`label`&&(t.name?.startsWith(`label:`)||t.name?.startsWith(`relation:`))&&d.isAffecting(t,e))){E();return}if(t.getBranchRows().some(t=>t.parentNoteId===e.noteId||r.includes(t.parentNoteId??``))||t.getNoteIds().some(e=>r.includes(e))||t.getAttributeRows().some(e=>r.includes(e.noteId))||t.getAttributeRows().some(e=>e.name===`archived`&&e.noteId&&r.includes(e.noteId))){E();return}}}),t(()=>{T(!C&&e.type!==`search`&&!x)},[C,e,x]),{columnDefs:m,rowData:y,movableRows:w,hasChildren:x}}export{T as t};