(()=>{"use strict";function e(e){const t=new DataTransfer;for(const i of e)t.items.add(i);return t.files}function t(e){return!!e.classList.contains("jet-form-builder-file-upload__input")&&"file"===e.type}const{InputData:i}=window.JetFormBuilderAbstract;function r(){i.call(this),this.isMultiple=!1,this.prevFiles=null,this.template=null,this.previewsContainer=null,this.wrapper=null,this.isSupported=function(e){return t(e)},this.addListeners=function(){const[t]=this.nodes;t.addEventListener("change",(t=>{var i;this.value.current=e(this.isMultiple?[...null!==(i=this.prevFiles)&&void 0!==i?i:[],...t.target.files]:[...t.target.files])}))},this.setNode=function(e){i.prototype.setNode.call(this,e),this.isMultiple=e.multiple,this.wrapper=e.closest(".jet-form-builder-file-upload"),this.previewsContainer=this.wrapper.querySelector(".jet-form-builder-file-upload__files"),this.template=this.wrapper.closest(".field-type-media-field").querySelector(".jet-form-builder__preview-template")},this.setValue=function(){this.callable.loadFiles()},this.initNotifyValue=()=>{},this.reQueryValue=()=>{}}r.prototype=Object.create(i.prototype),r.prototype.wrapper=null,r.prototype.previewsContainer=null,r.prototype.template=null;const n=r,{BaseSignal:o}=window.JetFormBuilderAbstract;function l(){o.call(this),this.lock.current=!0,this.isSupported=function(e){return t(e)},this.runSignal=function(){const[t]=this.input.nodes,i=[],{current:r}=this.input.value,n=a(null!=r?r:[]);for(const e of n)i.push(this.getPreview(e));!function(e,t){const i=e.querySelectorAll(".jet-form-builder-file-upload__file");for(const e of i)t.some((t=>t.isEqualNode(e)))||e.remove();for(const i in t){if(!t.hasOwnProperty(i))continue;const r=t[i];r.isConnected||e.appendChild(r)}}(this.input.previewsContainer,i),t.files=e(n),this.input.prevFiles=n,this.sortable()}}l.prototype=Object.create(o.prototype),l.prototype.loadFiles=function(){const t=this.input.previewsContainer.querySelectorAll(".jet-form-builder-file-upload__file"),i=[];for(const e of t){this.addRemoveHandler(e);const t=e.dataset.file,r=e.querySelector(".jet-form-builder-file-upload__file-remove"),{fileName:n}=r.dataset;i.push([t,n])}i.length?Promise.allSettled(i.map((([e,t])=>new Promise(((i,r)=>{fetch(e).then((e=>e.blob())).then((e=>i(function(e,t){return new File([e],t,e)}(e,t)))).catch(r)}))))).then((t=>{const i=a(t.map((({value:e})=>e)));this.lock.current=!1,this.input.silenceSet(e(i))})).catch((()=>{this.lock.current=!1})):this.lock.current=!1},l.prototype.sortable=function(){jQuery(this.input.previewsContainer).unbind(),jQuery(this.input.previewsContainer).sortable({items:".jet-form-builder-file-upload__file",forcePlaceholderSize:!0}).bind("sortupdate",(()=>this.onSortCallback()))},l.prototype.onSortCallback=function(){const e=new DataTransfer,[t]=this.input.nodes,i=this.input.previewsContainer.querySelectorAll(".jet-form-builder-file-upload__file-remove");for(const r of i){const{fileName:i}=r.dataset;for(const r of t.files)r.name===i&&e.items.add(r)}this.input.value.current=e.files},l.prototype.addRemoveHandler=function(e){e.querySelector(".jet-form-builder-file-upload__file-remove").addEventListener("click",this.removeFile.bind(this))},l.prototype.getPreview=function(e){const t=this.input.previewsContainer.querySelector(`[data-file-name="${e.name}"]`);if(!t){const t=this.createPreview(e);return this.addRemoveHandler(t),t}return t.closest(".jet-form-builder-file-upload__file")},l.prototype.createPreview=function(e){const t=URL.createObjectURL(e);let{innerHTML:i}=this.input.template;i=i.replace("%file_url%",t),i=i.replace("%file_name%",e.name);const r=document.createElement("template");r.innerHTML=i;const n=r.content.firstChild;if(/^image\//.test(e.type)){const i=document.createElement("img");i.src=t,i.alt=e.name,n.prepend(i)}return n},l.prototype.removeFile=function({target:e}){const t=".jet-form-builder-file-upload__file-remove",{value:i}=this.input;e.matches(t)||(e=e.closest(t));const{fileName:r}=e.dataset,n=new DataTransfer;for(const e of i.current)r!==e.name&&n.items.add(e);i.current=n.files},l.prototype.getFileNode=function(e){const t=`data-file-name="${e}"`;return this.input.previewsContainer.querySelector(`.jet-form-builder-file-upload__file-remove[${t}]`).closest(".jet-form-builder-file-upload__file")};const s=l;function a(e){const t=new Set,i=[];for(const r of e)t.has(r.name)||(t.add(r.name),i.push(r));return i}const{addFilter:u}=JetPlugins.hooks;u("jet.fb.inputs","jet-form-builder/media-field",(function(e){return[n,...e]})),u("jet.fb.signals","jet-form-builder/media-field",(function(e){return[s,...e]}))})();