import{X as e,bt as t,ht as n,vt as r}from"./xY0c-UfG.js";import{Y as i}from"./BW-sf0N4.js";import{i as a,r as o}from"./Ca8AGdcM.js";import{t as s}from"./vgzQgl7V.js";function c(e,t){let n=encodeURIComponent(e);return t?`/workspace/folder/${n}/${t.split(`/`).map(encodeURIComponent).join(`/`)}`:`/workspace/folder/${n}`}function l(e,t){if(!t)return e;let n=t.split(`/`),r=e;for(let e of n){let t=r.find(t=>t.name===e);if(!t)return[];r=t.children}return r}var u=/^\[\[(.+)\]\]$/,d=`user:`;function f(e){if(typeof e!=`string`)return null;let t=e.trim();if(!t)return null;let n=t.match(u);n&&(t=n[1].trim());let r,i=t.indexOf(`|`);if(i>=0&&(r=t.slice(i+1).trim()||void 0,t=t.slice(0,i).trim()),!t)return null;if(t.startsWith(d)){let e=t.slice(5).trim();return e?{kind:`user`,userId:e,handle:(r??``).replace(/^@/,``).trim()||void 0,label:r}:null}let a;if(t.includes(`/`)){let e=t.split(`/`);t=(e.pop()??``).trim();let n=e.join(`/`).trim();n&&(a=n)}return t?{kind:`entity`,id:t,repo:a,label:r}:null}function p(e,t){let n=String(e).trim(),r=(t??``).replace(/^@/,``).trim();return r?`[[user:${n}|@${r}]]`:`[[user:${n}]]`}var m=t(n([])),h=t(n({})),g=t(n({}));async function _(){try{let{users:e}=await i(`/api/auth/users`);r(m,e,!0);let t={},n={};for(let r of e)t[r.handle]=r,n[String(r.id)]=r;r(h,t,!0),r(g,n,!0)}catch{}}function v(t){return t?e(h)[t]??null:null}function y(t){let n=String(t).trim();return n?e(g)[n]??null:null}function b(e){let t=f(e);if(t?.kind!==`user`)return null;let n=y(t.userId);return n?n.displayName:t.handle?`@${t.handle}`:t.userId}_(),s.addConnectHandler(()=>_());var x={getByHandle:v,getById:y,resolveUserRefName:b,load:_,get users(){return e(m)}};function S(e){let t=e.replace(/^\[\[|\]\]$/g,``).split(`|`)[0].trim();if(!t)return null;for(let e of a.repos){let n=e.entities.find(e=>e.id===t);if(n)return{id:n.id,title:n.title,entityType:n.entityType,repo:e.name,url:`/${e.name}/${n.entityType}/${n.id}`}}let n=t.indexOf(`/`);if(n>0){let e=t.slice(0,n),r=t.slice(n+1),i=a.repos.find(t=>t.name===e);if(i&&r){let n=i.subfolders,a;for(let e of r.split(`/`)){if(a=n.find(t=>t.name===e),!a)break;n=a.children}if(a)return{id:t,title:a.displayName??o(a.name),entityType:`folder`,repo:e,url:c(e,r)}}}return null}function C(e){if(!e)return[];let t=e.replace(/\[\[|\]\]/g,``).split(`,`).map(e=>e.trim()).filter(Boolean),n=[];for(let e of t){let t=S(e);t&&n.push(t)}return n}function w(e){let t=e.replace(/^\[\[|\]\]$/g,``),n=t.split(`|`)[0].trim();if(n.startsWith(`user:`))return x.resolveUserRefName(t)??n.slice(5);let r=t.indexOf(`|`);if(r!==-1){let e=t.slice(r+1).trim();if(e)return e}return n.replace(/-/g,` `).replace(/\b\w/g,e=>e.toUpperCase())}function T(e){let t=[],n=/\[\[([^\]]+)\]\]/g,r=0,i;for(;i=n.exec(e);){i.index>r&&t.push({type:`text`,value:e.slice(r,i.index)});let n=S(i[1]);t.push(n?{type:`ref`,value:n.title,id:n.id,url:n.url}:{type:`ref`,value:w(i[1]),id:i[1].split(`|`)[0].trim(),url:null}),r=i.index+i[0].length}return r<e.length&&t.push({type:`text`,value:e.slice(r)}),t}var E=/[\s([\-—*_~]/;function D(e){return!e||E.test(e)}function O(e,t){let n=D(t);return e===`"`?n?`“`:`”`:n?`‘`:`’`}function k(e,t=``){if(!e)return e;let n=``;for(let r=0;r<e.length;r++){let i=e[r];if(i===`"`||i===`'`){let e=n.length?n[n.length-1]:t;n+=O(i,e)}else n+=i}return n}var A=`\xA0`;function j(e){return e&&e.replace(/([‘“])[ \t]/g,`$1${A}`).replace(/[ \t]([’”])/g,`${A}$1`)}function M(e){if(!e)return e;let t=``;for(let n=0;n<e.length;n++){let r=e[n];if(r===`"`||r===`'`){t+=O(r,t[t.length-1]??``);continue}if(r===`-`&&e[n+1]===`-`){let r=e[n+2]??``;if(r!==`-`&&!/[A-Za-z0-9_]/.test(r)){t+=`—`,n++;continue}}t+=r}return t}export{w as a,T as c,f as d,l as f,k as i,x as l,j as n,S as o,c as p,D as r,C as s,M as t,p as u};