import{c as a,a as r,o as s,w as K,S as Y,r as h,G as ee,b as te,f as ne,Q as ae,T as se,e as g,U as oe,g as J,h as le,p as t,F,y as M,i as P,j as B,l as E,s as re,J as ie,P as ue,x as R,V as ce,W as de,X as ve,v as G,_ as fe}from"./index-BcBhCcW4.js";import{g as pe}from"./marked.esm-D9eDbhZJ.js";function we(L,T){return s(),a("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 20 20",fill:"currentColor","aria-hidden":"true","data-slot":"icon"},[r("path",{d:"M10.75 2.75a.75.75 0 0 0-1.5 0v8.614L6.295 8.235a.75.75 0 1 0-1.09 1.03l4.25 4.5a.75.75 0 0 0 1.09 0l4.25-4.5a.75.75 0 0 0-1.09-1.03l-2.955 3.129V2.75Z"}),r("path",{d:"M3.5 12.75a.75.75 0 0 0-1.5 0v2.5A2.75 2.75 0 0 0 4.75 18h10.5A2.75 2.75 0 0 0 18 15.25v-2.5a.75.75 0 0 0-1.5 0v2.5c0 .69-.56 1.25-1.25 1.25H4.75c-.69 0-1.25-.56-1.25-1.25v-2.5Z"})])}function me(){const L=ae(),T=oe(),{onMessage:v}=K,k=Y(),f=g(()=>k.fstoken),S=g(()=>k.roots),b=f;function _(e){if(!e)return e;const o=k.roots;if(!o.length)return e;const[y,...x]=e.split("/");if(o.includes(y))return e;const m=o.find(i=>i===`workspace-${y}`||i.endsWith(`-${y}`));return m?[m,...x].join("/"):e}const u=h(_(L.query.path||localStorage.getItem("viewer_last_path")||"")),H=h(window.innerWidth<768),c=h(""),w=h(""),V=h([]),U=h([]),D=h(!1),I=h(!1);let C=null;const p=h(""),$=h(Date.now()),l=h(!1),n=g(()=>c.value?c.value.split(` `).length:0),d=g(()=>w.value!=="md"||!c.value?"":pe.parse(c.value)),W=g(()=>w.value!=="pdf"||!u.value||!b.value?"":`${q()}/api/viewer/file?path=${encodeURIComponent(u.value)}&token=${encodeURIComponent(b.value)}&t=${$.value}`);function q(){return J()}function z(e){const o=e.match(/\.([^./]+)$/);return o?o[1].toLowerCase():""}function O(e){e||(D.value=!0,I.value=!1,C&&clearTimeout(C),C=setTimeout(()=>{I.value=!0},3e3))}function Z(){D.value=!1,I.value=!1,C&&(clearTimeout(C),C=null)}async function N(e,o=!1,y=!1){O(o);try{const m=`${q()}/api/viewer/file?path=${encodeURIComponent(e)}&token=${encodeURIComponent(b.value)}`,i=await fetch(m);if(i.status===401&&!y)return k.invalidate(),await k.acquire(!0),N(e,o,!0);if(!i.ok){p.value=`${i.status}: ${await i.text()}`;return}p.value="",c.value=await i.text()}catch(x){p.value=x.message||"Fetch failed"}finally{Z()}}async function A(e){e=_(e),u.value=e,localStorage.setItem("viewer_last_path",e),se.value=e,L.query.path!==e&&T.push({name:"viewer",query:{path:e}});const o=z(e);if(!e){w.value="dir",c.value="",p.value="",U.value=S.value.length?S.value:["shared","workspace-titan"],V.value=[];return}if(!o){w.value="dir",c.value="",p.value="",O(!1);try{const x=`${q()}/api/viewer/tree?root=${encodeURIComponent(e)}&token=${encodeURIComponent(b.value)}`,m=await fetch(x);if(!m.ok)throw new Error(`${m.status} ${m.statusText}`);const i=await m.json();U.value=i.dirs||[],V.value=i.files||[]}catch(y){U.value=[],V.value=[],p.value=y.message||"Failed to load directory"}finally{Z()}return}if(o==="pdf"){w.value="pdf",c.value="",p.value="",$.value=Date.now(),fetch(`${q()}/api/viewer/file?path=${encodeURIComponent(e)}&token=${encodeURIComponent(b.value)}`,{method:"HEAD"}).catch(()=>{});return}w.value=o==="md"?"md":"text",p.value="",await N(e,!1)}function Q(e){const o=window.getSelection();if(!o||o.isCollapsed)return;const x=(()=>{const i=document.createElement("div");return i.appendChild(o.getRangeAt(0).cloneContents()),i.innerHTML})().replace(/background(-color)?:[^;"]*(;|(?="))/gi,""),m=o.toString();e.clipboardData.setData("text/html",x),e.clipboardData.setData("text/plain",m),e.preventDefault()}async function X(e){if(z(e)==="pdf"){$.value=Date.now();return}await N(e,!0)}let j=null;return ee(()=>{j=v(e=>{e.type==="viewer_file_changed"&&e.path===u.value&&X(u.value),e.type==="viewer_tree_changed"&&e.path===u.value&&w.value==="dir"&&A(u.value)}),k.acquire(),u.value&&A(u.value)}),te(()=>L.query.path,e=>{e&&e!==u.value&&A(e)}),ne(()=>{j&&j()}),{fstoken:f,viewerRoots:S,currentPath:u,sidebarCollapsed:H,content:c,fileType:w,loading:D,fetchError:p,mdRaw:l,mdLineCount:n,renderedMd:d,pdfSrc:W,openFile:A,onCopy:Q,dirFiles:V,dirDirs:U,showLoading:I}}const he={class:"viewer-layout h-full overflow-hidden"},_e={class:"viewer-pane min-w-0 flex flex-col h-full overflow-hidden"},ye={key:0,class:"viewer-toolbar"},ge={class:"viewer-panel breadcrumb-panel"},ke={key:0,class:"breadcrumb-sep"},be=["onClick"],Ce={key:0,class:"viewer-panel toggle-panel"},$e=["href","download"],xe={key:0,class:"viewer-empty"},Re={key:1,class:"viewer-loading"},Ve={key:2,class:"viewer-error"},Ue={key:3,class:"viewer-dir"},De=["onClick"],Ie=["onClick"],Le={key:0,class:"viewer-empty"},Te=["src"],Se=["innerHTML"],Fe={key:6,class:"viewer-raw-md"},Me={class:"viewer-raw-code"},Be={class:"line-num"},Ee={key:7,class:"viewer-raw-code"},qe={class:"line-num"},Ae=le({name:"ViewerView",__name:"ViewerView",setup(L){const{fstoken:T,currentPath:v,content:k,fileType:f,showLoading:S,fetchError:b,mdRaw:_,renderedMd:u,pdfSrc:H,openFile:c,onCopy:w,dirFiles:V,dirDirs:U}=me(),D=g(()=>k.value.split(` `)),I=g(()=>v.value&&v.value.split("/").pop()||""),C=g(()=>!v.value||!T.value?"":`${J()}/api/viewer/file?path=${encodeURIComponent(v.value)}&token=${encodeURIComponent(T.value)}&dl=1`),p=g(()=>{const $=[{label:"files",path:""}];if(!v.value)return $;const l=v.value.split("/");for(let n=0;n(s(),a("div",he,[r("main",_e,[t(v)||t(f)==="dir"?(s(),a("div",ye,[r("div",ge,[(s(!0),a(F,null,M(p.value,(n,d)=>(s(),a("span",{key:d},[d>0?(s(),a("span",ke,"/")):B("",!0),r("span",{class:P(["breadcrumb-item",{active:d===p.value.length-1}]),onClick:W=>t(c)(n.path)},R(n.label),11,be)]))),128))]),l[3]||(l[3]=r("div",{class:"viewer-toolbar-spacer"},null,-1)),t(f)==="md"?(s(),a("div",Ce,[r("button",{class:P(["md-toggle-btn",{active:!t(_)}]),onClick:l[0]||(l[0]=n=>_.value=!1)},"Rendered",2),r("button",{class:P(["md-toggle-btn",{active:t(_)}]),onClick:l[1]||(l[1]=n=>_.value=!0)},"Raw",2)])):B("",!0),t(f)&&t(f)!=="dir"?(s(),a("a",{key:1,class:"viewer-panel download-panel",href:C.value,download:I.value,title:"Download"},[E(t(we),{class:"w-4 h-4"})],8,$e)):B("",!0)])):B("",!0),E(t(ue),{class:"content flex-1 min-h-0",options:t(ie),element:"div"},{default:re(()=>[!t(v)&&t(f)!=="dir"?(s(),a("div",xe,[E(t(ce),{class:"w-8 h-8 text-text-dim"}),l[4]||(l[4]=r("p",null,"Select a file from the tree",-1))])):t(S)?(s(),a("div",Re,"loading…")):t(b)?(s(),a("div",Ve,R(t(b)),1)):t(f)==="dir"?(s(),a("div",Ue,[(s(!0),a(F,null,M(t(U),n=>(s(),a("div",{key:n,class:"dir-entry dir-entry--dir",onClick:d=>t(c)(t(v)?t(v)+"/"+n:n)},[E(t(de),{class:"w-4 h-4"}),r("span",null,R(n)+"/",1)],8,De))),128)),(s(!0),a(F,null,M(t(V),n=>(s(),a("div",{key:n.path,class:"dir-entry dir-entry--file",onClick:d=>t(c)(n.path)},[E(t(ve),{class:"w-4 h-4"}),r("span",null,R(n.name),1)],8,Ie))),128)),!t(U).length&&!t(V).length?(s(),a("div",Le,[...l[5]||(l[5]=[r("p",null,"Empty directory",-1)])])):B("",!0)])):t(f)==="pdf"?(s(),a("iframe",{key:4,class:"viewer-pdf",src:t(H),frameborder:"0"},null,8,Te)):t(f)==="md"&&!t(_)?(s(),a("div",{key:5,class:"viewer-md md-body",innerHTML:t(u),onCopy:l[2]||(l[2]=(...n)=>t(w)&&t(w)(...n))},null,40,Se)):t(f)==="md"&&t(_)?(s(),a("div",Fe,[r("pre",Me,[r("code",null,[(s(!0),a(F,null,M(D.value,(n,d)=>(s(),a("span",{key:d,class:"code-line"},[r("span",Be,R(d+1),1),G(R(n)+` `,1)]))),128))])])])):(s(),a("pre",Ee,[r("code",null,[(s(!0),a(F,null,M(D.value,(n,d)=>(s(),a("span",{key:d,class:"code-line"},[r("span",qe,R(d+1),1),G(R(n)+` `,1)]))),128))])]))]),_:1},8,["options"])])]))}}),je=fe(Ae,[["__scopeId","data-v-6b5cb038"]]);export{je as default};