(function(){"use strict";u8.Crm.FilterSets=function(e){$.extend(this,e);if(!this.containerName){this.containerName="FilterSets"}if(!this.items){this.items=null}if(!this.defaultFilterSet){this.defaultFilterSet=this.createDefaultFilterSet()}this.bind()};u8.Crm.FilterSets.makeClass({name:"FilterSets",events:["Save","Delete"]});$.extend(u8.Crm.FilterSets.prototype,{findById:function(e){if(!this.items){return null}for(var t=0;t<this.items.length;t++){if(this.items[t].id===e){return this.items[t]}}return null},dispose:function(){if(this._isDisposed){return}this.unbind();delete this.items;this._isDisposed=null},load:function(e){var t=this;function i(i,r){if(t._isDisposed){return}var s=r.response?r.response.filterSets:null;t._buildFilterSets(s);t.items=s;r={};if(typeof e=="function"){e(t,r)}}u8.request("QueryChannel.GetFilterSets",{filterSetContainerName:this.containerName},i,true)},save:function(e,t){if(e.isReadOnly){$error("This filter set is marked as read only. It cannot be saved!")}var i=this;function r(e,r){var s=r.response;var n=s?i._buildFilterSet(s.filterSet):null;r={filterSet:n,error:r.error,isUpdate:s?s.isUpdate:false,isNew:s?s.isNew:false};if(n){i._updateItems(n)}if(typeof t=="function"){t(i,r)}i.raise("Save",r)}var s=e.toTransport();s._xsiElementName="FilterSet";u8.request("QueryChannel.SaveFilterSet",{filterSetContainerName:this.containerName,filterSet:s},r)},delete_:function(e,t){var i=this;function r(r,s){var n=s.response?s.response.success:false;s={success:n};if(i.items){for(var a=0;a<i.items.length;a++){if(i.items[a].id==e){i.items.splice(a,1);break}}}if(typeof t=="function"){t(i,s)}i.raise("Delete",s)}u8.request("QueryChannel.DeleteFilterSet",{filterSetContainerName:this.containerName,filterSetId:e},r)},createDefaultFilterSet:function(){var e={};var t=[];var i=this.infoAreaIds;if(i){for(var r=0;r<i.length;r++){t.push(new u8.Crm.FilterSetItem({infoAreaId:i[r]}))}}e.items=t;return new u8.Crm.FilterSet(e)},createFilterSet:function(e){return this._buildFilterSet(e)},_buildFilterSets:function(e){if(!e){return}for(var t=0;t<e.length;t++){e[t]=this._buildFilterSet(e[t])}},_buildFilterSet:function(e){if(!e){return e}e=new u8.Crm.FilterSet(e);if(!e.items){return e}for(var t=0;t<this.infoAreaIds.length;t++){e.items[t]=new u8.Crm.FilterSetItem(e.items[t])}if(this.infoAreaIds){var i,r=[];for(t=0;t<this.infoAreaIds.length;t++){var s=this.infoAreaIds[t];var n=e.indexOfInfoAreaId(s);if(n<0){i=new u8.Crm.FilterSetItem({infoAreaId:s,visible:false})}else{i=e.items[n]}r.push(i)}e.items=r}return e},_updateItems:function(e){if(!this.items){this.items=[]}var t=false;for(var i=0;i<this.items.length;i++){var r=this.items[i];if(r.id==e.id){this.items[i]=e;t=true;break}}if(!t){this.items.push(e);this.items.sort(function(e,t){return(e.name||"").localeCompare(t.name)})}}});u8.Crm.FilterSets._xsiNoSerialize=true})();