angular.module("app").service("delta",["core","modal","$timeout","$q",function(e,n,r,t){if(window.dropperDelta)return window.dropperDelta;var i=window.dropperDelta={changed:[],removed:[],eventListeners:[],on:function(e,n){angular.isString(e)&&(e=(e=e.split(",")).map((function(e){return e.trim()}))).length&&e.forEach((function(e){i.eventListeners[e]||(i.eventListeners[e]=[]);var r=i.eventListeners[e];r.indexOf(n)>=0||r.push(n)}))},notify:function(e,...n){if(i.eventListeners[e]){var r=i.eventListeners[e];r&&r.length&&r.forEach((function(r){r.apply({event:e},n)}))}},isLoaded:function(n){var r=null;if(angular.isString(n))for(var t=0;t<e.settings.drops.length;t++){var i=e.settings.drops[t];if(i&&i._internal&&i._internal.guid===n){r=i;break}}else r=n;return!(!r||r.__partial)},loadDrop:function(e,n){return t((function(r,t){i.loadDrops([e],n).then((function(e){if(angular.isArray(e)&&1===e.length)return r(e[0]);t("Could not load drop, because it doesnt exist remotely anymore!")}),(function(e){t(e)}))}))},loadDrops:function(n,r){return t((function(t,a){var o=[];if(n.forEach((function(e){e&&e._internal&&e._internal.guid&&(!i.isLoaded(e)||r&&!i.isChanged(e))&&o.push(e._internal.guid)})),!o.length)return t(n);console.log("fetching drops from remote",o),$.ajax({type:"POST",url:e.shopURL,data:{kk_dropper_admin_pass:e.adminSecret,kk_dropper_get_drops_data:o.join(",")},success:function(e){var r={};e.forEach((function(e){e&&e._internal&&e._internal.guid&&(r[e._internal.guid]=e)}));for(var i=0;i<n.length;i++){var a=n[i],o=a._internal.guid,s=r[o]||null;if(s){var d=s._internal.revision;delete s._internal,$.extend(!0,a,s),a._internal.revision=d,delete a.__partial,delete r[o]}else n.splice(i,1),i--}t(n)},error:function(e){a(e)}})}))},findDrop:function(n){return n&&e.settings.drops.find((function(e){var r=angular.isString(n)?n:n._internal.guid;return e._internal.guid===r}))||null},isStoredDrop:function(e){return!!i.findDrop(e)},getLoaded:function(){for(var n={},r=0;r<e.settings.drops.length;r++){var t=e.settings.drops[r],a=t._internal.guid;i.isLoaded(a)&&(n[a]=t)}return n},setChanged:function(e,n){angular.isDefined(n)||(n=!0);var r=angular.isString(e)?e:angular.isObject(e)?e._internal.guid:null;if(r){var t=i.changed.indexOf(r),a=t>=0;if(n&&!a){if(!i.isStoredDrop(r))return;i.changed.push(r)}!n&&a&&(i.changed.splice(t,1),i.notify("unchange",r)),n&&i.notify("change",r)}},setAllChanged:function(){e.settings.drops.forEach((function(e){i.setChanged(e)}))},resetChanged:function(){i.changed=[]},isChanged:function(e){var n=angular.isString(e)?e:angular.isObject(e)?e._internal.guid:null;return i.changed.indexOf(n)>=0},getChanged:function(){for(var n={},r=0;r<e.settings.drops.length;r++){var t=e.settings.drops[r],a=t._internal.guid;i.isChanged(a)&&(n[a]=t)}return n},setRemoved:function(e){var n=angular.isString(e)?e:angular.isObject(e)?e._internal.guid:null;n&&(i.removed.indexOf(n)<0&&i.removed.push(n),i.notify("remove",n),i.setChanged(n,!1))},setAllRemoved:function(){e.settings.drops.forEach((function(e){i.setRemoved(e)}))},resetRemoved:function(){i.removed=[]},getRemoved:function(){return i.removed},reset:function(){i.resetRemoved(),i.resetChanged()},saveDeltaSettings:function(r,a){return angular.isDefined(r)||(r=!1),a=a||"",t((function(t,o){e.settings.dropperVersion=e.dropperVersion,e.latestRevision={revision:e.latestRevision.revision+1,user:e.adminUser},e.settings.meta=e.latestRevision;var s=JSON.parse(angular.toJson(e.settings));delete s.drops;var d={changed:JSON.parse(angular.toJson(i.getChanged())),removed:i.getRemoved(),available:e.settings.drops.map((function(e){return e._internal.guid})),settings:s};$.ajax({type:"POST",url:e.shopURL,dataType:"JSON",data:{kk_dropper_save_delta:angular.toJson(d),kk_dropper_save_delta_force:r?"1":"",kk_dropper_save_note:angular.toJson(a),kk_dropper_save_user:e.adminUser,kk_dropper_admin_pass:e.adminSecret},success:function(e){if(console.log("saved: ",e),i.resetRemoved(),e&&angular.isObject(e.revisions))for(var r in e.revisions){var s=i.findDrop(r);s&&(s._internal.revision=e.revisions[r],i.setChanged(s,!1))}e&&e.conflicts&&e.conflicts.length?n.show("SaveConflictModal",(function(n){if(!angular.isArray(n))return t(e);n.forEach((function(e){var n=i.findDrop(e._internal.guid);if(n){for(var r in n)0!==r.indexOf("$")&&delete n[r];$.extend(!0,n,e),i.setChanged(n,!1)}})),i.changed.length?i.saveDeltaSettings(!0,a).then((function(e){t(e)}),(function(e){o(e)})):t(e)}),{conflicts:e.conflicts}):t(e)},error:function(e){o(e)}})}))}};return window.dropperDelta}]);