import*as Platform from"../platform/platform.js";import{ProfileDataGridNode,ProfileDataGridTree}from"./ProfileDataGrid.js";export class BottomUpProfileDataGridNode extends ProfileDataGridNode{constructor(e,t){super(e,t,null!==e.parent&&Boolean(e.parent.parent)),this._remainingNodeInfos=[]}static _sharedPopulate(e){if(void 0===e._remainingNodeInfos)return;const t=e._remainingNodeInfos,o=t.length;for(let r=0;r<o;++r){const o=t[r],i=o.ancestor,s=o.focusNode;let a=e.findChild(i);if(a){const e=o.totalAccountedFor;a.self+=s.self,e||(a.total+=s.total)}else a=new BottomUpProfileDataGridNode(i,e.tree),i!==s&&(a.self=s.self,a.total=s.total),e.appendChild(a);const l=i.parent;l&&l.parent&&(o.ancestor=l,a._remainingNodeInfos||(a._remainingNodeInfos=[]),a._remainingNodeInfos.push(o))}delete e._remainingNodeInfos}_takePropertiesFromProfileDataGridNode(e){this.save(),this.self=e.self,this.total=e.total}_keepOnlyChild(e){this.save(),this.removeChildren(),this.appendChild(e)}_exclude(e){this._remainingNodeInfos&&this.populate(),this.save();const t=this.children;let o=this.children.length;for(;o--;)t[o]._exclude(e);const r=this.childrenByCallUID.get(e);r&&this.merge(r,!0)}restore(){super.restore(),this.children.length||this.setHasChildren(this._willHaveChildren(this.profileNode))}merge(e,t){this.self-=e.self,super.merge(e,t)}populateChildren(){BottomUpProfileDataGridNode._sharedPopulate(this)}_willHaveChildren(e){return Boolean(e.parent&&e.parent.parent)}}export class BottomUpProfileDataGridTree extends ProfileDataGridTree{constructor(e,t,o,r){super(e,t,r),this.deepSearch=!1;let i=0;const s=[[],[o]],a=new Map;this._remainingNodeInfos=[];for(let l=0;l<s.length;++l){const e=s[l],t=s[++l],o=t.length,r=new WeakMap;for(let l=0;l<o;++l){const o=t[l];if(r.get(o)||r.set(o,++i),o.parent){let t=a.get(o.callUID),i=!1;if(t){const o=e.length;for(let s=0;s<o;++s){const o=r.get(e[s]);if(o&&t.has(o)){i=!0;break}}}else t=new Set,a.set(o.callUID,t);const s=r.get(o);s&&t.add(s),this._remainingNodeInfos.push({ancestor:o,focusNode:o,totalAccountedFor:i})}const n=o.children;n.length&&(s.push(e.concat([o])),s.push(n))}}return ProfileDataGridNode.populate(this),this}focus(e){if(!e)return;this.save();let t=e,o=e;for(;t.parent&&t instanceof BottomUpProfileDataGridNode;)t._takePropertiesFromProfileDataGridNode(e),o=t,t=t.parent,t instanceof BottomUpProfileDataGridNode&&t._keepOnlyChild(o);this.children=[o],this.total=e.total}exclude(e){if(!e)return;this.save();const t=e.callUID,o=this.childrenByCallUID.get(t);o&&Platform.ArrayUtilities.removeElement(this.children,o);const r=this.children,i=r.length;for(let s=0;s<i;++s)r[s]._exclude(t);this.lastComparator&&this.sort(this.lastComparator,!0)}populateChildren(){BottomUpProfileDataGridNode._sharedPopulate(this)}}