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