(function(){"use strict";u8.Crm.Widgets.ListingTreeModel=function(e){$.extend(this,u8.Crm.Widgets.ListingTreeModel.defaults,e||{});$assert(this.link,"Missing required property 'link'.");$assert(u8.isValidRecordUid(this.link),"Required property 'link' is invalid.");this.bind(e);this._isCreated=false;this._create()};u8.Crm.Widgets.ListingTreeModel.makeClass({name:"u8.Crm.Widgets.ListingTreeModel",events:["Create","SelectionChange"]});u8.Crm.Widgets.ListingTreeModel.defaults={};$.extend(u8.Crm.Widgets.ListingTreeModel.prototype,{dispose:function(){this.unbind()},getSelection:function(){return this._selectedNode},setSelection:function(e){this._selectedNode=e;if(e){e.checkState=true;e.update()}if(this._isCreated){this._model.eachNode(function(t){if(e&&t.id==e.id){return}if(!t.checkState){return}t.checkState=false;t.update()});this.raise("SelectionChange")}},canSelect:function(){return!!(this._model&&this._model.findNode(i))},getValue:function(){return this._nodeToValue(this._selectedNode)},setValue:function(i){if(!this._isCreated){return}var r=i&&i.recordIds?t(i):i,n=this._model.findNodes(function(t){return e(r,t.value)});if(n&&n.length&&n[0]!=this._selectedNode){this.setSelection(n[0])}},getPath:function(e,t){return this._getPath(e||this._selectedNode,t)||[]},getTree:function(){return this._model},_create:function(){var e=this;u8.request("ListingChannel.Bc.GetTree",{uid:this.link},function(t,i){if(i.error){return undefined}var r=i.response&&i.response.tree;if(!r){return u8.debug.error("Failed to read listings tree for record '"+JSON.stringify(e.link)+"'.")}if("recordId"in r){e._createTreeModel(r)}else{e._model=null}return u8.tools.created(e)})},_createTreeModel:function(r){var n=r,d=t(this.value),l=new u8.Base.Widgets.TreeModel({rootNode:{value:{linkCatalogId:null,recordId:n.recordId},text:n.companyName},nodeTypes:{category:{canClick:false,canSelect:false},subCategory:{canClick:false,canSelect:false},record:{hasCheck:true}}}),o=l.getNode(0),a=null,c={},s={};function u(t,i){for(var r=0;r<i.length;r++){var n=i[r].child,o=n.recordId?{recordId:n.recordId,linkCatalogId:i[r].linkCatalogId}:undefined,c=l.createNode({text:n.companyName,linkCatalogId:i[r].linkCatalogId,value:o,type:n.children?"subCategory":"record"});if(!a&&e(d,o)){a=c}t.appendChild(c);if(n.children){u(c,n.children)}}}function h(t){for(var i=0;i<t.length;i++){var r=t[i];if(r.linkName){c[r.linkCatalogId]=l.createNode({text:r.linkName,linkCatalogId:r.linkCatalogId,type:"category"})}if(r.child){var n=r.child.recordId?{recordId:r.child.recordId,linkCatalogId:r.linkCatalogId}:undefined,o=l.createNode({text:r.child.companyName,linkCatalogId:r.linkCatalogId,value:n,type:r.child.children?"subCategory":"record"});if(s[r.linkCatalogId]){s[r.linkCatalogId].push(o)}else{s[r.linkCatalogId]=[o]}if(!a&&e(d,n)){a=o}if(r.child.children){u(o,r.child.children)}}}}if(n.children&&n.children.length){h(n.children)}for(var f in s){for(var g=0;g<s[f].length;g++){c[f].appendChild(s[f][g])}}for(var C in c){o.appendChild(c[C])}this.setSelection(a||l.findNode(i)||o);this._model=l},_getPath:function(e,t){if(!e){return undefined}var i=[],r=t?function(e){return e.value&&e.value.recordId}:function(e){return e.text};do{i.unshift(e);e=e.parent}while(e);return i.mapAndFilter(r,u8._.trueish())},_nodeToValue:function(e){return e?{recordIds:this._getPath(e,true),linkCatalogId:e.linkCatalogId}:null}});function e(e,t){if(!e||!t){return false}return e.linkCatalogId===t.linkCatalogId&&e.recordId==t.recordId}function t(e){return e&&{linkCatalogId:e.linkCatalogId,recordId:e.recordIds.peek()}||e}function i(e){return e&&e.type=="record"}})();