(function(){"use strict";u8.Crm.ResultSetSaveProvider=function(e){$.extend(this,u8.Crm.ResultSetSaveProvider.defaults,e);if(!this.resultSet){$error("ResultSetSaveProvider needs a 'resultSet'")}};u8.Crm.ResultSetSaveProvider.makeClass({name:"ResultSetSaveProvider"});u8.Crm.ResultSetSaveProvider.defaults={_xsiNoSerialize:true};$.extend(u8.Crm.ResultSetSaveProvider.prototype,{save:function(e,i){var r=this;var a=this.resultSet;var s=e.row;var t;function u(i){var r={type:"update",fields:[],uid:i,options:{}};if(e.enableCalculations=="disable-if-live"){r.options.enableCalculations=!u8.services.schema.getEnableLiveCalculations(i.infoAreaId)}return r}function n(){var e,i,r,t={};for(e=0;e<s.values.length;e++){var n=s.values[e];var l=s._originalValues?s._originalValues[e]:n;if(n==l){continue}i=a.columns[e];r=t[i.alias];if(!r){var d=a.indexOfRecord(i.alias);if(s.uids[d]){r=t[i.alias]=u(s.uids[d])}}if(r){r.fields.push({field:i.fieldId,value:n})}}var f=s.__extendedSaveValues||{};for(var o in f){var v=f[o];var c=v.businessObject;c.getModifiedFieldIds().each(function(e){r=t[o];if(!r){r=t[o]=u(c.uid)}r.fields.push({field:e,value:c.get(e)})});if(v.links.length>0){r=t[o];if(!r){r=t[o]=u(c.uid)}r.options.links=v.links}}return u8.dict(t).values()}var l=n();if(l.length==0){if(i){i(this,{})}return false}this._preExecute(e,l,d);function d(){u8.services.crud.executeBatch({requests:l,options:{resultSet:a}},f)}function f(e,i){if(i.error){return o({error:i.error})}t=u8.Crm.Crud.buildMessages(i);if(i.refreshRecommended){return a.load({},o)}r._reReadRow(s,o)}function o(e){if(!i){return}if(!e){e={}}if(t){e.messages=t}i(r,e)}return true},_reReadRow:function(e,i){var r=this.resultSet;function a(){var i={};var a=r.columns;for(var s=0;s<a.length;s++){var t=a[s];if(!i[t.alias]){var u=r.indexOfRecord(t.alias),n=e.uids[u];if(!u8.isValidRecordUid(n)){continue}i[t.alias]={type:"read",fields:[],alias:t.alias,uid:n}}i[t.alias].fields.push(t.fieldId)}return u8.dict(i).values()}function s(i){for(var a=0;a<i.length;a++){var s=i[a];if(s.type!="read"){continue}var t=s.alias;var u=s.businessObject;if(!u){continue}var n=r.indexOfRecord(t);e.uids[n]=u.uid;var l=e.values;for(var d=0;d<l.length;d++){var f=r.columns[d];if(f.alias!=t){continue}var o=u.values[f.fieldId];if(typeof o!="undefined"){l[d]=o}}}}var t=a();u8.services.crud.executeBatch({requests:t},function(e,r){if(r.error){return i({error:r.error})}s(r.results);i()})},_preExecute:function(e,i,r){var a=[this._executeAddressCheck.bind(this,e,i)];Function.chain(a)(r)},_executeAddressCheck:function(e,i,r){var a=this.resultSet;var s=this.resultSet.aliasToInfoAreaId(this.resultSet.rootAlias);var t=i.find(function(e){return u8.services.schema.equalsInfoAreaIdOrBase(e.uid.infoAreaId,s)});if(!t){r();return}var u=t.fields.map(function(e){return{fieldId:e.field,value:e.value}});u8.services.addressCheck.execute({uid:t.uid,fields:u},function(i){if(i.action!="select"){r();return}var s=a.rows.indexOf(e.row);var u=a.setValue.bind(a,s);var n=a.indexOfColumn.bind(a,a.rootAlias);var l=t.fields;i.fields.forEach(function(e){var i=n(e.fieldId);if(i!=-1){u(i,e.value)}var r=l.find(u8._.eq("field",e.fieldId));if(!r){r={field:e.fieldId};l.push(r)}r.value=e.value});r()})}})})();