import*as DataGrid from"../data_grid/data_grid.js";import*as Host from"../host/host.js";import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";const UIStrings={id:"Id",type:"Type",data:"Data",time:"Time",eventSource:"Event Source",copyMessage:"Copy message"},str_=i18n.i18n.registerUIStrings("network/EventSourceMessagesView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class EventSourceMessagesView extends UI.Widget.VBox{constructor(e){super(),this.registerRequiredCSS("network/eventSourceMessagesView.css",{enableLegacyPatching:!1}),this.element.classList.add("event-source-messages-view"),this._request=e;const t=[{id:"id",title:i18nString(UIStrings.id),sortable:!0,weight:8},{id:"type",title:i18nString(UIStrings.type),sortable:!0,weight:8},{id:"data",title:i18nString(UIStrings.data),sortable:!1,weight:88},{id:"time",title:i18nString(UIStrings.time),sortable:!0,weight:8}];this._dataGrid=new DataGrid.SortableDataGrid.SortableDataGrid({displayName:i18nString(UIStrings.eventSource),columns:t,editCallback:void 0,deleteCallback:void 0,refreshCallback:void 0}),this._dataGrid.setStriped(!0),this._dataGrid.setStickToBottom(!0),this._dataGrid.setRowContextMenuCallback(this._onRowContextMenu.bind(this)),this._dataGrid.markColumnAsSortedBy("time",DataGrid.DataGrid.Order.Ascending),this._sortItems(),this._dataGrid.addEventListener(DataGrid.DataGrid.Events.SortingChanged,this._sortItems,this),this._dataGrid.setName("EventSourceMessagesView"),this._dataGrid.asWidget().show(this.element)}wasShown(){this._dataGrid.rootNode().removeChildren();const e=this._request.eventSourceMessages();for(let t=0;t<e.length;++t)this._dataGrid.insertChild(new EventSourceMessageNode(e[t]));this._request.addEventListener(SDK.NetworkRequest.Events.EventSourceMessageAdded,this._messageAdded,this)}willHide(){this._request.removeEventListener(SDK.NetworkRequest.Events.EventSourceMessageAdded,this._messageAdded,this)}_messageAdded(e){const t=e.data;this._dataGrid.insertChild(new EventSourceMessageNode(t))}_sortItems(){const e=this._dataGrid.sortColumnId();if(!e)return;const t=Comparators[e];t&&this._dataGrid.sortNodes(t,!this._dataGrid.isSortOrderAscending())}_onRowContextMenu(e,t){e.clipboardSection().appendItem(i18nString(UIStrings.copyMessage),Host.InspectorFrontendHost.InspectorFrontendHostInstance.copyText.bind(Host.InspectorFrontendHost.InspectorFrontendHostInstance,t.data.data))}}export class EventSourceMessageNode extends DataGrid.SortableDataGrid.SortableDataGridNode{constructor(e){const t=new Date(1e3*e.time),s=("0"+t.getHours()).substr(-2)+":"+("0"+t.getMinutes()).substr(-2)+":"+("0"+t.getSeconds()).substr(-2)+"."+("00"+t.getMilliseconds()).substr(-3),i=document.createElement("div");UI.UIUtils.createTextChild(i,s),UI.Tooltip.Tooltip.install(i,t.toLocaleString()),super({id:e.eventId,type:e.eventName,data:e.data,time:i}),this._message=e}}export function EventSourceMessageNodeComparator(e,t,s){const i=e(t._message),r=e(s._message);return i<r?-1:i>r?1:0}export const Comparators={id:EventSourceMessageNodeComparator.bind(null,(e=>e.eventId)),type:EventSourceMessageNodeComparator.bind(null,(e=>e.eventName)),time:EventSourceMessageNodeComparator.bind(null,(e=>e.time))};