!function(e){"use strict";var a=mediabayConfig2,t={ajaxurl:a.ajaxUrl,nonce:a.nonce,moveOneFile:a.moveOneFile,moveText:a.move,filesText:a.files,dragItem:null,init:function(){var a=this;this.appendDragger(),this.dragItem=e("#mediabay-dragger"),e(document).ajaxComplete(function(e,t,o){null!=o.data&&""!=o.data&&-1!=o.data.indexOf("action=query-attachments")&&a.dragAndDropMedia()})},appendDragger:function(){0===e("#mediabay-dragger").length&&e("body").append('<div id="mediabay-dragger" data-id="">'+this.moveOneFile+"</div>")},dragAndDropMedia:function(){var a=this,t=a.moveOneFile;e(".attachments-browser li.attachment").draggable({revert:"invalid",containment:"document",cursor:"move",cursorAt:{left:2,top:2},helper:function(){return e("<div></div>")},start:function(){var o=e(".attachments li.selected").length;o>0&&(t=a.moveText+" "+o+" "+a.filesText),e("body").addClass("cc_draging"),a.dragItem.html(t),a.dragItem.addClass("active")},stop:function(){e("body").removeClass("cc_draging"),a.dragItem.removeClass("active"),t=a.moveOneFile},drag:function(){var t=e(this).data("id");a.dragItem.data("id",t),a.dragItem.css({top:event.clientY-15,left:event.clientX-15})}}),e("li.category_item").droppable({accept:".attachments-browser li.attachment",hoverClass:"hover",classes:{"ui-droppable-active":"ui-state-highlight"},drop:function(){var t=e(this).attr("data-id"),o=a.getSelectedFiles();o.length?a.moveMultipleMedia(o,t):a.moveSingleMedia(t)}})},getSelectedFiles:function(){var a=e(".attachments li.selected"),t=[];return!!a.length&&(a.each(function(a,o){t.push(e(o).data("id"))}),t)},moveMultipleMedia:function(a,t){var o=this,i=e(".wpmediacategory-filter").val(),r={action:"mediabayAjaxMoveMultipleMedia",IDs:a,folderID:t};e.ajax({type:"POST",url:o.ajaxurl,cache:!1,data:r,success:function(a){e.parseJSON(a).result.forEach(function(a){o.updateCount(a.from,a.to),"all"!==i&&e('ul.attachments li[data-id="'+a.id+'"]').detach()}),o.disableBulkSelect()},error:function(e,a,t){console.log(t),console.log(a),console.log(e)}})},disableBulkSelect:function(){e(".wp-admin.upload-php .media-toolbar.wp-filter .media-toolbar-secondary .media-button.delete-selected-button").hasClass("hidden")||e(".wp-admin.upload-php .media-toolbar.wp-filter .media-toolbar-secondary .media-button.select-mode-toggle-button").trigger("click")},moveSingleMedia:function(a){var t=this,o=t.dragItem.data("id"),i=e('.attachment[data-id="'+o+'"]'),r=e(".wpmediacategory-filter").val();if(r=e(".category_item.active").attr("data-id"),"all"===a||a==r)return!1;t.startPreloader();var n={action:"mediabayAjaxGetTermsByMedia",nonce:t.nonce,ID:o};e.ajax({type:"POST",url:t.ajaxurl,cache:!1,data:n,success:function(n){var l=e.parseJSON(n);"no"===l.error?t.moveSingleMediaAjaxProcess(l.terms,a,o,r,i):t.stopPreloader()},error:function(e,a,t){console.log(t),console.log(a),console.log(e)}})},moveSingleMediaAjaxProcess:function(a,t,o,i,r){var n=this,l=Array.from(a,e=>e.term_id);if(l.includes(parseInt(t)))n.stopPreloader();else{var d={};d[o]={menu_order:0};var c={action:"mediabayAjaxMoveSingleMedia",attachments:d,mediaID:o,folderID:t};e.ajax({type:"POST",url:n.ajaxurl,cache:!1,data:c,success:function(a){"no"===e.parseJSON(a).error&&(e.each(l,function(e,a){n.updateCount(a,t)}),"all"!==i||l.length||n.updateCount(-1,t),-1===parseInt(i)&&n.updateCount(-1,t),"all"!==i&&r.detach()),n.stopPreloader()},error:function(e,a,t){console.log(t),console.log(a),console.log(e)}})}},startPreloader:function(){e(".mediabay_be_loader").addClass("active")},stopPreloader:function(){e(".mediabay_be_loader").removeClass("active")},updateCount:function(a,t){if((a=parseInt(a))!==(t=parseInt(t))){if(a){var o=e('.wrap-all ul li.category_item[data-id="'+a+'"] .cc_count').text();(o=parseInt(o)-1)?e('.wrap-all ul li.category_item[data-id="'+a+'"] .cc_count').text(o):e('.wrap-all ul li.category_item[data-id="'+a+'"] .cc_count').text(0)}if(t){var i=e('.wrap-all ul li.category_item[data-id="'+t+'"] .cc_count').text();i=parseInt(i)+1,e('.wrap-all ul li.category_item[data-id="'+t+'"] .cc_count').text(i)}}}};e(document).ready(function(){t.init()})}(jQuery);