import{r as l,h as o}from"./p-dfbe004d.js";import{a as s}from"./p-7393a06c.js";import{_ as i}from"./p-8415f806.js";import"./p-fd56940f.js";import{a as n}from"./p-1c2e2695.js";import"./p-9a7a47e8.js";const t=class{constructor(s){l(this,s),this.renderFileExt=l=>{var s,i,n,t,e,d,a,r,u;if(null===(s=null==l?void 0:l.media)||void 0===s?void 0:s.filename)return null===(e=null===(t=null===(n=(i=l.media.filename).split)||void 0===n?void 0:n.call(i,"."))||void 0===t?void 0:t.pop)||void 0===e?void 0:e.call(t);if(null==l?void 0:l.url)try{const o=new URL(l.url);if(o.pathname.includes("."))return null===(u=null===(r=null===(a=(d=o.pathname).split)||void 0===a?void 0:a.call(d,"."))||void 0===r?void 0:r.pop)||void 0===u?void 0:u.call(r)}catch(l){console.error(l)}return o("sc-icon",{name:"file"})}}async downloadItem(l){var o,t;if(null==l?void 0:l.url)return void this.downloadFile(l.url,null!==(o=null==l?void 0:l.name)&&void 0!==o?o:"file");const e=null===(t=null==l?void 0:l.media)||void 0===t?void 0:t.id;if(e)try{this.busy=e;const l=await s({path:n(`surecart/v1/customers/${this.customerId}/expose/${e}`,{expose_for:60})});if(!(null==l?void 0:l.url))throw{message:i("Could not download the file.","surecart")};this.downloadFile(null==l?void 0:l.url,l.filename)}catch(l){console.error(l),this.error=(null==l?void 0:l.message)||i("Something went wrong","surecart")}finally{this.busy=null}}downloadFile(l,o){const s=document.createElement("a");s.href=l,s.download=o,document.body.appendChild(s),s.click(),setTimeout((()=>{document.body.removeChild(s)}),0)}render(){const l=this.downloads||[];return o("sc-dashboard-module",{class:"purchase",part:"base",heading:i("Downloads","surecart")},o("span",{slot:"heading"},o("slot",{name:"heading"},this.heading||i("Downloads","surecart"))),o("sc-card",{"no-padding":!0},o("sc-stacked-list",null,l.map((l=>{var s,n,t,e;const d=null==l?void 0:l.media;return o("sc-stacked-list-row",{style:{"--columns":"1"}},o("sc-flex",{class:"single-download",justifyContent:"flex-start",alignItems:"center"},o("div",{class:"single-download__preview"},this.renderFileExt(l)),o("div",null,o("div",null,o("strong",null,null!==(n=null!==(s=null==d?void 0:d.filename)&&void 0!==s?s:null==l?void 0:l.name)&&void 0!==n?n:"")),o("sc-flex",{justifyContent:"flex-start",alignItems:"center",style:{gap:"0.5em"}},(null==d?void 0:d.byte_size)&&o("sc-format-bytes",{value:d.byte_size}),!!(null===(t=null==d?void 0:d.release_json)||void 0===t?void 0:t.version)&&o("sc-tag",{type:"primary",size:"small",style:{"--sc-tag-primary-background-color":"#f3e8ff","--sc-tag-primary-color":"#6b21a8"}},"v",null===(e=null==d?void 0:d.release_json)||void 0===e?void 0:e.version)))),o("sc-button",{size:"small",slot:"suffix",onClick:()=>this.downloadItem(l),busy:!!(null==d?void 0:d.id)&&this.busy==(null==d?void 0:d.id),disabled:!!(null==d?void 0:d.id)&&this.busy==(null==d?void 0:d.id)},i("Download","surecart")))})))))}};t.style=":host{display:block}.purchase{display:flex;flex-direction:column;gap:var(--sc-spacing-large)}.single-download .single-download__preview{display:flex;align-items:center;justify-content:center;background:var(--sc-color-gray-200);border-radius:var(--sc-border-radius-small);height:4rem;min-width:4rem;width:4rem}";export{t as sc_downloads_list}