import{j as e}from"./vendor-codemirror-D5amxuPd.js";import{r as a,f as q,h as J}from"./vendor-ui-BLV2MnSs.js";import{M as X,S as o,I as R,B as g,c as G}from"./index-BH8xd5gL.js";import"./vendor-ag-grid-NsbaVf01.js";function se({open:$,onClose:d,sessionId:h,database:x,table:i,sql:n,pkValues:r}){const[l,F]=a.useState(n?"csv":"insert"),[C,Q]=a.useState("500"),[p,b]=a.useState(!1),[u,E]=a.useState(!1),[M,S]=a.useState(!1),[v,O]=a.useState(!1),[c,_]=a.useState(","),[w,k]=a.useState(""),[y,B]=a.useState('"'),[L,U]=a.useState('"'),[T,z]=a.useState("crlf"),[N,I]=a.useState("utf-8"),A={crlf:`\r
`,lf:`
`,cr:"\r"}[T]||`\r
`,P=c==="custom"?w:c,m=()=>JSON.stringify({database:x,...n?{sql:n}:{table:i},format:l,batch_size:parseInt(C),...r?{pk_values:r}:{},...l==="csv"?{csv_delimiter:P,csv_quotechar:y,csv_escapechar:L,csv_lineterminator:A,csv_encoding:N}:{}}),H=async()=>{b(!0);try{const t=await fetch(`/api/v1/sessions/${h}/export`,{method:"POST",headers:{"Content-Type":"application/json"},body:m()});if(!t.ok)throw new Error(await t.text());const s=await t.blob(),j=URL.createObjectURL(s),f=document.createElement("a"),D=(t.headers.get("Content-Disposition")??"").match(/filename="([^"]+)"/);f.download=D?D[1]:`${x}_${i}.${l==="csv"?"csv":"sql"}`,f.href=j,f.click(),URL.revokeObjectURL(j),d()}catch(t){alert(`Export failed: ${t}`)}finally{b(!1)}},W=async()=>{E(!0);try{let t;if(window.isSecureContext){const s=await fetch(`/api/v1/sessions/${h}/export`,{method:"POST",headers:{"Content-Type":"application/json"},body:m()});if(!s.ok)throw new Error(await s.text());t=await(await s.blob()).text()}else{const s=new XMLHttpRequest;if(s.open("POST",`/api/v1/sessions/${h}/export`,!1),s.setRequestHeader("Content-Type","application/json"),s.send(m()),s.status!==200)throw new Error(s.responseText);t=s.responseText}await G(t),S(!0),setTimeout(()=>S(!1),1500)}catch(t){alert(`Copy failed: ${t}`)}finally{E(!1)}};return e.jsx(X,{open:$,onClose:d,title:`Export ${x}.${i}`,footer:e.jsxs(e.Fragment,{children:[e.jsx(g,{variant:"ghost",onClick:d,children:"Cancel"}),e.jsx(g,{variant:"ghost",onClick:W,disabled:u||p,children:M?"Copied! ✓":u?"Copying…":"Copy"}),e.jsx(g,{variant:"primary",onClick:H,disabled:p||u,children:p?"Exporting…":"Download"})]}),children:e.jsxs("div",{className:"flex flex-col gap-4",children:[e.jsxs(o,{label:"Format",value:l,onChange:t=>F(t.target.value),children:[!n&&e.jsx("option",{value:"insert",children:"INSERT SQL"}),!n&&e.jsx("option",{value:"delete",children:"DELETE SQL"}),!n&&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:C,onChange:t=>Q(t.target.value)}),l==="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:()=>O(!v),children:[v?e.jsx(q,{size:12}):e.jsx(J,{size:12}),"Advanced CSV Options"]}),v&&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(o,{label:"Delimiter",value:c,onChange:t=>_(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"})]}),c==="custom"&&e.jsx(R,{label:"Custom",value:w,onChange:t=>k(t.target.value.slice(0,1)),className:"w-16"})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs(o,{label:"Quote Character",value:y,onChange:t=>B(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(o,{label:"Escape Character",value:L,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(o,{label:"Line Ending",value:T,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(o,{label:"Encoding",value:N,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:r?e.jsxs(e.Fragment,{children:["Exporting ",e.jsxs("strong",{className:"text-slate-300",children:[r.length," selected rows"]})," from ",e.jsx("code",{className:"text-slate-300",children:i}),"."]}):e.jsxs(e.Fragment,{children:["Exports all rows from ",e.jsx("code",{className:"text-slate-300",children:i}),". Large tables are streamed in batches."]})})]})})}export{se as default};
