1import{r as o,j as e}from"./vendor-react-BtGqQFvK.js";import{C as f,g as h,d as v,e as C,f as D}from"./card-DX6FEdjS.js";import{g as P,s as d,l as j,dO as E,aS as _,aN as k,n as S,bl as B,B as M,aY as q}from"./app-shell-CMj3qzLx.js";import{s as z}from"./stripHtml-DTNdUOhN.js";import{u as F}from"./vendor-i18n-Duo0qcql.js";import{a as T,e as $}from"./vendor-utils-BPqL1lCz.js";import"./vendor-ui-zUh5tm1d.js";import"./vendor-supabase-PhdYWwdx.js";import"./vendor-query-CzlW6vM0.js";import"./vendor-motion-D4fhTsRL.js";const R=t=>t?t<1024?`${t} B`:t<1024*1024?`${(t/1024).toFixed(1)} KB`:`${(t/(1024*1024)).toFixed(1)} MB`:"";function L(){const{t}=F(),{tenant_id:n}=P(),[m,N]=o.useState([]),[w,y]=o.useState(!0),[x,g]=o.useState(null),p=o.useCallback(async()=>{if(n)try{const{data:{user:s}}=await d.auth.getUser();if(!s)return;const{data:a,error:l}=await d.from("digital_product_purchases").select(` 2 id, 3 product_id, 4 purchased_at, 5 downloads_remaining 6 `).eq("tenant_id",n).eq("user_id",s.id).order("purchased_at",{ascending:!1});if(l)throw l;const c=await Promise.all((a||[]).map(async r=>{const[i,u]=await Promise.all([d.from("digital_products").select("id, name, description, thumbnail_url, product_type").eq("id",r.product_id).maybeSingle(),d.from("digital_product_files").select("id, name, file_url, file_size, file_type").eq("product_id",r.product_id).order("order_index",{ascending:!0})]);return{...r,product:i.data||{id:r.product_id,name:"Producto",description:null,thumbnail_url:null,product_type:"download"},files:u.data||[]}}));N(c)}catch(s){j.error("Error fetching purchases",s instanceof Error?s:new Error(String(s)))}finally{y(!1)}},[n]);o.useEffect(()=>{p()},[p]);const b=async(s,a,l,c)=>{g(s);try{const{data:{user:r}}=await d.auth.getUser();await d.from("digital_product_downloads").insert({tenant_id:n,product_id:m.find(u=>u.id===c)?.product_id,file_id:s,user_id:r?.id,purchase_id:c,download_token:`${Date.now()}-${Math.random().toString(36).substring(7)}`});const i=document.createElement("a");i.href=a,i.download=l,i.target="_blank",document.body.appendChild(i),i.click(),document.body.removeChild(i)}catch(r){j.error("Download error",r instanceof Error?r:new Error(String(r)))}finally{g(null)}};return w?e.jsx("div",{className:"text-center py-12 text-muted-foreground",children:t("common.loading","Loading...")}):m.length===0?e.jsx(f,{children:e.jsxs(h,{className:"py-12 text-center",children:[e.jsx(E,{className:"h-12 w-12 mx-auto text-muted-foreground mb-4"}),e.jsx("h3",{className:"font-semibold text-lg mb-2",children:t("digitalProducts.library.empty","No tienes productos digitales")}),e.jsx("p",{className:"text-muted-foreground",children:t("digitalProducts.library.emptyDesc","Cuando compres un producto digital, aparecerá aquà para descargarlo.")})]})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold",children:t("digitalProducts.library.title","Mi Biblioteca")}),e.jsx("p",{className:"text-muted-foreground",children:t("digitalProducts.library.subtitle","Tus productos digitales adquiridos")})]}),e.jsx("div",{className:"grid gap-4",children:m.map(s=>e.jsxs(f,{children:[e.jsx(v,{children:e.jsxs("div",{className:"flex items-start gap-4",children:[s.product.thumbnail_url?e.jsx("img",{src:s.product.thumbnail_url,alt:s.product.name,className:"w-20 h-20 object-cover rounded-lg",loading:"lazy",decoding:"async"}):e.jsx("div",{className:"w-20 h-20 bg-muted rounded-lg flex items-center justify-center",children:e.jsx(_,{className:"h-8 w-8 text-muted-foreground"})}),e.jsxs("div",{className:"flex-1",children:[e.jsx(C,{className:"text-lg",children:s.product.name}),e.jsx(D,{className:"line-clamp-2 mt-1",children:z(s.product.description)}),e.jsxs("div",{className:"flex items-center gap-2 mt-2",children:[e.jsx(k,{variant:"secondary",className:"capitalize",children:s.product.product_type}),e.jsxs("span",{className:"text-xs text-muted-foreground flex items-center gap-1",children:[e.jsx(S,{className:"h-3 w-3"}),T(new Date(s.purchased_at),"d MMM yyyy",{locale:$})]}),e.jsxs("span",{className:"text-xs text-success flex items-center gap-1",children:[e.jsx(B,{className:"h-3 w-3"}),t("digitalProducts.library.purchased","Comprado")]})]})]})]})}),e.jsxs(h,{children:[s.files.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:t("digitalProducts.library.noFiles","No hay archivos disponibles")}):e.jsxs("div",{className:"space-y-2",children:[e.jsxs("p",{className:"text-sm font-medium mb-2",children:[t("digitalProducts.library.files","Archivos incluidos")," (",s.files.length,")"]}),s.files.map(a=>e.jsxs("div",{className:"flex items-center justify-between p-3 bg-muted rounded-lg",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(_,{className:"h-5 w-5 text-muted-foreground"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium",children:a.name}),e.jsx("p",{className:"text-xs text-muted-foreground",children:R(a.file_size)})]})]}),e.jsxs(M,{size:"sm",onClick:()=>b(a.id,a.file_url,a.name,s.id),disabled:x===a.id,className:"gap-2",children:[e.jsx(q,{className:"h-4 w-4"}),x===a.id?t("common.downloading","Descargando..."):t("common.download","Descargar")]})]},a.id))]}),s.downloads_remaining!==null&&e.jsx("p",{className:"text-xs text-muted-foreground mt-3",children:t("digitalProducts.library.downloadsRemaining","Descargas restantes: {{count}}",{count:s.downloads_remaining})})]})]},s.id))})]})}const V=()=>e.jsx("div",{className:"p-4 sm:p-6 lg:p-8 space-y-6 min-h-screen",children:e.jsx(L,{})});
6export{V as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.