import{h}from"@stencil/core";import apiFetch from"../../../../functions/fetch";import{__}from"@wordpress/i18n";import{addQueryArgs}from"@wordpress/url";export class ScDownloadsList{constructor(){this.renderFileExt=e=>{var o,l,t,i,s,n,r,d,a;if(null===(o=null==e?void 0:e.media)||void 0===o?void 0:o.filename)return null===(s=null===(i=null===(t=(l=e.media.filename).split)||void 0===t?void 0:t.call(l,"."))||void 0===i?void 0:i.pop)||void 0===s?void 0:s.call(i);if(null==e?void 0:e.url)try{const o=new URL(e.url);if(o.pathname.includes("."))return null===(a=null===(d=null===(r=(n=o.pathname).split)||void 0===r?void 0:r.call(n,"."))||void 0===d?void 0:d.pop)||void 0===a?void 0:a.call(d)}catch(e){console.error(e)}return h("sc-icon",{name:"file"})},this.downloads=void 0,this.customerId=void 0,this.heading=void 0,this.busy=void 0,this.error=void 0}async downloadItem(e){var o,l;if(null==e?void 0:e.url)return void this.downloadFile(e.url,null!==(o=null==e?void 0:e.name)&&void 0!==o?o:"file");const t=null===(l=null==e?void 0:e.media)||void 0===l?void 0:l.id;if(t)try{this.busy=t;const e=await apiFetch({path:addQueryArgs(`surecart/v1/customers/${this.customerId}/expose/${t}`,{expose_for:60})});if(!(null==e?void 0:e.url))throw{message:__("Could not download the file.","surecart")};this.downloadFile(null==e?void 0:e.url,e.filename)}catch(e){console.error(e),this.error=(null==e?void 0:e.message)||__("Something went wrong","surecart")}finally{this.busy=null}}downloadFile(e,o){const l=document.createElement("a");l.href=e,l.download=o,document.body.appendChild(l),l.click(),setTimeout((()=>{document.body.removeChild(l)}),0)}render(){const e=this.downloads||[];return h("sc-dashboard-module",{key:"ff99634c5a10b0ebe5faa223c9e37cbbf44476f4",class:"purchase",part:"base",heading:__("Downloads","surecart")},h("span",{key:"3c05a3f6d54aaf54e4b1c4d311db1d16f4a6ffa7",slot:"heading"},h("slot",{key:"fe4fcb0c532b2f327e2096ddc2afff8eb71946c9",name:"heading"},this.heading||__("Downloads","surecart"))),h("sc-card",{key:"232e4cd2a53c451ce75c67812b437b39990e7b04","no-padding":!0},h("sc-stacked-list",{key:"a2503975f22cd80b70be7da6c9f558c63fff39cb"},e.map((e=>{var o,l,t,i;const s=null==e?void 0:e.media;return h("sc-stacked-list-row",{style:{"--columns":"1"}},h("sc-flex",{class:"single-download",justifyContent:"flex-start",alignItems:"center"},h("div",{class:"single-download__preview"},this.renderFileExt(e)),h("div",null,h("div",null,h("strong",null,null!==(l=null!==(o=null==s?void 0:s.filename)&&void 0!==o?o:null==e?void 0:e.name)&&void 0!==l?l:"")),h("sc-flex",{justifyContent:"flex-start",alignItems:"center",style:{gap:"0.5em"}},(null==s?void 0:s.byte_size)&&h("sc-format-bytes",{value:s.byte_size}),!!(null===(t=null==s?void 0:s.release_json)||void 0===t?void 0:t.version)&&h("sc-tag",{type:"primary",size:"small",style:{"--sc-tag-primary-background-color":"#f3e8ff","--sc-tag-primary-color":"#6b21a8"}},"v",null===(i=null==s?void 0:s.release_json)||void 0===i?void 0:i.version)))),h("sc-button",{size:"small",slot:"suffix",onClick:()=>this.downloadItem(e),busy:!!(null==s?void 0:s.id)&&this.busy==(null==s?void 0:s.id),disabled:!!(null==s?void 0:s.id)&&this.busy==(null==s?void 0:s.id)},__("Download","surecart")))})))))}static get is(){return"sc-downloads-list"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-downloads-list.scss"]}}static get styleUrls(){return{$:["sc-downloads-list.css"]}}static get properties(){return{downloads:{type:"unknown",mutable:!1,complexType:{original:"Download[]",resolved:"Download[]",references:{Download:{location:"import",path:"../../../../types",id:"src/types.ts::Download"}}},required:!1,optional:!1,docs:{tags:[],text:""}},customerId:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"customer-id",reflect:!1},heading:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"heading",reflect:!1}}}static get states(){return{busy:{},error:{}}}}