(function(){"use strict";u8.Crm.RecordSets=function(){this._nextId=1;this._recordSets={};this.commandBindings={"Application/ExecuteActionOnRecordSet":this._onCommandExecuteAction.bind(this),"RecordSet/WriteToLog":this._onCommandWriteToLog.bind(this)}};$.extend(u8.Crm.RecordSets.prototype,{create:function(){var e=this;var r="rs"+this._nextId;var t=new u8.Crm.RecordSet;t._id=r;t.dispose=function(){e._onRecordSetDispose(t)};this._recordSets[r]=t;this._nextId++;return t},get:function(e){return this._recordSets[e]},executeAction:function(e,r){var t=this.get(e);if(!t){return}var n=r.multiRecordAction;var o=t.getCount();if(o==1||!n){var i=t.getInfoAreaIds();var s=i[0];var c=t.getRecordId(s,0);u8.services.actions.execute(r.action||"Default",{arguments:{uid:{infoAreaId:s,recordId:c},target:null}});return}var d={commandBindings:{"RecordSet/Fill":function(e,r,n){n.recordSet=t}}};u8.services.actions.execute(n,{target:d})},_onCommandExecuteAction:function(e,r,t){var n=t.recordSet;this.executeAction(n,{action:t.SingleRecordMenuAction,multiAction:t.MultiRecordMenuAction})},_onCommandWriteToLog:function(e,r,t){var n=t.recordSet;var o=u8.services.recordSets.get(n);if(!o){u8.log.error("A record set with id '"+n+"' does not exist.")}else{o.writeToLog()}},_onRecordSetDispose:function(e){if(!e){return}var r=e._id;if(!r){return}delete this._recordSets[r]}});u8.services.add({name:"recordSets",scope:"Login",ctor:u8.Crm.RecordSets});u8.Crm.RecordSet=function(){this._byInfoAreaId={};this._numberOfInfoAreas=0;this._count=0};$.extend(u8.Crm.RecordSet.prototype,{getCount:function(){return this._count},getId:function(){return this._id},getNumberOfInfoAreas:function(){return this._numberOfInfoAreas},getInfoAreaIds:function(e){var r=[];for(var t in this._byInfoAreaId){r.push(t)}return r},getRecordId:function(e,r){var t=this._byInfoAreaId[e];if(!t){return null}var n=t.records[r];return n?n.recordId:null},getRecordIds:function(e){var r=[];var t=this._byInfoAreaId[e];if(t){for(var n=0;n<t.records.length;n++){var o=t.records[n];if(!o){continue}r.push(o.recordId)}}return r},getUids:function(){var e=[];for(var r in this._byInfoAreaId){var t=this._byInfoAreaId[r];for(var n=0;n<t.records.length;n++){var o=t.records[n];if(!o){continue}e.push({infoAreaId:r,recordId:o.recordId})}}return e},getRecordSet:function(e){if(!e){e=u8._.val(true)}if(typeof e!="function"){var r=e;if(typeof r=="string"){r=Array.prototype.slice.call(arguments)}else if(!u8.tools.isArray(r)){$error("Parameters of 'getRecordSet' must be a function, strings or an array of strings")}e=function(e){return r.contains(e.infoAreaId)}}var t=this.hasOwnProperty("_id")?u8.services.recordSets.create():new u8.Crm.RecordSet;this.getUids().filter(e).each(function(e){t.add(e.infoAreaId,e.recordId)});return t},add:function(e,r){if(!r){var t=e.split(".");if(t.length==2){e=t[0];r=t[1]}}var n=this._byInfoAreaId[e];if(!n){this._numberOfInfoAreas++;this._byInfoAreaId[e]=n={records:[],indexesByRecordId:{}}}var o=n.records;var i=n.indexesByRecordId;if(typeof i[r]!=="number"){i[r]=o.length;o.push({recordId:r});this._count++}},contains:function(e,r){var t=this._byInfoAreaId[e];if(!t){return false}var n=t.indexesByRecordId[r];return n>0||n==0?true:false},clear:function(){this.remove()},clearInfoArea:function(e){this.remove(e)},remove:function(e,r){if(!e){this._numberOfInfoAreas=0;this._count=0;this._byInfoAreaId={}}else if(!r){if(this._byInfoAreaId[e]){delete this._byInfoAreaId[e];this._numberOfInfoAreas--}}else{var t=this._byInfoAreaId[e];var n=t?t.indexesByRecordId[r]:null;if(n>0||n===0){delete t.indexesByRecordId[r];t.records.splice(n,1);if(t.records.length<=0){delete this._byInfoAreaId[e];this._numberOfInfoAreas--}}}},writeToLog:function(){u8.log.info("TODO! Write record set '"+this._id+"' to the log.")},"Uri.format":function(){return this.getId()}});u8.Base.Uri.parsers.unshift({name:"recordSet",match:function(e){return e.startsWith("recordSet")},parse:function(e,r){return u8.services.recordSets.get(r)||r}})})();