import{r as p,j as s}from"./index-CUDottTv.js";import{D as b}from"./download-CwvkYJyJ.js";function f(t,n){const o=new Blob([JSON.stringify(t,null,2)],{type:"application/json"});x(o,`${n}.json`)}function m(t,n){if(t.length===0)return;const o=Object.keys(t[0]),e=o.join(","),r=t.map(a=>o.map(u=>{const d=a[u];if(d==null)return"";const i=typeof d=="object"?JSON.stringify(d):String(d);return i.includes(",")||i.includes('"')||i.includes(`
`)?`"${i.replace(/"/g,'""')}"`:i}).join(",")),c=[e,...r].join(`
`),l=new Blob([c],{type:"text/csv"});x(l,`${n}.csv`)}function x(t,n){const o=URL.createObjectURL(t),e=document.createElement("a");e.href=o,e.download=n,document.body.appendChild(e),e.click(),document.body.removeChild(e),URL.revokeObjectURL(o)}function y({data:t,filename:n,disabled:o}){const[e,r]=p.useState(!1),c=p.useRef(null);p.useEffect(()=>{if(!e)return;const a=u=>{c.current&&!c.current.contains(u.target)&&r(!1)};return document.addEventListener("mousedown",a),()=>document.removeEventListener("mousedown",a)},[e]);const l=t.length;return s.jsxs("div",{className:"relative",ref:c,children:[s.jsxs("button",{type:"button",onClick:()=>r(!e),disabled:o||l===0,className:"flex items-center gap-1.5 px-2.5 py-1.5 rounded-md text-[12px] font-medium text-text-tertiary hover:text-text-secondary hover:bg-bg-hover transition-colors cursor-pointer disabled:opacity-40 disabled:cursor-not-allowed",children:[s.jsx(b,{className:"w-3.5 h-3.5"}),"Export"]}),e&&s.jsxs("div",{className:"absolute right-0 top-full mt-1 bg-bg-elevated border border-border-default rounded-lg shadow-lg z-20 py-1 min-w-[140px]",children:[s.jsxs("button",{type:"button",onClick:()=>{f(t,n),r(!1)},className:"w-full px-3 py-1.5 text-left text-[12px] text-text-secondary hover:bg-bg-hover transition-colors cursor-pointer",children:["Export JSON (",l,")"]}),s.jsxs("button",{type:"button",onClick:()=>{m(t,n),r(!1)},className:"w-full px-3 py-1.5 text-left text-[12px] text-text-secondary hover:bg-bg-hover transition-colors cursor-pointer",children:["Export CSV (",l,")"]})]})]})}export{y as E};
