jQuery(document).ready(function(e){e("#mlo-replace-file-field").on("change",function(e){handleFileSelect(this.files[0])});e("#mlo-replace-file-btn").on("click",function(e){e.preventDefault();uploadFile()});e("#mlo-replace-clear-btn").on("click",function(e){e.preventDefault();resetFileReplacer()});const l=document.getElementById("mlo-file-drop-area");if(!l){return}["dragenter","dragover","dragleave","drop"].forEach(function(e){l.addEventListener(e,preventDefaults,false)});function preventDefaults(e){e.preventDefault();e.stopPropagation()}["dragenter","dragover"].forEach(function(e){l.addEventListener(e,highlight,false)});["dragleave","drop"].forEach(function(e){l.addEventListener(e,unhighlight,false)});function highlight(){l.classList.add("drag-active")}function unhighlight(){l.classList.remove("drag-active")}l.addEventListener("drop",handleDrop,false);function handleDrop(e){const l=e.dataTransfer;const t=l.files[0];handleFileSelect(t)}function resetFileReplacer(l){if(l){e(".mlo-replace-file-error").removeClass("hidden");e(".mlo-replace-file-error").text(l)}else{e(".mlo-replace-file-error").addClass("hidden")}e("#mlo-replace-file-btn").prop("disabled",true);e("#mlo-replace-clear-btn").prop("disabled",true);e("#mlo-replace-file-field").val("");e(".mlo-replace-file-preview").html("");e(".label-text").show();e(".mlo-drop-icon").show();e(".mlo-supported-text").show()}function handleFileSelect(l){e(".mlo-replace-file-error").addClass("hidden");if(!l)return;if(MLOAttachmentEdit.mimeType!==l.type){resetFileReplacer(MLOAttachmentEdit.i18n.mimeTypeError);return}if(l.size>MLOAttachmentEdit.maxFileSize){resetFileReplacer(MLOAttachmentEdit.i18n.maxFileSizeError);return}if(l instanceof File){const e=new DataTransfer;e.items.add(l);document.getElementById("mlo-replace-file-field").files=e.files}e("#mlo-replace-file-btn").prop("disabled",false);e("#mlo-replace-clear-btn").prop("disabled",false);e(".label-text").hide();e(".mlo-drop-icon").hide();e(".mlo-supported-text").hide();const t=l.type;const a=t.startsWith("image/");let r=a?"<img src='"+URL.createObjectURL(l)+"' />":"";r+="<span>"+l.name+"</span>";e(".mlo-replace-file-preview").html(r)}function uploadFile(){const l=new FormData;l.append("action","mlo_replace_file");l.append("attachment_id",MLOAttachmentEdit.attachmentId);l.append("file",e("#mlo-replace-file-field")[0].files[0]);l.append("nonce",MLOAttachmentEdit.nonce);e(".mlo-svg-loader").show();jQuery.ajax({url:MLOAttachmentEdit.ajaxURL,type:"POST",data:l,processData:false,contentType:false,success:function(l){e(".mlo-svg-loader").hide();if(l.success){window.location.reload()}else{e(".mlo-replace-file-error").removeClass("hidden");e(".mlo-replace-file-error").text(l.message)}},error:function(l,t,a){e(".mlo-svg-loader").hide();resetFileReplacer(l.responseJSON?l.responseJSON.message:MLOAttachmentEdit.i18n.replaceFileError)}})}});