(function(){u8.Crm.RecordDeselector=function(i){$.extend(this,i)};$.extend(u8.Crm.RecordDeselector.prototype,{execute:function(i){if(!i){i=this._nop}Function.chain(this._loadSchema.bind(this),this._getLinksToRemove.bind(this),this._getFieldsToClear.bind(this),this._execute.bind(this))(i)},_execute:function(i){if(this.adapter){this._executeWithAdapter(i)}else if(u8.isValidRecordUid(this.uid)){this._executeWithRecord(i)}return i()},_loadSchema:function(i){u8.using({schema:this.infoAreaId},i)},_getFieldsToClear:function(i){this._fieldsToClear={};if(typeof this.setFields=="object"){$.extend(this._fieldsToClear,this.setFields)}var e={};var n=this._removeLinks;if(n){var t=u8.services.schema.getInfoArea(this.infoAreaId);for(var s=0;s<n.length;s++){var d=n[s],l=t.getLinkField(d.infoAreaId,d.linkId);if(!l){continue}e[l.fieldId]=null;var r=l.getLinkFields();for(var a=0;a<r.length;a++){e[r[a].sourceFieldId]=null}var o=l.getRelatedLinkFields().filter(u8._.neq(l));for(a=0;a<o.length;a++){var h=o[a];var f=!!n.find(function(i){return i.infoAreaId==h.link.destinationInfoAreaId&&u8.services.schema.equalsLinkId(i.linkId,h.link.linkId)});if(f){continue}if(h.isParentLinkOf(l)){r=h.getLinkFields();for(var c=0;c<r.length;c++){delete e[r[c].sourceFieldId]}}}}}if(this.linkFieldId&&!this._fieldsToClear.hasOwnProperty(this.linkFieldId)){e[this.linkFieldId]=null}if(this.linkFieldSchema&&!this._fieldsToClear.hasOwnProperty(this.linkFieldSchema.fieldId)){e[this.linkFieldSchema.fieldId]=null}for(var u in e){if(!this._fieldsToClear.hasOwnProperty(u)){this._fieldsToClear[u]=e[u]}}i()},_getLinksToRemove:function(i){this._removeLinks=[];if(this.linkIds){this._removeLinks.push({infoAreaId:this.infoAreaId,linkId:u8.services.schema.validateLinkId(linkIds[0])})}if(this.linkId){this._removeLinks.push({infoAreaId:this.infoAreaId,linkId:u8.services.schema.validateLinkId(this.linkId)})}if(this.linkFieldSchema){var e=this.linkFieldSchema.link;if(e){this._removeLinks.push({infoAreaId:e.destinationInfoAreaId,linkId:e.linkId})}var n=this.linkFieldSchema.getRelatedLinkFields()||[];for(var t=0;t<n.length;t++){var s=n[t];if(s.fieldId==this.linkFieldSchema.fieldId){continue}if(this.linkFieldSchema.isParentLinkOf(s)){this._removeLinks.push({infoAreaId:s.link.destinationInfoAreaId,linkId:s.link.linkId})}}}if(this.linkFieldId){s=u8.services.schema.getField(this.infoAreaId,this.linkFieldId);e=s.link;this._removeLinks.push({infoAreaId:e.destinationInfoAreaId,linkId:e.linkId})}return i()},_executeWithAdapter:function(i){var e=this.adapter;for(var n=0;n<this._removeLinks.length;n++){e.removeLink(this._removeLinks[n])}for(var t in this._fieldsToClear){e.setValue(t,this._fieldsToClear[t]||null)}},_executeWithRecord:function(i){var e=this.uid;var n=this._fieldsToClear.map(function(i,e){return{fieldId:e,value:i||""}});u8.services.crud.update({uid:e,fields:n},i)},_nop:function(){}});u8.Crm.RecordDeselector.makeClass({name:"RecordDeselector"})})();