import{u as w,a,Z as S,$ as _,j as t}from"./index-BtK3YhJc.js";function F({input:i,disabled:h=!1,onCandidates:u,onError:l}){const{t:s}=w(),[f,g]=a.useState([]),[o,p]=a.useState(new Set),[d,n]=a.useState(!1),[x,c]=a.useState([]),y=a.useCallback(async()=>{if(i.trim()){n(!0),c([]),l(null);try{const e=await S({source:{kind:"github-folder",input:i.trim()}});g(e.files),p(new Set(e.files.slice(0,5).map(r=>r.path))),c(e.warnings),e.files.length===0&&l(s("promptLibrary.folderFilesEmpty"))}catch(e){g([]),p(new Set),l(e instanceof Error?e.message:s("promptLibrary.folderUnsupported"))}finally{n(!1)}}},[i,l,s]),b=a.useCallback(async()=>{const e=[...o];if(e.length===0){l(s("promptLibrary.folderNoSelection"));return}n(!0),c([]),l(null);try{const r=await _({source:{kind:"github-folder",input:i.trim()},paths:e});c(r.warnings),u(r.candidates)}catch(r){l(r instanceof Error?r.message:s("promptLibrary.importFailed"))}finally{n(!1)}},[i,u,l,o,s]);return t.jsxs("div",{className:"prompt-import-dialog__folder",children:[t.jsxs("div",{className:"prompt-import-dialog__section-title",children:[t.jsx("strong",{children:s("promptLibrary.folderFiles")}),t.jsx("span",{children:s("promptLibrary.folderBrowseHint")})]}),t.jsxs("div",{className:"prompt-import-dialog__folder-actions",children:[t.jsx("button",{type:"button",onClick:()=>{y()},disabled:h||d||!i.trim(),children:s(d?"common.loading":"promptLibrary.folderBrowse")}),t.jsx("button",{type:"button",onClick:()=>{b()},disabled:h||d||o.size===0,children:s("promptLibrary.folderPreviewSelected")}),t.jsx("span",{children:s("promptLibrary.folderSelectedCount",{count:o.size})})]}),f.length>0?t.jsx("div",{className:"prompt-import-dialog__folder-list",children:f.map(e=>t.jsxs("label",{className:"prompt-import-dialog__folder-file",children:[t.jsx("input",{type:"checkbox",checked:o.has(e.path),onChange:r=>{p(j=>{const m=new Set(j);return r.target.checked?m.add(e.path):m.delete(e.path),m})}}),t.jsxs("span",{children:[t.jsx("strong",{children:e.name}),t.jsx("small",{children:e.path})]}),t.jsxs("em",{children:[Math.ceil(e.sizeBytes/1024)," KB"]})]},e.path))}):null,x.length>0?t.jsx("div",{className:"prompt-import-dialog__folder-warning",children:x.slice(0,3).join(" · ")}):null]})}export{F as PromptImportFolderSection};
