(function(){"use strict";u8.Crm.AddressCheck=function(){this.id="AddressCheck"};u8.Crm.AddressCheck.defaults={automaticSelectionMessage:"TXT_AddressCheck_8",automaticSelectionMessageInvalid:"TXT_AddressCheck_9"};$.extend(u8.Crm.AddressCheck.prototype,{dispose:function(){if(this._isDisposed){return}this._isDisposed=true},execute:function(e,i){var s=$.Deferred();function r(r){var n;if(r.error){n={fields:e.fields,uid:e.uid,action:"error"}}else if(r.cancel){n={fields:e.fields,uid:e.uid,action:"cancel"}}else{n={fields:r.fields,uid:e.uid,action:"select"}}if(i){i(n)}if(r.error){return s.reject(n)}else{return s.resolve(n)}}if(!e.uid){e.uid={infoAreaId:e.infoAreaId}}if(!this._validateOptions(e)){return r({error:true})}var n;var d;var t;var a=e.uid;var u=u8.Crm.AddressCheck.defaults,o="automaticSelectionMessage"in e?e.automaticSelectionMessage:u.automaticSelectionMessage,f="automaticSelectionMessageInvalid"in e?e.automaticSelectionMessageInvalid:u.automaticSelectionMessageInvalid;function l(){u8.using({textIds:[o,f],schema:[a.infoAreaId]},c)}function c(){var i=u8.session.webConfigValues.getStringArray("Expand.AddressCheck.InfoAreas",[]);if(e.force!==true&&i.indexWhere(u8.services.schema.equalsInfoAreaIdOrBase.curry(a.infoAreaId).bind(u8.services.schema))==-1){r({cancel:true});return}h()}function h(){var i={uid:a,fields:e.fields.map(function(e){return{field:"fieldId"in e?e.fieldId:e.field,value:e.value}})};u8.request("DataQualityChannel.FindAddress",i,function(e,i){if(i.error){r({error:true});return}var s=i&&i.response;var t=s&&s.result;n=t&&t.addresses;d=t&&t.fieldIds;if(!n||!n.length){r({cancel:true});return}v()})}function v(){var e={options:{fieldGroupName:a.infoAreaId+".AddressCheck",includeLinkedInfoAreas:false}};u8.request("ListChannel.GetListConfiguration",e,function(i,s){var r=s.response,n=r&&r.fieldControl;if(n){t=[];n=new u8.Crm.FieldControl(n);n.eachField(function(e){t.push(e.fieldId)});C()}else{if(s.error){s.error.handled=true}u8.log.warn("FieldGroup '"+e.options.fieldGroupName+"' not found, using MC fields instead.");u8.request("DataQualityChannel.GetAddressCheckFields",{infoAreaId:a.infoAreaId},function(e,i){if(i.error){i.error.handled=true}var s=i.response;if(s&&s.length){t=s}else{u8.log.warn("No MC fields defined, using input fields instead.");t=(d||[]).map(u8._.attr("fieldId"))}C()})}})}function C(){n.forEach(function(e){var i=e.fields;t.forEach(function(e){if(!i.find(u8._.eq("fieldId",e))){i.push({fieldId:e,value:""})}});e.fields.sort(u8._.cmp("fieldId"))});m()}function m(){if(n.length==1&&n[0].score==1e3){if(n[0].badFields&&n[0].badFields.length){var e=n[0].badFields.map(function(e){var i=u8.services.schema.getField(a.infoAreaId,e.fieldId);return i&&i.label||undefined}).clean().join(", ");if(f!==false){if(u8.services.texts.isTextId(f)){f=$t(f)}u8.services.notification.warn(f.format({fields:e}))}}else if(o!==false){u8.services.notification.info(o)}r({fields:n[0].fields});return}p()}function p(){var i={addresses:n,columnFieldIds:t,uid:a,fieldsToCheck:e.fields,onSelect:function(e,i){r({fields:i.fields})},onCancel:function(){r({cancel:true})}};var s=e.element;if(s){i.parent=$p(s);u8.widget("AddressCheckView",s,i)}else{u8.Crm.Widgets.AddressCheckView.showAsPopup(i)}}l();return s.promise()},_validateOptions:function(e){var i=[];if(!e){i.push("Required options missing")}else{if(!e.uid||!e.uid.infoAreaId){i.push("Missing info area id")}if(!e.fields||!e.fields.length){i.push("Missing fields")}}if(!i.length){return true}u8.log.warn("AddressCheck was not executed due to: #) "+i.join(" #) ")+".");return false}});u8.Crm.AddressCheck.makeClass({name:"AddressCheck"});u8.services.add({name:"addressCheck",scope:"Session",ctor:u8.Crm.AddressCheck})})();