angular.module("app").controller("InsertConditionsModalController",["$modalScope","$element","$timeout","core","utils","state","drop",function(e,t,n,o,i,r,l){e.pickerActive=!1,e.pickerSelectionMode=!0,e.utils=i,e.state=r,e.core=o,e.conditionSuggestions=null,e.drop=$.extend(!0,{},l),e.insertConditionConfig=[{name:"Selektor",description:"Ein Selektor der die Position des Drops innerhalb des Templates angibt.",type:"selector-input",property:"selector"},{name:"Einfügemethode",description:"Bestimme wie das Drop an der durch den Selektor ausgewählten Stelle eingefügt werden soll.",type:"imagepicker",optionImageRoot:"admin",property:"replaceMethod",width:375,options:[{value:"before",label:"Vorstellen",detail:"Platziert das Drop vor dem ausgewählten Element",image:"img/insert-method-before.png"},{value:"after",label:"Anstellen",detail:"Platziert das Drop nach dem ausgewählten Element",image:"img/insert-method-after.png"},{value:"prepend",label:"Vorhängen",detail:"Hängt das Drop als ersten Inhalt in das ausgewählte Element ein",image:"img/insert-method-prepend.png"},{value:"append",label:"Anhängen",detail:"Hängt das Drop als letzten Inhalt an das ausgewählte Element an",image:"img/insert-method-append.png"},{value:"html",label:"Inhalt Ersetzen",detail:"Ersetzt den Inhalt des ausgewählten Elementes durch den Inhalt des Drops",image:"img/insert-method-html.png"},{value:"replaceWith",label:"Ersetzen",detail:"Ersetzt das ausgewählte Element komplett durch den Inhalt des Drops",image:"img/insert-method-replaceWith.png"},{value:"",label:"Keine",image:"img/insert-method-none.png"}]}],e.linkingDrops=i.Drops.getInboundLinks(l),e.linkedDrops=i.Drops.getOutboundLinks(l),e.validateConditionals=function(e){e._internal&&i.validateConditionals(e._internal.conditionalSettings)};var a=e.drop._internal.guid,setPickerParam=function(e){var t=(e=e.replace(/(?:\?|&)zonePicker(?:=[^&]+)*/gi,"")).split("#");return t[0]=t[0]+(t[0].indexOf("?")>=0?"&zonePicker=":"?zonePicker=")+a,t.join("#")};e.initPickerOptions=function(){var t=e.drop._internal.selectorUrl?o.shopURL+e.drop._internal.selectorUrl:o.shopURL;e.selectorOption={selector:e.drop._internal.selector,replaceMethod:e.drop._internal.replaceMethod,visible:!1,frameUrl:setPickerParam(t),inputUrl:t}},e.setPickerVisible=function(t){e.pickerActive=t},e.togglePickerVisibility=function(){e.pickerActive=!e.pickerActive},e.setReplaceMethod=function(t){e.selectorOption.replaceMethod=t},e.$watch("pickerActive",(function(i){var updateUrl=function(t,n){e.$apply((function(){var o=setPickerParam(t);o!==t&&(n.location.href=o),e.selectorOption.inputUrl=t.replace(/(?:\?|&)zonePicker(?:=[^&]+)*/gi,""),e.frameLoading=!0}))};i&&(e.initPickerOptions(),n((function(){e.frameLoading=!0;var n=t.find("iframe.picker-frame"),i=n.get(0).contentWindow||n.get(0);n.load((function(){if(updateUrl(i.location.href,i),e.$apply((function(){e.frameLoading=!1})),e.drop&&e.drop._internal&&e.drop._internal.selector){var t=e.drop._internal.selector.split(",")[0].trim(),n=o.templateZones[t.replace("@","").replace(/\(\d+\)/g,"($index)")]||null;i.postMessage({type:"kk_dropper.presetResult",zone:n&&n.zone?n.zone:null,selector:t,replaceMethod:e.drop._internal.replaceMethod},"*")}i.onbeforeunload=function(t){e.$apply((function(){e.frameLoading=!1})),i.document.activeElement&&i.document.activeElement.href&&updateUrl(i.document.activeElement.href,i)}}))})))})),window.onmessage=function(t){t&&t.data&&t.data.type&&("kk_dropper.pickerResult"===t.data.type&&e.$apply((function(){e.selectorOption.selector=t.data.selector,e.selectorOption.replaceMethod=t.data.method,e.fetchConditionalSuggestions(e.selectorOption.inputUrl),e.applySelectorChanges()})),"kk_dropper.selectionMode"===t.data.type&&e.$apply((function(){e.pickerSelectionMode=t.data.active})))},e.updatePickerUrl=function(){e.selectorOption.frameUrl=setPickerParam(e.selectorOption.inputUrl)},e.toggleSelectionMode=function(){e.pickerSelectionMode=!e.pickerSelectionMode;var n=t.find("iframe.picker-frame");(n.get(0).contentWindow||n.get(0)).postMessage({type:"kk_dropper.selectionMode",active:e.pickerSelectionMode},"*")},e.fetchConditionalSuggestions=function(t){let n=o.shopURL+t.replace(o.shopURL,"");$.ajax({type:"POST",dataType:"JSON",url:n,data:{kk_dropper_condition_suggestions:"true",kk_dropper_admin_pass:o.adminSecret},success:function(t){t&&t.length&&e.$apply((function(){e.conditionSuggestions={url:n,conditionals:t}}))},error:function(t){console.log("could not fetch condition suggestions"),e.$apply((function(){e.conditionSuggestions=null}))}})},e.applySelectorChanges=function(){var t=e.drop._internal;$.extend(!0,t,{selector:e.selectorOption.selector,replaceMethod:e.selectorOption.replaceMethod,selectorUrl:e.selectorOption.inputUrl.replace(o.shopURL,"")}),e.pickerActive=!1},e.drop&&e.drop._internal&&angular.isString(e.drop._internal.selectorUrl)&&e.fetchConditionalSuggestions(e.drop._internal.selectorUrl)}]);