import{o as e}from"./rolldown-runtime-iuO8RvI4.js";import{Ky as s,Mb as t,jb as a}from"./shared-0-C4kdVF7Y.js";import{Mi as l,ki as r}from"./vendor-frame-CwiylG4Y.js";import{Qf as i,Rf as n,kg as o}from"./shared-1-3-6x7RKF.js";import{nn as c,rn as d}from"./vendor-all-2-BJdkbLSj.js";import{Ln as m,Qt as x,on as u}from"./vendor-all-6-BsAVZMtu.js";import{Qc as f}from"./shared-4-6CEyRAHb.js";var h=e(l(),1),p=new Map([["regular",h.createElement(h.Fragment,null,h.createElement("path",{d:"M219.31,108.68l-80-80a16,16,0,0,0-22.62,0l-80,80A15.87,15.87,0,0,0,32,120v96a8,8,0,0,0,8,8h64a8,8,0,0,0,8-8V160h32v56a8,8,0,0,0,8,8h64a8,8,0,0,0,8-8V120A15.87,15.87,0,0,0,219.31,108.68ZM208,208H160V152a8,8,0,0,0-8-8H104a8,8,0,0,0-8,8v56H48V120l80-80,80,80Z"}))]]),j=h.forwardRef((e,s)=>h.createElement(m,{ref:s,...e,weights:p}));j.displayName="HouseIcon";var g=j,b=r();function v({isOpen:e,onClose:l,onSelect:r,sshConfig:m,initialPath:p}){const j=d(),[v,y]=(0,h.useState)(p||"~"),[N,S]=(0,h.useState)([]),[C,k]=(0,h.useState)(!1),[M,w]=(0,h.useState)(null),[H,z]=(0,h.useState)(""),E=(0,h.useCallback)(async e=>{if(s?.listSSHDirectory){k(!0),w(null);try{const t=await s.listSSHDirectory(m,e);if(t.error)w(t.error),S([]);else{S(t.entries.filter(e=>e.isDirectory));const s=t.resolvedPath??e;y(s),z(s)}}catch(l){t.error(a.LOCAL_SESSION,"FolderBrowserModal: listSSHDirectory threw",l),w(j.formatMessage({defaultMessage:"Failed to connect to SSH server. Check your connection and try again.",id:"uE+N+pZ+Ca"})),S([])}finally{k(!1)}}else w(j.formatMessage({defaultMessage:"SSH browsing requires the desktop app.",id:"KztcpqBs9Q"}))},[m,j]);(0,h.useEffect)(()=>{e&&E(p||"~")},[e,p,E]);const F=(0,h.useCallback)(e=>{E(e)},[E]),B=(0,h.useCallback)(()=>{if("/"===v||"~"===v)return;const e=v.split("/").slice(0,-1).join("/")||"/";E(e)},[v,E]),D=(0,h.useCallback)(()=>{E("~")},[E]),O=(0,h.useCallback)(()=>{H.trim()&&E(H.trim())},[H,E]),V=(0,h.useCallback)(()=>{r(v),l()},[v,r,l]),q=(0,h.useMemo)(()=>"~"===v?["~"]:v.startsWith("~")?["~",...v.slice(2).split("/").filter(Boolean)]:v.split("/").filter(Boolean),[v]);return(0,b.jsx)(n,{isOpen:e,onClose:l,modalSize:"lg",hasCloseButton:!0,title:j.formatMessage({defaultMessage:"Select Remote Folder",id:"1+B6FLaGe2"}),children:(0,b.jsxs)("div",{className:"flex flex-col gap-3 p-4",children:[(0,b.jsxs)("div",{className:"flex gap-2",children:[(0,b.jsx)(f,{type:"text",value:H,onChange:e=>z(e.target.value),onKeyDown:e=>{"Enter"===e.key&&O()},placeholder:"/path/to/folder",className:"flex-1",disabled:C}),(0,b.jsx)(i,{variant:"secondary",onClick:O,disabled:C||!H.trim(),children:(0,b.jsx)(c,{defaultMessage:"Go",id:"nSwpYgWqUX"})})]}),(0,b.jsxs)("div",{className:"flex items-center gap-1 text-sm text-text-300 overflow-x-auto",children:[(0,b.jsx)("button",{type:"button",onClick:D,className:"p-1 hover:bg-bg-200 rounded",disabled:C,"aria-label":j.formatMessage({defaultMessage:"Go to home directory",id:"g1Dy492V3e"}),children:(0,b.jsx)(g,{size:16})}),q.map((e,s)=>(0,b.jsxs)("div",{className:"flex items-center",children:[s>0&&(0,b.jsx)(u,{size:12,className:"mx-1"}),(0,b.jsx)("button",{type:"button",onClick:()=>{if("~"===e)F("~");else{const e=q.slice(0,s+1).join("/"),t=v.startsWith("/")?`/${e}`:e;F(t)}},className:"px-1 py-0.5 hover:bg-bg-200 rounded truncate max-w-32",disabled:C,children:e})]},s))]}),(0,b.jsx)("div",{className:"border border-border-300 rounded-lg h-64 overflow-y-auto",children:C?(0,b.jsx)("div",{className:"flex items-center justify-center h-full",children:(0,b.jsx)(x,{size:24,className:"animate-spin"})}):M?(0,b.jsx)("div",{className:"flex items-center justify-center h-full text-danger-100 text-sm p-4 text-center",children:M}):0===N.length?(0,b.jsx)("div",{className:"flex items-center justify-center h-full text-text-400 text-sm",children:(0,b.jsx)(c,{defaultMessage:"No subdirectories",id:"tVqP+rhco5"})}):(0,b.jsxs)("div",{className:"p-1",children:["/"!==v&&"~"!==v&&(0,b.jsxs)("button",{type:"button",onClick:B,className:"w-full flex items-center gap-2 px-2 py-1.5 text-sm text-text-300 hover:bg-bg-200 rounded",children:[(0,b.jsx)(o,{size:16}),(0,b.jsx)("span",{children:".."})]}),N.map(e=>(0,b.jsxs)("button",{type:"button",onClick:()=>F(e.path),className:"w-full flex items-center gap-2 px-2 py-1.5 text-sm text-text-200 hover:bg-bg-200 rounded",children:[(0,b.jsx)(o,{size:16,className:"text-text-400"}),(0,b.jsx)("span",{className:"truncate",children:e.name})]},e.path))]})}),(0,b.jsxs)("div",{className:"flex justify-end gap-2 pt-2",children:[(0,b.jsx)(i,{variant:"secondary",onClick:l,children:(0,b.jsx)(c,{defaultMessage:"Cancel",id:"47FYwba+bI"})}),(0,b.jsx)(i,{variant:"primary",onClick:V,disabled:C,children:(0,b.jsx)(c,{defaultMessage:"Select Folder",id:"JUdxvKt2OO"})})]})]})})}export{v as FolderBrowserModal};