import{r as o,h as i}from"./p-e97fde0a.js";import{a as d}from"./p-6135d661.js";import{a as l}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(d){o(this,d);this.renderFileExt=o=>{var d,l,e,s,n,t,a,r,c;if((d=o===null||o===void 0?void 0:o.media)===null||d===void 0?void 0:d.filename){return(n=(s=(e=(l=o.media.filename).split)===null||e===void 0?void 0:e.call(l,"."))===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 d({path:l(`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 d=document.createElement("a");d.href=o;d.download=i;document.body.appendChild(d);d.click();setTimeout((()=>{document.body.removeChild(d)}),0)}render(){const o=this.downloads||[];return i("sc-dashboard-module",{key:"7ef3dca046fbcd55ff81c5ab92f52b2dd8630f3b",class:"purchase",part:"base",heading:wp.i18n.__("Downloads","surecart")},i("span",{key:"d3d95afb070f1fb26319c1002d02e5d075e196f0",slot:"heading"},i("slot",{key:"c35e1ad0ffa901edab292c0f10c9bd489e406478",name:"heading"},this.heading||wp.i18n.__("Downloads","surecart"))),i("sc-card",{key:"8152d989ca16d8c791cb2fc5ace95d4211c56ad2","no-padding":true},i("sc-stacked-list",{key:"413b8f5775ea4f611adf3e513f6b2941fed45ca1"},o.map((o=>{var d,l,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,(l=(d=n===null||n===void 0?void 0:n.filename)!==null&&d!==void 0?d:o===null||o===void 0?void 0:o.name)!==null&&l!==void 0?l:"")),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-e84c4934.entry.js.map