import{reactExports as i,jsxRuntimeExports as t}from"./vendor-react.js";import{History as J,GitCommitHorizontal as K,Eye as L,RotateCcw as W,toast as n}from"./vendor-common.js";import{PromptActionButton as Y}from"../index.js";import{Dialog as _,DialogContent as q,DialogHeader as Q,DialogTitle as X,DialogDescription as Z,Button as x}from"./dialog.js";import{useAppDialog as ee,Input as te}from"./AppDialogProvider.js";import"./_commonjsHelpers.js";import"./spec-template-vendor.js";import"./vendor-antd.js";import"./preload-helper.js";import"./_commonjs-dynamic-modules.js";import"./excalidrawUiMode.js";import"./vendor-assistant.js";import"./vendor-export.js";import"./vendor-genie.js";function re(l){if(!l||typeof l!="object")return"";const c=l.errorCode;return typeof c=="string"?c.trim():""}function $(l){const c=re(l);return c==="git-repository-not-initialized"?{title:"当前项目未启用 Git 版本管理",description:"请先在项目根目录执行 git init，并至少提交一次版本后再查看历史版本。"}:c==="git-not-available"?{title:"当前环境未安装 Git",description:"安装 Git 并重启开发服务器后，才能使用版本管理功能。"}:c==="git-history-not-ready"?{title:"当前项目还没有版本历史",description:"当前项目已启用 Git，但还没有任何提交记录。请先提交一次版本后再查看历史版本。"}:null}function ge({visible:l,onCancel:c,item:a,activeTab:D,preferredPromptClient:T,preferredIDE:U,ideAvailability:G}){const E=ee(),[g,y]=i.useState([]),[u,j]=i.useState(!1),[b,v]=i.useState(""),[m,w]=i.useState(""),[N,S]=i.useState(!1),[I,C]=i.useState(!1),[O,P]=i.useState(null),[p,h]=i.useState(null),z=r=>{let s=String(r||"").trim().replace(/\\/g,"/");const e=s.lastIndexOf("/src/");return e>=0?s=s.substring(e+5):s.startsWith("src/")&&(s=s.substring(4)),s.replace(/^\/+/,"").replace(/\/index\.(t|j)sx?$/i,"").replace(/\/+$/,"")},d=r=>{const s=String(r.filePath||r.absoluteFilePath||"").trim();return s?z(s):""},A=r=>{const s=new Date(r),e=o=>String(o).padStart(2,"0");return`${s.getFullYear()}/${e(s.getMonth()+1)}/${e(s.getDate())} ${e(s.getHours())}:${e(s.getMinutes())}:${e(s.getSeconds())}`},F=r=>{const s=Math.floor((Date.now()-r)/1e3);let e=s/31536e3;return e>1?Math.floor(e)+" 年前":(e=s/2592e3,e>1?Math.floor(e)+" 个月前":(e=s/86400,e>1?Math.floor(e)+" 天前":(e=s/3600,e>1?Math.floor(e)+" 小时前":(e=s/60,e>1?Math.floor(e)+" 分钟前":"刚刚"))))},f=async()=>{if(a){C(!0),h(null);try{const r=d(a);if(!r){n.error("无法获取文件路径");return}const s=await fetch(`/api/git/history?path=${encodeURIComponent(r)}`),e=await s.json();if(s.ok)h($(e)),y(Array.isArray(e.commits)?e.commits:[]),j(!!e.hasUncommitted),v(typeof e.uncommittedFiles=="string"?e.uncommittedFiles:"");else{const o=$(e);o?(h(o),y([]),j(!1),v("")):n.error(e.error||"加载版本历史失败")}}catch{n.error("加载版本历史失败")}finally{C(!1)}}};i.useEffect(()=>{l&&a&&f()},[l,a]);const H=async r=>{if(!(!a||!await E.confirm({title:"恢复此版本？",description:"当前未提交的更改将会丢失，请确认是否继续。",confirmText:"确认恢复",cancelText:"取消",tone:"destructive",dismissible:!1})))try{const e=d(a);if(!e){n.error("无法获取文件路径");return}const o=await fetch("/api/git/restore",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({path:e,commitHash:r})}),R=await o.json();o.ok?(n.success("版本恢复成功"),f()):n.error(R.error||"版本恢复失败")}catch{n.error("版本恢复失败")}},k=async()=>{if(!a||!m.trim()){n.warning("请输入提交信息");return}S(!0);try{const r=d(a);if(!r){n.error("无法获取文件路径");return}const s=await fetch("/api/git/commit",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({path:r,message:m})}),e=await s.json();s.ok?(n.success("提交成功"),w(""),f()):n.error(e.error||"提交失败")}catch{n.error("提交失败")}finally{S(!1)}},V=async()=>{if(!a)throw new Error("请选择条目后再执行");const r=d(a);if(!r)throw new Error("当前资源未声明本地文件路径，无法生成提交指令");return`我需要提交以下目录的更改，请帮我处理：

目标路径: ${r}

请执行以下步骤：
1. 使用 git status 和 git diff 查看该路径下的变更内容。
2. 分析变更，生成一个简洁明了的中文提交信息（Commit Message）。
3. 向我展示变更摘要和建议的提交信息，并询问是否继续。
4. 获得确认后，执行：
   git add ${r}
   git commit -m "你的提交信息"

请直接开始执行第 1 步。`},B=async r=>{if(a){P(r);try{const s=d(a);if(!s){n.error("无法获取文件路径");return}const e=await fetch("/api/git/build-version",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({path:s,commitHash:r})}),o=await e.json();e.ok&&o.hasPrototype&&o.prototypeUrl?window.open(o.prototypeUrl,"_blank"):o.hasPrototype?n.error(o.error||"无法访问原型"):n.warning("该版本没有原型文件")}catch{n.error("加载原型失败")}finally{P(null)}}},M=i.useMemo(()=>b.split(`
`).filter(Boolean).length,[b]);return t.jsx(_,{open:l,onOpenChange:r=>!r&&c(),children:t.jsxs(q,{className:"max-h-[80vh] max-w-[760px] overflow-y-auto text-sm",children:[t.jsxs(Q,{children:[t.jsxs(X,{className:"flex items-center gap-2",children:[t.jsx(J,{className:"h-4 w-4"}),`版本管理 - ${(a==null?void 0:a.displayName)||"-"}`]}),t.jsx(Z,{children:"查看历史版本、提交未保存更改并回滚到指定版本。"})]}),I?t.jsx("div",{className:"py-8 text-center text-sm text-muted-foreground",children:"加载中..."}):t.jsxs("div",{className:"space-y-3",children:[p?t.jsxs("div",{className:"rounded-md border border-dashed p-6 text-center text-sm text-muted-foreground",children:[t.jsx("div",{className:"font-medium text-foreground",children:p.title}),t.jsx("div",{className:"mt-2",children:p.description})]}):null,u?t.jsxs("div",{className:"rounded-md border border-amber-300/60 bg-amber-50/50 p-3 dark:border-amber-700/70 dark:bg-amber-950/20",children:[t.jsxs("div",{className:"flex items-center justify-between",children:[t.jsx("div",{className:"text-sm font-medium text-foreground",children:"未提交的更改"}),t.jsx("span",{className:"rounded-full bg-amber-200 px-2 py-0.5 text-sm text-amber-900 dark:bg-amber-700 dark:text-amber-100",children:"未保存"})]}),t.jsxs("div",{className:"mt-3 space-y-3",children:[t.jsx(te,{placeholder:"手动输入提交信息...",value:m,onChange:r=>w(r.target.value),onKeyDown:r=>{r.key==="Enter"&&k()}}),t.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-x-3 gap-y-2",children:[t.jsx("div",{className:"text-sm text-muted-foreground",children:M>0?`${M} 个文件变更`:"有文件变更"}),t.jsxs("div",{className:"flex items-center gap-2",children:[t.jsx(Y,{type:"default",preferredClient:T,preferredIDE:U,ideAvailability:G,scene:`version-commit-${D}`,buildPrompt:V,getIdeTargetPath:()=>a&&d(a)||null,copySuccessMessage:"AI 指令已复制，请粘贴给助手",executeSuccessMessage:"已打开新会话",fallbackMessage:"自动执行失败，已回退为复制 Prompt"}),t.jsxs(x,{variant:"default",size:"sm",onClick:()=>void k(),disabled:N||!m.trim(),children:[t.jsx(K,{className:"h-4 w-4"}),N?"提交中...":"提交"]})]})]})]})]}):null,g.length===0?p||u?null:t.jsx("div",{className:"rounded-md border border-dashed p-6 text-center text-sm text-muted-foreground",children:"暂无版本历史"}):t.jsx("div",{className:"space-y-3",children:g.map((r,s)=>{const e=s===0&&!u;return t.jsx("div",{className:"rounded-md border bg-card px-4 py-3.5",children:t.jsxs("div",{className:"flex items-start justify-between gap-3",children:[t.jsxs("div",{className:"min-w-0",children:[t.jsx("div",{className:"truncate text-sm font-medium text-foreground",children:r.message}),t.jsxs("div",{className:"mt-1.5 flex flex-wrap items-center gap-2 text-sm text-muted-foreground",children:[t.jsx("span",{children:r.author||"Unknown"}),t.jsx("span",{children:"·"}),t.jsx("span",{title:A(r.timestamp),children:F(r.timestamp)}),t.jsx("span",{children:"·"}),t.jsx("code",{className:"rounded bg-muted px-1 py-0.5 text-sm",children:r.hash.substring(0,7)})]})]}),e?t.jsx("span",{className:"rounded-full bg-emerald-100 px-2 py-0.5 text-sm text-emerald-800 dark:bg-emerald-900/50 dark:text-emerald-100",children:"当前版本"}):t.jsxs("div",{className:"flex items-center gap-1",children:[t.jsx(x,{variant:"ghost",size:"icon-xs",onClick:()=>void B(r.hash),disabled:O===r.hash,children:t.jsx(L,{className:"h-4 w-4"})}),t.jsx(x,{variant:"ghost",size:"icon-xs",onClick:()=>void H(r.hash),children:t.jsx(W,{className:"h-4 w-4"})})]})]})},r.hash)})})]})]})})}export{ge as default};
