import*as DataGrid from"../data_grid/data_grid.js";import*as i18n from"../i18n/i18n.js";import*as SourceFrame from"../source_frame/source_frame.js";import*as UI from"../ui/ui.js";const UIStrings={timestamp:"Timestamp",eventName:"Event name",value:"Value",eventDisplay:"Event display"},str_=i18n.i18n.registerUIStrings("media/EventDisplayTable.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class EventNode extends DataGrid.DataGrid.DataGridNode{constructor(t){super(t,!1),this._expandableElement=null}createCell(t){const e=this.createTD(t),i=this.data[t];if("value"===t){const t=e.createChild("div","event-display-table-contents-json-wrapper");this._expandableElement=new SourceFrame.JSONView.JSONView(new SourceFrame.JSONView.ParsedJSON(i,"",""),!0),this._expandableElement.markAsRoot(),this._expandableElement.show(t)}else e.classList.add("event-display-table-basic-text-table-entry"),UI.UIUtils.createTextChild(e,i);return e}}export class PlayerEventsView extends UI.Widget.VBox{constructor(){super(),this.registerRequiredCSS("media/eventDisplayTable.css",{enableLegacyPatching:!1}),this.contentElement.classList.add("event-display-table-contents-table-container"),this._dataGrid=this._createDataGrid([{id:"displayTimestamp",title:i18nString(UIStrings.timestamp),weight:1,sortable:!1},{id:"event",title:i18nString(UIStrings.eventName),weight:2,sortable:!1},{id:"value",title:i18nString(UIStrings.value),weight:7,sortable:!1}]),this._firstEventTime=0,this._dataGrid.setStriped(!0),this._dataGrid.asWidget().show(this.contentElement)}_createDataGrid(t){const e=[];for(const a of t)e.push(PlayerEventsView._convertToGridDescriptor(a));const i=new DataGrid.DataGrid.DataGridImpl({displayName:i18nString(UIStrings.eventDisplay),columns:e,deleteCallback:void 0,editCallback:void 0,refreshCallback:void 0});return i.asWidget().contentElement.classList.add("no-border-top-datagrid"),i}onEvent(t){0===this._firstEventTime&&"number"===typeof t.timestamp&&(this._firstEventTime=t.timestamp);const e=(t=this._subtractFirstEventTime(t)).value;try{const i=JSON.parse(e);t.event=i.event,delete i.event,t.value=i;const a=new EventNode(t),s=this._dataGrid.scrollContainer,r=s.scrollTop===s.scrollHeight-s.offsetHeight;this._dataGrid.rootNode().appendChild(a),r&&(s.scrollTop=s.scrollHeight)}catch(i){}}_subtractFirstEventTime(t){return"number"===typeof t.timestamp&&(t.displayTimestamp=(t.timestamp-this._firstEventTime).toFixed(3)),t}static _convertToGridDescriptor(t){return{id:t.id,title:t.title,sortable:t.sortable,weight:t.weight||0,sort:DataGrid.DataGrid.Order.Ascending}}}