import*as Common from"../common/common.js";import*as DataGrid from"../data_grid/data_grid.js";import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";import{Category,IsLong}from"./TimelineFilters.js";import{TimelineSelection}from"./TimelinePanel.js";import{TimelineTreeView}from"./TimelineTreeView.js";import{TimelineUIUtils}from"./TimelineUIUtils.js";const UIStrings={filterEventLog:"Filter event log",startTime:"Start Time",durationFilter:"Duration filter",Dms:"{PH1}\xa0ms",all:"All"},str_=i18n.i18n.registerUIStrings("timeline/EventsTimelineTreeView.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class EventsTimelineTreeView extends TimelineTreeView{constructor(e){super(),this._filtersControl=new Filters,this._filtersControl.addEventListener(Filters.Events.FilterChanged,this._onFilterChanged,this),this.init(),this._delegate=e,this.dataGrid.markColumnAsSortedBy("startTime",DataGrid.DataGrid.Order.Ascending),this.splitWidget.showBoth(),this._currentTree}filters(){return[...super.filters(),...this._filtersControl.filters()]}updateContents(e){if(super.updateContents(e),e.type()===TimelineSelection.Type.TraceEvent){const t=e.object();this._selectEvent(t,!0)}}getToolbarInputAccessiblePlaceHolder(){return i18nString(UIStrings.filterEventLog)}_buildTree(){return this._currentTree=this.buildTopDownTree(!0,null),this._currentTree}_onFilterChanged(){const e=this.lastSelectedNode(),t=e&&e.event;this.refreshTree(),t&&this._selectEvent(t,!1)}_findNodeWithEvent(e){const t=[this._currentTree.children().values()];for(;t.length;){const i=t[t.length-1].next();if(i.done){t.pop();continue}const r=i.value;if(r.event===e)return r;t.push(r.children().values())}return null}_selectEvent(e,t){const i=this._findNodeWithEvent(e);if(i&&(this.selectProfileNode(i,!1),t)){const e=this.dataGridNodeForTreeNode(i);e&&e.expand()}}populateColumns(e){e.push({id:"startTime",title:i18nString(UIStrings.startTime),width:"80px",fixedWidth:!0,sortable:!0}),super.populateColumns(e),e.filter((e=>e.fixedWidth)).forEach((e=>{e.width="80px"}))}populateToolbar(e){super.populateToolbar(e),this._filtersControl.populateToolbar(e)}_showDetailsForNode(e){const t=e.event;if(!t)return!1;const i=this.model();return!!i&&(TimelineUIUtils.buildTraceEventDetails(t,i.timelineModel(),this.linkifier,!1).then((e=>this.detailsView.element.appendChild(e))),!0)}_onHover(e){this._delegate.highlightEvent(e&&e.event)}}export class Filters extends Common.ObjectWrapper.ObjectWrapper{constructor(){super(),this._categoryFilter=new Category,this._durationFilter=new IsLong,this._filters=[this._categoryFilter,this._durationFilter]}filters(){return this._filters}populateToolbar(e){const t=new UI.Toolbar.ToolbarComboBox(function(){const e=t.selectedOption().value,i=parseInt(e,10);this._durationFilter.setMinimumRecordDuration(i),this._notifyFiltersChanged()}.bind(this),i18nString(UIStrings.durationFilter));for(const s of Filters._durationFilterPresetsMs)t.addOption(t.createOption(s?`\u2265 ${i18nString(UIStrings.Dms,{PH1:s})}`:i18nString(UIStrings.all),String(s)));e.appendToolbarItem(t);const i=new Map,r=TimelineUIUtils.categories();for(const s in r){const t=r[s];if(!t.visible)continue;const o=new UI.Toolbar.ToolbarCheckbox(t.title,void 0,n.bind(this,s));o.setChecked(!0),o.inputElement.style.backgroundColor=t.color,i.set(t.name,o),e.appendToolbarItem(o)}function n(e){const t=TimelineUIUtils.categories(),r=i.get(e);t[e].hidden=!r||!r.checked(),this._notifyFiltersChanged()}}_notifyFiltersChanged(){this.dispatchEventToListeners(Filters.Events.FilterChanged)}}Filters._durationFilterPresetsMs=[0,1,15],Filters.Events={FilterChanged:Symbol("FilterChanged")};