import{j as e}from"./vendor-codemirror-D5amxuPd.js";import{r as s,f as G,h as H}from"./vendor-ui-BLV2MnSs.js";import{M as K,S as n,I as R,B as m}from"./index-BeLXjJhD.js";import"./vendor-ag-grid-NsbaVf01.js";function te({open:F,onClose:d,sessionId:j,database:h,table:o,sql:a,pkValues:l}){const[i,Q]=s.useState(a?"csv":"insert"),[g,$]=s.useState("500"),[x,f]=s.useState(!1),[v,C]=s.useState(!1),[_,b]=s.useState(!1),[u,k]=s.useState(!1),[r,B]=s.useState(","),[E,M]=s.useState(""),[S,O]=s.useState('"'),[w,U]=s.useState('"'),[y,z]=s.useState("crlf"),[L,I]=s.useState("utf-8"),A={crlf:`\r
`,lf:`
`,cr:"\r"}[y]||`\r
`,P=r==="custom"?E:r,N=()=>JSON.stringify({database:h,...a?{sql:a}:{table:o},format:i,batch_size:parseInt(g),...l?{pk_values:l}:{},...i==="csv"?{csv_delimiter:P,csv_quotechar:S,csv_escapechar:w,csv_lineterminator:A,csv_encoding:L}:{}}),J=async()=>{f(!0);try{const t=await fetch(`/api/v1/sessions/${j}/export`,{method:"POST",headers:{"Content-Type":"application/json"},body:N()});if(!t.ok)throw new Error(await t.text());const T=await t.blob(),c=URL.createObjectURL(T),p=document.createElement("a"),D=(t.headers.get("Content-Disposition")??"").match(/filename="([^"]+)"/);p.download=D?D[1]:`${h}_${o}.${i==="csv"?"csv":"sql"}`,p.href=c,p.click(),URL.revokeObjectURL(c),d()}catch(t){alert(`Export failed: ${t}`)}finally{f(!1)}},W=async()=>{C(!0);try{const t=await fetch(`/api/v1/sessions/${j}/export`,{method:"POST",headers:{"Content-Type":"application/json"},body:N()});if(!t.ok)throw new Error(await t.text());const c=await(await t.blob()).text();await navigator.clipboard.writeText(c),b(!0),setTimeout(()=>b(!1),1500)}catch(t){alert(`Copy failed: ${t}`)}finally{C(!1)}};return e.jsx(K,{open:F,onClose:d,title:`Export ${h}.${o}`,footer:e.jsxs(e.Fragment,{children:[e.jsx(m,{variant:"ghost",onClick:d,children:"Cancel"}),e.jsx(m,{variant:"ghost",onClick:W,disabled:v||x,children:_?"Copied! ✓":v?"Copying…":"Copy"}),e.jsx(m,{variant:"primary",onClick:J,disabled:x||v,children:x?"Exporting…":"Download"})]}),children:e.jsxs("div",{className:"flex flex-col gap-4",children:[e.jsxs(n,{label:"Format",value:i,onChange:t=>Q(t.target.value),children:[!a&&e.jsx("option",{value:"insert",children:"INSERT SQL"}),!a&&e.jsx("option",{value:"delete",children:"DELETE SQL"}),!a&&e.jsx("option",{value:"delete+insert",children:"DELETE + INSERT SQL"}),e.jsx("option",{value:"csv",children:"CSV"})]}),e.jsx(R,{label:"Batch Size",type:"number",value:g,onChange:t=>$(t.target.value)}),i==="csv"&&e.jsxs("div",{children:[e.jsxs("button",{type:"button",className:"flex items-center gap-1 text-xs text-slate-400 hover:text-slate-200 transition-colors",onClick:()=>k(!u),children:[u?e.jsx(G,{size:12}):e.jsx(H,{size:12}),"Advanced CSV Options"]}),u&&e.jsxs("div",{className:"mt-3 flex flex-col gap-3 pl-4 border-l border-surface-700",children:[e.jsxs("div",{className:"flex gap-3",children:[e.jsxs(n,{label:"Delimiter",value:r,onChange:t=>B(t.target.value),children:[e.jsx("option",{value:",",children:"Comma (,)"}),e.jsx("option",{value:";",children:"Semicolon (;)"}),e.jsx("option",{value:"\\t",children:"Tab"}),e.jsx("option",{value:"|",children:"Pipe (|)"}),e.jsx("option",{value:"custom",children:"Custom"})]}),r==="custom"&&e.jsx(R,{label:"Custom",value:E,onChange:t=>M(t.target.value.slice(0,1)),className:"w-16"})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs(n,{label:"Quote Character",value:S,onChange:t=>O(t.target.value),children:[e.jsx("option",{value:'"',children:'Double Quote (")'}),e.jsx("option",{value:"'",children:"Single Quote (')"}),e.jsx("option",{value:"",children:"None"})]}),e.jsxs(n,{label:"Escape Character",value:w,onChange:t=>U(t.target.value),children:[e.jsx("option",{value:'"',children:'Double Quote (")'}),e.jsx("option",{value:"\\",children:String.raw`Backslash (\)`}),e.jsx("option",{value:"",children:"None"})]})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs(n,{label:"Line Ending",value:y,onChange:t=>z(t.target.value),children:[e.jsx("option",{value:"crlf",children:"CRLF (Windows)"}),e.jsx("option",{value:"lf",children:"LF (Unix/Mac)"}),e.jsx("option",{value:"cr",children:"CR (Legacy Mac)"})]}),e.jsxs(n,{label:"Encoding",value:L,onChange:t=>I(t.target.value),children:[e.jsx("option",{value:"utf-8",children:"UTF-8"}),e.jsx("option",{value:"utf-8-sig",children:"UTF-8 with BOM"}),e.jsx("option",{value:"ascii",children:"ASCII"})]})]})]})]}),e.jsx("p",{className:"text-xs text-slate-500",children:l?e.jsxs(e.Fragment,{children:["Exporting ",e.jsxs("strong",{className:"text-slate-300",children:[l.length," selected rows"]})," from ",e.jsx("code",{className:"text-slate-300",children:o}),"."]}):e.jsxs(e.Fragment,{children:["Exports all rows from ",e.jsx("code",{className:"text-slate-300",children:o}),". Large tables are streamed in batches."]})})]})})}export{te as default};
