import*as e from"@wordpress/interactivity";var t={833:(t,o,r)=>{t.exports=(e=>{var t={};return r.d(t,e),t})({getConfig:()=>e.getConfig,getContext:()=>e.getContext,getElement:()=>e.getElement,store:()=>e.store,withScope:()=>e.withScope})}},o={};function r(e){var n=o[e];if(void 0!==n)return n.exports;var i=o[e]={exports:{}};return t[e](i,i.exports,r),i.exports}r.d=(e,t)=>{for(var o in t)r.o(t,o)&&!r.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var n=r(833);const i="jetpack/field-file",a=(e,t=2)=>{const o=(0,n.getConfig)(i);if(0===e)return o.i18n.zeroBytes;const r=t<0?0:t,a=o.i18n.fileSizeUnits||["Bytes","KB","MB","GB","TB"],s=Math.floor(Math.log(e)/Math.log(1024)),l=parseFloat((e/Math.pow(1024,s)).toFixed(r));return`${new Intl.NumberFormat(o.i18n.locale,{minimumFractionDigits:r,maximumFractionDigits:r}).format(l)} ${a[s]}`},s=e=>{const t=new FileReader;t.readAsDataURL(e),t.onload=(0,n.withScope)((()=>{const o=(0,n.getContext)(),r=(0,n.getConfig)(i),s=performance.now()+"-"+Math.random();let d=null;e.size>r.maxUploadSize&&(d=r.i18n.fileTooLarge),o.files.push({name:e.name,url:"url("+t.result+")",formattedSize:a(e.size,2),hasToken:!1,id:s,error:d}),o.hasFiles=!0,!d&&l(e,s)}))},l=(e,t)=>{const{endpoint:o,uploadToken:r}=(0,n.getConfig)(i),a=new XMLHttpRequest,s=new FormData;a.open("POST",o,!0),a.upload.addEventListener("progress",(0,n.withScope)(d.bind(void 0,t))),a.addEventListener("readystatechange",(0,n.withScope)(p.bind(void 0,t))),s.append("file",e),s.append("upload_token",r),a.send(s)},d=(e,t)=>{const o=t.loaded/t.total*100;f({progress:Math.min(o,97)},e)},p=(e,t)=>{const o=t.target;if(4===o.readyState){if(200===o.status){const t=JSON.parse(o.responseText);if(t.success)return void f({token:t.data.token,hasToken:!0},e)}if(o.responseText){const e=JSON.parse(o.responseText);console.error("Error uploading file",e)}}},f=(e,t)=>{const o=(0,n.getContext)(),r=o.files.findIndex((e=>e.id===t));o.files[r]=Object.assign(o.files[r],e)};(0,n.store)(i,{actions:{openFilePicker(){const{ref:e}=(0,n.getElement)(),t=e.parentNode.querySelector(".jetpack-form-file-field");t&&t.click()},fileAdded:e=>{Array.from(e.target.files).forEach(s)},fileDropped:e=>{if(e.preventDefault(),e.dataTransfer)for(const t of Array.from(e.dataTransfer.items)){if(t.webkitGetAsEntry()?.isDirectory)return;s(t.getAsFile())}(0,n.getContext)().isDropping=!1},dragOver:e=>{(0,n.getContext)().isDropping=!0,e.preventDefault()},dragLeave:()=>{(0,n.getContext)().isDropping=!1},removeFile:e=>{const t=(0,n.getContext)(),o=e.target.dataset.id;t.files=t.files.filter((e=>e.id!==o)),t.hasFiles=t.files.length>0,(e=>{const{endpoint:t,uploadToken:o}=(0,n.getConfig)(i),r=new FormData;r.append("file_id",e),r.append("upload_token",o),fetch(`${t}/remove`,{method:"POST",body:r}).then((e=>e.json()))})(o)}},callbacks:{}});