import{r as o,h as i}from"./p-e97fde0a.js";import{a as l}from"./p-d9fa04f9.js";import{a as d}from"./p-401e165e.js";import"./p-d3366af3.js";const e=":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}";const s=e;const n=class{constructor(l){o(this,l);this.renderFileExt=o=>{var l,d,e,s,n,t,a,r,c;if((l=o===null||o===void 0?void 0:o.media)===null||l===void 0?void 0:l.filename){return(n=(s=(e=(d=o.media.filename).split)===null||e===void 0?void 0:e.call(d,"."))===null||s===void 0?void 0:s.pop)===null||n===void 0?void 0:n.call(s)}if(o===null||o===void 0?void 0:o.url){try{const i=new URL(o.url);if(i.pathname.includes(".")){return(c=(r=(a=(t=i.pathname).split)===null||a===void 0?void 0:a.call(t,"."))===null||r===void 0?void 0:r.pop)===null||c===void 0?void 0:c.call(r)}}catch(o){console.error(o)}}return i("sc-icon",{name:"file"})};this.downloads=undefined;this.customerId=undefined;this.heading=undefined;this.busy=undefined;this.error=undefined}async downloadItem(o){var i,e;if(o===null||o===void 0?void 0:o.url){this.downloadFile(o.url,(i=o===null||o===void 0?void 0:o.name)!==null&&i!==void 0?i:"file");return}const s=(e=o===null||o===void 0?void 0:o.media)===null||e===void 0?void 0:e.id;if(!s)return;try{this.busy=s;const o=await l({path:d(`surecart/v1/customers/${this.customerId}/expose/${s}`,{expose_for:60})});if(!(o===null||o===void 0?void 0:o.url)){throw{message:wp.i18n.__("Could not download the file.","surecart")}}this.downloadFile(o===null||o===void 0?void 0:o.url,o.filename)}catch(o){console.error(o);this.error=(o===null||o===void 0?void 0:o.message)||wp.i18n.__("Something went wrong","surecart")}finally{this.busy=null}}downloadFile(o,i){const l=document.createElement("a");l.href=o;l.download=i;document.body.appendChild(l);l.click();setTimeout((()=>{document.body.removeChild(l)}),0)}render(){const o=this.downloads||[];return i("sc-dashboard-module",{key:"ff99634c5a10b0ebe5faa223c9e37cbbf44476f4",class:"purchase",part:"base",heading:wp.i18n.__("Downloads","surecart")},i("span",{key:"3c05a3f6d54aaf54e4b1c4d311db1d16f4a6ffa7",slot:"heading"},i("slot",{key:"fe4fcb0c532b2f327e2096ddc2afff8eb71946c9",name:"heading"},this.heading||wp.i18n.__("Downloads","surecart"))),i("sc-card",{key:"232e4cd2a53c451ce75c67812b437b39990e7b04","no-padding":true},i("sc-stacked-list",{key:"a2503975f22cd80b70be7da6c9f558c63fff39cb"},o.map((o=>{var l,d,e,s;const n=o===null||o===void 0?void 0:o.media;return i("sc-stacked-list-row",{style:{"--columns":"1"}},i("sc-flex",{class:"single-download",justifyContent:"flex-start",alignItems:"center"},i("div",{class:"single-download__preview"},this.renderFileExt(o)),i("div",null,i("div",null,i("strong",null,(d=(l=n===null||n===void 0?void 0:n.filename)!==null&&l!==void 0?l:o===null||o===void 0?void 0:o.name)!==null&&d!==void 0?d:"")),i("sc-flex",{justifyContent:"flex-start",alignItems:"center",style:{gap:"0.5em"}},(n===null||n===void 0?void 0:n.byte_size)&&i("sc-format-bytes",{value:n.byte_size}),!!((e=n===null||n===void 0?void 0:n.release_json)===null||e===void 0?void 0:e.version)&&i("sc-tag",{type:"primary",size:"small",style:{"--sc-tag-primary-background-color":"#f3e8ff","--sc-tag-primary-color":"#6b21a8"}},"v",(s=n===null||n===void 0?void 0:n.release_json)===null||s===void 0?void 0:s.version)))),i("sc-button",{size:"small",slot:"suffix",onClick:()=>this.downloadItem(o),busy:(n===null||n===void 0?void 0:n.id)?this.busy==(n===null||n===void 0?void 0:n.id):false,disabled:(n===null||n===void 0?void 0:n.id)?this.busy==(n===null||n===void 0?void 0:n.id):false},wp.i18n.__("Download","surecart")))})))))}};n.style=s;export{n as sc_downloads_list};
//# sourceMappingURL=p-f6732e7e.entry.js.map