(function(){const a=function(){this.type="Replacement"};const b=function(c,f){const e=typeof f==="boolean"&&f?1:-1;const d=sp.moment.make(c.endDate).add(e,"days");c.endDate=sp.moment.formatAsLocalDate(d)};window.ReplacementService=SilverpeasClass.extend({initialize:function(c){this.context=c;const d=webContext+"/services/workflow/"+this.context.componentInstanceId+"/replacements";this.baseAdapter=RESTAdapter.get(d,a)},getTools:function(){return{newReplacementEntity:function(){return this.getTools().extractReplacementEntityData()}.bind(this),extractReplacementEntityData:function(e){e=e?e:{};const c=sp.moment.atZoneIdSimilarLocal(moment(),this.context.currentUser.zoneId);const d=new a();d.uri=e.uri;d.incumbent=e.incumbent?e.incumbent:{};d.substitute=e.substitute?e.substitute:{};d.startDate=e.startDate?e.startDate:c.format();d.endDate=e.endDate?e.endDate:c.format();d.workflowId=e.workflowId?e.workflowId:this.context.componentInstanceId;return d}.bind(this)}},getAllAsIncumbent:function(c){return this.baseAdapter.find({incumbent:c}).then(function(d){d.forEach(b);return d})},getAllAsSubstitute:function(c){return this.baseAdapter.find({substitute:c}).then(function(d){d.forEach(b);return d})},getAll:function(c){return this.baseAdapter.find().then(function(d){if(c){return d.filter(function(e){b(e);return e.incumbent.id!==c&&e.substitute.id!==c})}})},saveReplacement:function(c){const d=this.getTools().extractReplacementEntityData(c);d.startDate=sp.moment.formatAsLocalDate(d.startDate);d.endDate=sp.moment.formatAsLocalDate(d.endDate);b(d,true);return !d.uri?this.baseAdapter.post(d):this.baseAdapter.put(d.uri,d)},deleteReplacement:function(c){return this.baseAdapter.remove(c.uri)}})})();