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,d,n,r,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===(r=null===(n=(d=o.pathname).split)||void 0===n?void 0:n.call(d,"."))||void 0===r?void 0:r.pop)||void 0===a?void 0:a.call(r)}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:"7ef3dca046fbcd55ff81c5ab92f52b2dd8630f3b",class:"purchase",part:"base",heading:__("Downloads","surecart")},h("span",{key:"d3d95afb070f1fb26319c1002d02e5d075e196f0",slot:"heading"},h("slot",{key:"c35e1ad0ffa901edab292c0f10c9bd489e406478",name:"heading"},this.heading||__("Downloads","surecart"))),h("sc-card",{key:"8152d989ca16d8c791cb2fc5ace95d4211c56ad2","no-padding":!0},h("sc-stacked-list",{key:"413b8f5775ea4f611adf3e513f6b2941fed45ca1"},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:{}}}}