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";const UIStrings={database:"Database",refresh:"Refresh",visibleColumns:"Visible columns",theStableIsEmpty:'The "{PH1}" table is empty.',anErrorOccurredTryingToreadTheS:'An error occurred trying to read the "{PH1}" table.'},str_=i18n.i18n.registerUIStrings("resources/DatabaseTableView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class DatabaseTableView extends UI.View.SimpleView{constructor(t,e){super(i18nString(UIStrings.database)),this.database=t,this.tableName=e,this._lastVisibleColumns="",this._columnsMap=new Map,this.element.classList.add("storage-view","table"),this._visibleColumnsSetting=Common.Settings.Settings.instance().createSetting("databaseTableViewVisibleColumns",{}),this.refreshButton=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.refresh),"largeicon-refresh"),this.refreshButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this._refreshButtonClicked,this),this._visibleColumnsInput=new UI.Toolbar.ToolbarInput(i18nString(UIStrings.visibleColumns),"",1),this._visibleColumnsInput.addEventListener(UI.Toolbar.ToolbarInput.Event.TextChanged,this._onVisibleColumnsChanged,this),this._dataGrid=null}wasShown(){this.update()}async toolbarItems(){return[this.refreshButton,this._visibleColumnsInput]}_escapeTableName(t){return t.replace(/\"/g,'""')}update(){this.database.executeSql('SELECT rowid, * FROM "'+this._escapeTableName(this.tableName)+'"',this._queryFinished.bind(this),this._queryError.bind(this))}_queryFinished(t,e){if(this.detachChildWidgets(),this.element.removeChildren(),this._dataGrid=DataGrid.SortableDataGrid.SortableDataGrid.create(t,e,i18nString(UIStrings.database)),this._visibleColumnsInput.setVisible(Boolean(this._dataGrid)),!this._dataGrid)return this._emptyWidget=new UI.EmptyWidget.EmptyWidget(i18nString(UIStrings.theStableIsEmpty,{PH1:this.tableName})),void this._emptyWidget.show(this.element);this._dataGrid.setStriped(!0),this._dataGrid.asWidget().show(this.element),this._dataGrid.autoSizeColumns(5),this._columnsMap.clear();for(let s=1;s<t.length;++s)this._columnsMap.set(t[s],String(s));this._lastVisibleColumns="";const i=this._visibleColumnsSetting.get()[this.tableName]||"";this._visibleColumnsInput.setValue(i),this._onVisibleColumnsChanged()}_onVisibleColumnsChanged(){if(!this._dataGrid)return;const t=this._visibleColumnsInput.value(),e=t.split(/[\s,]+/),i=new Set,s=new Set;s.add("0");for(const r of e){const t=this._columnsMap.get(r);void 0!==t&&(i.add(r),s.add(t))}const n=[...i].sort().join(", ");if(0===n.length)for(const r of this._columnsMap.values())s.add(r);if(n===this._lastVisibleColumns)return;const a=this._visibleColumnsSetting.get();a[this.tableName]=t,this._visibleColumnsSetting.set(a),this._dataGrid.setColumnsVisiblity(s),this._lastVisibleColumns=n}_queryError(){this.detachChildWidgets(),this.element.removeChildren();const t=document.createElement("div");t.className="storage-table-error",t.textContent=i18nString(UIStrings.anErrorOccurredTryingToreadTheS,{PH1:this.tableName}),this.element.appendChild(t)}_refreshButtonClicked(t){this.update()}}