angular.module("app").controller("DropRemoveModalController",["$modalScope","$q","core","utils","state","delta","dropsToRemove",function(e,n,r,t,o,i,a){e.utils=t;var u={outbound:{},inbound:{}},findOrphanedLinks=function(e,n){var r={};if(!e||!e._internal||!e._internal.guid)return r;n||(n=0);var t=getOutboundLinks(e);if(t&&t.length)for(var o=0;o<t.length;o++){var i=t[o];if(!i._internal.selector.trim()||!i._internal.replaceMethod||"none"===i._internal.replaceMethod){var a=i._internal.guid,l=getInboundLinks(i);if(u.inbound[a]||(u.inbound[a]=l),1===l.length&&l[0]===e&&e!==i)r[i._internal.guid]={drop:i,level:n,parent:e},r=$.extend({},r,findOrphanedLinks(i,n+1))}}return r};e.getTemplateName=function(e){return t.Drops.getTemplateName(e)};var getInboundLinks=function(e){if(!e)return[];var n=e._internal.guid,o=!!u.inbound[n],i=[];return o?i=u.inbound[n]:(i=t.Drops.getInboundLinks(e),t.Drops.isDropletTemplate(e)&&r.settings.drops.forEach((function(n){t.Drops.isDropletInstance(n)&&n._internal.dropletId===e._internal.guid&&i.indexOf(n)<0&&i.push(n)}))),u.inbound[n]||(u.inbound[n]=i),i},getOutboundLinks=function(e){if(!e)return[];var n=e._internal.guid,r=u.outbound[n]?u.outbound[n]:t.Drops.getOutboundLinks(e);return u.outbound[n]||(u.outbound[n]=r),r},removeDropLinks=function(e,n){if(angular.isObject(n)||angular.isArray(n))for(var r of Object.keys(n))n[r]=removeDropLinks(e,n[r]);else if(angular.isString(n)){var t=new RegExp("\\s*\\[drop\\s+"+e._internal.guid+"\\s*\\]\\s*","g");return n.replace(t,"")}return n};e.isBreaking=function(n){if(!n)return!1;var r=[];r=getInboundLinks(n);for(var t=0;t<r.length;t++){var o=r[t]._internal.guid;e.removeable[o]&&(r.splice(t,1),t--)}return r.length||!1},e.onRemoveList=function(n){return e.remove.indexOf(n)>=0},e.addToRemoveList=function(n,r){n&&(!r&&e.isBreaking(n)||(e.remove.indexOf(n)<0&&e.remove.push(n),Object.values(e.removeable).filter((function(e){return e.parent===n})).forEach((function(n){e.addToRemoveList(n.drop,!1)}))))},e.subFromRemoveList=function(n){if(n){var r=e.remove.indexOf(n);r>=0&&e.remove.splice(r,1),Object.values(e.removeable).filter((function(e){return e.parent===n})).forEach((function(n){e.subFromRemoveList(n.drop)}))}},e.toggleRemove=function(n,r){e.onRemoveList(n.drop)?e.subFromRemoveList(n.drop):(!n.parent||e.onRemoveList(n.parent))&&e.addToRemoveList(n.drop,r)},e.removePossible=function(n){return!n.parent||e.onRemoveList(n.parent)},e.applyOperations=function(){if(!e.remove.length)return e.close();var o=[];e.remove.forEach((function(e){var n=e._internal.guid,a=t.Drops.getDropByGuid(n),u=r.settings.drops.indexOf(a);u>=0&&(r.settings.drops.splice(u,1),i.setRemoved(e));var l=getInboundLinks(e);l&&l.length&&(o=o.concat(l.map((function(n){return i.loadDrop(n).then((function(n){removeDropLinks(e,n),i.setChanged(n)}),(function(e){console.log("unlink error:",e)}))}))))})),n.all(o).then((function(){e.close(!0)}))},e.linkWarningHtml=function(e){var n=getInboundLinks(e),r="\t\t\t<div>\t\t\t\t<strong>Achtung, dieses Drop wird noch von "+n.length+'&nbsp;anderen Drops genutzt:</strong><br>\t\t\t\t<ul class="tleft m-05-top">';return n.forEach((function(e){r+='<li class="lh-15">'+e._internal.name+"</li>"})),r+="</ul></div>"};var l=[].concat(a);e.removeable=function(e){for(var n=[],r=0;r<e.length;r++){var t={drop:e[r],level:0,parent:null};n[t.drop._internal.guid]=t,n=$.extend({},n,findOrphanedLinks(t.drop,1))}return n}(l),e.remove=[],l.forEach((function(n){e.isBreaking(n)||e.addToRemoveList(n)}))}]);