import{a as c,j as e}from"./vendor-react-YMhRUmcb.js";import{m as j,B as x,l as N,C as y}from"./index-D2uzyr9g.js";import{bb as w,X as S,bc as k,al as C,b1 as R}from"./vendor-icons-DINCNwpk.js";import{c as D}from"./commerceService-bs-xd9qB.js";import{S as I}from"./input-DL1WY4HG.js";import{S as h}from"./skeleton-BPDNnZ8E.js";import"./vendor-fFnFILYR.js";import"./vendor-router-DX94Iu-2.js";import"./vendor-tanstack-CoFNL2zy.js";import"./vendor-utils-4BWoYre8.js";const P=({orderId:s,onClose:t})=>{const{addToast:a}=j(),[d,n]=c.useState("Duplicate Purchase"),[i,o]=c.useState(""),r=()=>{a(`Refund request submitted for Order #${s}`,"success"),t()};return e.jsxs("div",{className:"fixed inset-0 z-[100] flex items-center justify-center p-4",children:[e.jsx("div",{className:"absolute inset-0 bg-background/90 backdrop-blur-sm",onClick:t}),e.jsxs("div",{className:"relative w-full max-w-lg bg-card border border-border rounded-xl shadow-2xl animate-scaleIn overflow-hidden",children:[e.jsxs("div",{className:"p-4 border-b border-border bg-muted flex justify-between items-center",children:[e.jsxs("h3",{className:"font-bold text-white flex items-center gap-2",children:[e.jsx(w,{className:"w-4 h-4 text-warning"})," Request Refund"]}),e.jsx("button",{onClick:t,children:e.jsx(S,{className:"w-5 h-5 text-muted-foreground hover:text-white"})})]}),e.jsxs("div",{className:"p-6 space-y-4",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Refund requests are subject to approval. Please provide details below for Order"," ",e.jsxs("span",{className:"font-mono text-white",children:["#",s]}),"."]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-bold text-muted-foreground uppercase mb-2",children:"Reason"}),e.jsxs("select",{className:"w-full bg-muted border border-border rounded-lg px-4 py-2.5 text-white focus:border-primary outline-none",value:d,onChange:u=>n(u.target.value),children:[e.jsx("option",{children:"Duplicate Purchase"}),e.jsx("option",{children:"Accidental Purchase"}),e.jsx("option",{children:"Quality Issue / Corrupted File"}),e.jsx("option",{children:"Other"})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-bold text-muted-foreground uppercase mb-2",children:"Details"}),e.jsx("textarea",{className:"w-full bg-muted border border-border rounded-lg p-4 text-white focus:border-primary outline-none text-sm resize-none h-24",placeholder:"Please explain why you are requesting a refund...",value:i,onChange:u=>o(u.target.value)})]}),e.jsxs("div",{className:"border-2 border-dashed border-border rounded-lg p-6 flex flex-col items-center justify-center text-muted-foreground hover:text-foreground hover:border-border cursor-pointer transition-colors",children:[e.jsx(k,{className:"w-8 h-8 mb-2"}),e.jsx("span",{className:"text-xs font-bold uppercase",children:"Upload Evidence (Optional)"})]})]}),e.jsxs("div",{className:"p-4 border-t border-border bg-muted flex justify-end gap-4",children:[e.jsx(x,{variant:"ghost",onClick:t,children:"Cancel"}),e.jsx(x,{variant:"primary",onClick:r,children:"Submit Request"})]})]})]})};function O(s){const{addToast:t}=j(),[a,d]=c.useState(""),[n,i]=c.useState(null),[o,r]=c.useState(null),[u,m]=c.useState(s??[]),[f,b]=c.useState(s==null),p=c.useCallback(async()=>{if(s!=null){m(s),b(!1);return}b(!0);try{const l=await D.getPurchases();m(l)}catch(l){N.error("Error loading purchases",{error:l instanceof Error?l.message:String(l),stack:l instanceof Error?l.stack:void 0})}finally{b(!1)}},[s]);c.useEffect(()=>{p()},[p]);const g=u.filter(l=>l.product.title.toLowerCase().includes(a.toLowerCase())),v=c.useCallback(l=>{t(`Downloading ${l}...`,"success"),r(null)},[t]);return{search:a,setSearch:d,refundOrderId:n,setRefundOrderId:i,activeDownloadId:o,setActiveDownloadId:r,purchases:g,loading:f,addToast:t,handleDownload:v}}function A({search:s,onSearchChange:t}){return e.jsxs("div",{className:"flex flex-col md:flex-row justify-between items-end border-b border-border pb-6 gap-4 mb-8",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-display font-bold text-white mb-2",children:"MY PURCHASES"}),e.jsx("p",{className:"text-muted-foreground font-mono text-sm",children:"Access your sounds, licenses, and receipts."})]}),e.jsx("div",{className:"w-full md:w-64",children:e.jsx(I,{placeholder:"Search library...",value:s,onChange:a=>t(a.target.value)})})]})}const E=["WAV (24-bit)","MP3 (320kbps)","Stems (ZIP)"];function L({purchase:s,isDownloadOpen:t,onToggleDownload:a,onDownloadFormat:d,onLicense:n,onRefund:i}){const o=s.product;return e.jsxs(y,{variant:"default",className:"flex flex-col md:flex-row items-center gap-8 p-4",children:[e.jsx("div",{className:"w-20 h-20 rounded-lg overflow-hidden flex-shrink-0 bg-muted",children:e.jsx("img",{src:o.coverUrl,alt:"",className:"w-full h-full object-cover"})}),e.jsxs("div",{className:"flex-1 w-full text-center md:text-left",children:[e.jsx("h3",{className:"font-bold text-foreground text-lg mb-1 tracking-tight",children:o.title}),e.jsxs("div",{className:"flex flex-wrap items-center justify-center md:justify-start gap-4 text-xs text-muted-foreground",children:[e.jsx("span",{className:"bg-card px-2 py-1 rounded border border-border text-muted-foreground",children:o.type??"pack"}),e.jsxs("span",{children:["Order #",s.orderId]}),e.jsx("span",{children:"•"}),e.jsx("span",{children:s.date}),e.jsx("span",{children:"•"}),e.jsxs("span",{className:"text-foreground",children:["$",s.price]})]})]}),e.jsxs("div",{className:"flex flex-col sm:flex-row gap-4 w-full md:w-auto",children:[e.jsxs("div",{className:"relative",children:[e.jsx(x,{variant:"primary",size:"sm",icon:e.jsx(C,{className:"w-4 h-4"}),onClick:a,children:"Download"}),t&&e.jsx("div",{className:"absolute top-full right-0 mt-2 w-40 bg-card border border-border rounded-lg shadow-xl z-20 overflow-hidden animate-fadeIn",children:E.map(r=>e.jsx("button",{type:"button",className:"w-full text-left px-4 py-2 text-xs text-white hover:bg-white/10",onClick:()=>d(r),children:r},r))})]}),e.jsx(x,{variant:"ghost",size:"sm",className:"border border-border",icon:e.jsx(R,{className:"w-4 h-4"}),onClick:n,children:"License"}),e.jsx(x,{variant:"ghost",size:"sm",className:"text-muted-foreground hover:text-foreground",title:"Request Refund",onClick:i,children:e.jsx(w,{className:"w-4 h-4"})})]})]})}function q({purchases:s,loading:t,activeDownloadId:a,setActiveDownloadId:d,onDownloadFormat:n,onLicense:i,onRefund:o}){return t?null:s.length===0?e.jsx("div",{className:"text-center py-24 text-muted-foreground",children:e.jsx("p",{children:"No purchases found."})}):e.jsx("div",{className:"space-y-4",children:s.map(r=>e.jsx(L,{purchase:r,isDownloadOpen:a===r.id,onToggleDownload:()=>d(a===r.id?null:r.id),onDownloadFormat:n,onLicense:i,onRefund:()=>o(r.orderId)},r.id))})}function T(){return e.jsxs("div",{className:"animate-fadeIn max-w-5xl mx-auto pb-20",children:[e.jsxs("div",{className:"flex flex-col md:flex-row justify-between items-end border-b border-border pb-6 gap-4 mb-8",children:[e.jsxs("div",{children:[e.jsx(h,{className:"h-9 w-56 mb-2"}),e.jsx(h,{className:"h-4 w-72"})]}),e.jsx(h,{className:"h-10 w-full md:w-64"})]}),e.jsx("div",{className:"space-y-4",children:[1,2,3].map(s=>e.jsx(h,{className:"h-28 w-full rounded-xl"},s))})]})}function X({initialPurchases:s}={}){const{search:t,setSearch:a,refundOrderId:d,setRefundOrderId:n,activeDownloadId:i,setActiveDownloadId:o,purchases:r,loading:u,addToast:m,handleDownload:f}=O(s??void 0);return u?e.jsx(T,{}):e.jsxs("div",{className:"animate-fadeIn max-w-5xl mx-auto pb-20",children:[e.jsx(A,{search:t,onSearchChange:a}),e.jsx(q,{purchases:r,loading:!1,activeDownloadId:i,setActiveDownloadId:o,onDownloadFormat:f,onLicense:()=>m("License document opened"),onRefund:n}),d&&e.jsx(P,{orderId:d,onClose:()=>n(null)})]})}export{X as PurchasesView};