import{React as r,jsxRuntimeExports as t}from"./vendor-react.js";import{toast as d,LoaderCircle as y,RefreshCw as T,FileArchive as U,Download as z}from"./vendor-common.js";import{apiService as w}from"./excalidrawUiMode.js";import{PromptActionButton as $}from"../index.js";import{Dialog as B,DialogContent as S,DialogHeader as H,DialogTitle as K,DialogDescription as O,Button as p,DialogFooter as q}from"./dialog.js";import"./_commonjsHelpers.js";import"./spec-template-vendor.js";import"./vendor-antd.js";import"./preload-helper.js";import"./_commonjs-dynamic-modules.js";import"./AppDialogProvider.js";import"./vendor-assistant.js";import"./vendor-export.js";import"./vendor-genie.js";function F(i){if(!i)return"尚未导出";const n=new Date(i);return Number.isNaN(n.getTime())?i:new Intl.DateTimeFormat("zh-CN",{year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit"}).format(n)}function re({open:i,onOpenChange:n,itemName:f,targetPath:b,ideTargetPath:M,preferredPromptClient:R,preferredIDE:C,onDownloadSuccess:m,onDownloadFailure:o}){const a=String(b||"").trim(),P=String(M||"").trim(),[e,x]=r.useState(null),[g,u]=r.useState(!1),[j,N]=r.useState(!1),l=r.useCallback(async()=>{if(!a){x(null);return}u(!0);try{const s=await w.probeExportMake(a);x(s)}catch(s){x(null),d.error((s==null?void 0:s.message)||"加载导出状态失败")}finally{u(!1)}},[a]);r.useEffect(()=>{i&&l()},[l,i]);const A=r.useCallback(async()=>{if(!a){d.warning("请先选择一个原型页面");return}N(!0);try{const s=await fetch(`/api/export-make?path=${encodeURIComponent(a)}`);if(!s.ok){const h=await s.json().catch(()=>({}));throw new Error((h==null?void 0:h.error)||"下载 .fig 失败")}const I=await s.blob(),k=(e==null?void 0:e.fileName)||`${f||"project"}.fig`,v=URL.createObjectURL(I),c=document.createElement("a");c.href=v,c.download=k,document.body.appendChild(c),c.click(),c.remove(),URL.revokeObjectURL(v),d.success(".fig 文件已开始下载"),m==null||m(k),await l()}catch(s){o==null||o(s),d.error((s==null?void 0:s.message)||"下载 .fig 失败")}finally{N(!1)}},[f,l,o,m,e==null?void 0:e.fileName,a]),E=!!(e!=null&&e.hasMakeAssets)&&!(e!=null&&e.hasDriftRisk)&&!g&&!j,L=(e==null?void 0:e.fileName)||`${f||"project"}.fig`,D=F((e==null?void 0:e.lastExportedAt)||null);return t.jsx(B,{open:i,onOpenChange:n,children:t.jsxs(S,{className:"sm:max-w-[560px]",children:[t.jsxs(H,{className:"gap-1.5",children:[t.jsx(K,{className:"text-[18px] font-semibold tracking-tight",children:"导出 Figma.Make"}),t.jsx(O,{className:"text-sm leading-6",children:"使用 AI 导出或更新 .fig 文件"})]}),t.jsxs("div",{className:"space-y-4",children:[t.jsxs("div",{className:"flex items-center justify-between",children:[t.jsx("div",{className:"text-sm font-medium",children:"MAKE 文件"}),t.jsxs(p,{type:"button",variant:"ghost",size:"sm",className:"h-8 gap-1.5 px-2 text-xs",onClick:()=>void l(),disabled:g,children:[g?t.jsx(y,{className:"h-3.5 w-3.5 animate-spin"}):t.jsx(T,{className:"h-3.5 w-3.5"}),"刷新状态"]})]}),t.jsx("div",{className:"rounded-xl border bg-card p-4 shadow-sm",children:t.jsxs("div",{className:"flex items-center justify-between gap-4",children:[t.jsxs("div",{className:"flex min-w-0 items-center gap-3",children:[t.jsx("div",{className:`rounded-lg p-2 ${e!=null&&e.hasMakeAssets?"bg-emerald-50 text-emerald-600":"bg-amber-50 text-amber-600"}`,children:t.jsx(U,{className:"h-5 w-5"})}),t.jsxs("div",{className:"min-w-0 space-y-1",children:[t.jsx("div",{className:"truncate text-sm font-medium",children:e!=null&&e.hasMakeAssets?L:"尚未生成"}),t.jsxs("div",{className:"text-xs text-muted-foreground",children:["最后更新：",D]})]})]}),t.jsxs(p,{type:"button",variant:"brand",size:"sm",className:"shrink-0 self-center gap-1.5",onClick:()=>void A(),disabled:!E,children:[j?t.jsx(y,{className:"h-4 w-4 animate-spin"}):t.jsx(z,{className:"h-4 w-4"}),"下载 Make"]})]})}),e!=null&&e.hasDriftRisk&&e.driftReasons.length>0?t.jsx("div",{className:"rounded-lg border border-dashed px-3 py-2.5 text-xs text-muted-foreground",children:e.driftReasons.map(s=>t.jsxs("div",{children:["- ",s]},s))}):null]}),t.jsxs(q,{className:"gap-2 sm:justify-end",children:[t.jsx($,{type:"default",preferredClient:R,preferredIDE:C,scene:"export-figma-make",buildPrompt:async()=>{if(!a)throw new Error("请先选择一个原型页面");return(await w.getExportMakePrompt(a)).prompt},getIdeTargetPath:()=>P||null,copySuccessMessage:"导出 Prompt 已复制，请发送给 AI 继续生成或更新 .fig 文件",executeSuccessMessage:"已打开新会话",fallbackMessage:"自动执行失败，已回退为复制导出 Prompt",copyLabel:"复制导出 Prompt",disabled:!a}),t.jsx(p,{type:"button",variant:"outline",size:"sm",onClick:()=>n(!1),children:"关闭"})]})]})})}export{re as default};
