(function(){"use strict";u8.Crm.Widgets.MissingRecords=function(e,t){this.Widget(e,$.extend(true,{},u8.Crm.Widgets.MissingRecords.defaults,t));this._isCreated=false;this._detectedInfoAreaIds={};this._updateDespammed=this._update.bind(this).despam(1);u8.services.events.bind("u8.services.crud.onDelete",this._onCrud.bind(this),this.id).bind("u8.services.crud.onUpdate",this._onCrud.bind(this),this.id).bind("u8.services.crud.onCreate",this._onCrud.bind(this),this.id);Function.chain(Function.chain.onDisposedAbort,this._using,this._createView).call(this,u8.tools.created.curry(this))};u8.Crm.Widgets.MissingRecords.makeClass({name:"MissingRecords",deriveFrom:u8.Base.Widgets.Widget,events:["Create","Update"]});u8.Crm.Widgets.MissingRecords.defaults={theme:{css:{root:"u8-missingrecords",groups:"u8-missingrecords-groups u8-vstack spaced-l",group:"u8-missingrecords-group",label:"u8-missingrecords-label",item:"u8-missingrecords-item"}}};$.extend(u8.Crm.Widgets.MissingRecords.prototype,{dispose:function(){if(this._isDisposed){return}u8.services.events.unbind("u8.services.crud.onDelete",this.id);u8.services.events.unbind("u8.services.crud.onUpdate",this.id);u8.services.events.unbind("u8.services.crud.onCreate",this.id);this.Widget_dispose()},update:function(){this._updateDespammed()},_update:function(){var e=this;this._createView(function(){e.raise("Update")})},isEmpty:function(){return this.$(this.element).children().length==0},_using:function(e){u8.using({texts:u8.Crm.Widgets.MissingRecords.getTextIds(this.value),templates:[{name:"u8-missingrecords",url:"~/crm/MissingRecords.utml.htm"}]},e)},_createView:function(e){var t=this;u8.Crm.Widgets.MissingRecords.createItemGroups(this.value,this.queryExecuteOptions,function(i,r){$.extend(t._detectedInfoAreaIds,r.infoAreaIds);var s={name:"u8-missingrecords",data:{css:t.theme.css,groups:r.groups}};t.$(t.element).utml(s,e)})},_onCrud:function(e,t){var i=u8.services.crud.getUidFromEventArgs(t);if(!i||!i.infoAreaId){return}for(var r in this._detectedInfoAreaIds){if(u8.services.schema.equalsInfoAreaIdOrBase(r,i.infoAreaId)){this._updateDespammed();return}}var s=this.queryExecuteOptions&&this.queryExecuteOptions.link;if(s&&s.infoAreaId&&u8.services.schema.equalsInfoAreaIdOrBase(s.infoAreaId,i.infoAreaId)){this._updateDespammed()}}});$.extend(u8.Crm.Widgets.MissingRecords,{createItemGroups:function(i,r,s){if(!i){return s&&s([])}var n=this;var u={};var d={};this._traverseItemGroups(i,null,function(t){var i=e(t);if(u[i]){return}var s=n._createCountQuery(t);if(!s){return}u[i]=function(e){n._executeCountQuery(s,r,function(e,t){d[i]=t}.chain(e))}});Function.join(u8.dict(u).values())(function(){var r={};var u=n._projectItemGroups(i,function(i){var s=e(i);if(!s||!d[s]){return undefined}$.extend(r,d[s].infoAreaIds);if(!d[s].valid){return undefined}var n=d[s].count;var u=i.minimum;var o=u-n;if(o<=0){return undefined}var a=i.textId&&$t(i.textId)||i.text;if(!a){return undefined}return{text:a.format({minimum:u,missing:o,count:n}),minimum:i.minimum,missing:o,count:n,id:t(i)}});s(n,{groups:u,infoAreaIds:r})})},getTextIds:function(e){var t=[];var i=u8._.attr(["textId",t.push.bind(t)]);this._traverseItemGroups(e,i,i);return t.filter(u8._.id).removeDuplicates()},_createCountQuery:function(e){if(e){if(e.queryName){return u8.create("QueryCommand",{name:e.queryName})}else if(e.query){return u8.create("QueryCommand",{statement:e.query})}}},_executeCountQuery:function(e,t,i){var r=this;e.execute($.extend({maxRows:1},t),function(e,t){var s={valid:false,infoAreaIds:{}};function n(e){if(e&&e.infoAreaId){s.infoAreaIds[e.infoAreaId]=true}}function u(e){if(e){u8.log.error("MissingRecords",e)}if(i){i(r,s)}}if(t.error||!t.resultSet){return u()}u8.each(t.resultSet.columns,n);var d=t.resultSet.rows;if(!d||d.length==0){return u()}u8.each(d[0]&&d[0].uids,n);var o=t.resultSet.columns;if(o.length==0){return u("No output fields found to read a record count from.")}if(o.length>1){u8.log.warn("MissingRecords","Too many output fields found to read a record count from, so only the first output field will be used.")}var a=o[0].infoAreaId;u8.using({schema:[a]},function(){var e=u8.services.schema.getField(a,o[0].fieldId);var t=u8.services.typeConverter.toNumber(d[0].values[0],{schema:e});if(isNaN(t)){return u("Value of the record count output field was not a number.")}else if(t<0){return u("Value of the record count output field was negative.")}s.valid=true;s.count=t;u()})})},_traverseItemGroups:function(e,t,i){if(e){e.each(function(e){if(t){t(e)}if(i&&e.items){e.items.each(i)}})}},_projectItemGroups:function(e,t){return e?e.mapAndFilter(function(e){var i=e.items?e.items.mapAndFilter(t,u8._.trueish()):undefined;return i&&i.length?$.extend({},e,{items:i,text:e.textId&&$t(e.textId)||e.text}):undefined},u8._.trueish()):e}});function e(e){return e.id||e.queryName||e.query}function t(e){return e.id||e.queryName}})();