import Utils from"../utils";class FileEventsClass{constructor(){}wpUploadMedia(e,t=!0){let i,a=this,l="";return new Promise((function(t,s){i=wp.media({multiple:!1,order:"DESC",orderby:"date",title:"Select an Image"}).on("select",(()=>{let t=i.state().get("selection").first().toJSON();a.appendMediaToCell(t,e),l=t.url})).on("close",(()=>{setTimeout((()=>{if(l.length)return t(l)}),10)})).open()}))}appendMediaToCell(e,t){let i=e.id;t.cellData.value=i,t.cellData.html=this.getMediaElement(e),t.cellData.file_type=e.mime,t.cellData.attachment=e,t.cellData.type="file",t.cellData.attachment_type=e.type,Utils.updateCell(t.cellData,t.ActionsHandlerInstance)}getMediaElement(e){let t;switch(e.type){case"image":let i=void 0!==e.sizes.thumbnail?e.sizes.thumbnail.url:e.url;t=this.getImage(i);break;case"video":t=this.getVideo(e.url,e.mime);break;case"audio":t=this.getAudio(e.url,e.mime);break;default:t=this.getLink(e.url,e.filename)}return t}getImage(e){return`<a href="${e}" class="tablesome__inputMediaPreview--link" target="_blank">\n\t\t<img class="tablesome__inputMediaPreview tablesome__inputMediaPreview--image" src="${e}" />\n\t\t</a>`}getAudio(e,t){return`<audio controls class="tablesome__inputMediaPreview tablesome__inputMediaPreview--audio">\n\t\t<source src="${e}" type="${t}">\n\t\tYour browser does not support HTML audio\n\t\t</audio>`}getVideo(e,t){return`<video controls class="tablesome__inputMediaPreview tablesome__inputMediaPreview--video">\n\t\t<source src="${e}" type="${t}">\n\t\tYour browser does not support HTML video\n\t\t</video>`}getLink(e,t){return`<a class="tablesome__inputMediaPreview tablesome__inputMediaPreview--link" href="${e}" target="_blank">${t}</a>`}}let fileEvents=new FileEventsClass;export default fileEvents;