import{isStringEmpty as e}from"./lib/utils.js";import{KvScanner as t}from"./kv-scanner.js";import{Abortable as s,matchKvPattern as i}from"./kv-utils.js";class a{constructor(e,t,i){this._component=e,this._updateState=t,this._kvClient=i,this._abortable=new s,this._scanner=new l(i,this._abortable,(()=>{const e=this._getViewState();"loaded"===e.type&&this._updateState({type:e.type,keys:this._scanner.elements,total:this._scanner.total})})),this._selectedKey=null}abort(){this._abortable.abort()}async filter(t,s){if("loaded"!==this._getViewState().type&&"error-keys"!==this._getViewState().type)return;const i={type:"all"===t?null:t,pattern:e(s)?null:s};this._scanner.setFilter(i),await this.fetchKeys({type:"filtering",keys:[]})}async refresh(){"loaded"!==this._getViewState().type&&"error-keys"!==this._getViewState().type||(this._scanner.reset(),await this.fetchKeys({type:"refreshing",keys:[]}))}async loadMore(){"loaded"===this._getViewState().type&&await this.fetchKeys()}select(e){return"loaded"===this._getViewState().type&&(this._updateKeyState(e.name,"selected"),!0)}clearSelection(){null!=this._selectedKey&&this._updateKeyState(this._selectedKey.name,"idle")}async delete(e){if("loaded"!==this._getViewState().type)return;const t=this._scanner.getElement(e);this._updateKeyState(e,"deleting");try{await this._kvClient.deleteKey(e);const t=this._scanner.delete(e);this._selectedKey?.name===e&&(this._selectedKey=null),await this._component.focusAfterDeleteKeyAt(t)}catch(s){throw this._updateKeyState(e,t.type),s}}add(e){const t=this._getViewState();if(!this._isLoadedState(t))return;this.clearSelection();const s={type:"selected",key:e};this._scanner.update([s]),this._selectedKey=e}async fetchKeys(e={}){if(this._scanner.hasMore()){let t,s;const i=this._getViewState();if("loaded"!==i.type&&"loading-keys"!==i.type||(t=e.keys??i.keys,s=e.total??i.total),this._updateState({type:e.type??"loading-keys",keys:t??[],total:s??0}),await this._scanner.loadMore(),t=this._scanner.elements,s=this._scanner.total,null!=this._selectedKey){null==this._scanner.getElement(this._selectedKey.name)&&(this._selectedKey=null)}if(null!=this._selectedKey){const e=this._selectedKey.name;this._selectedKey=null,this._updateKeyState(e,"selected")}this._updateState({type:"loaded",keys:t,total:s})}}onKeyNotFound(e){this._updateKeyState(e,"idle")}_getViewState(){return this._component.state}_updateKeyState(e,t){const s=this._scanner.getElement(e);if(null!=s){const i=[{...s,type:t}];"selected"!==t&&e===this._selectedKey?.name&&(this._selectedKey=null),"selected"===t&&null!=this._selectedKey&&e!==this._selectedKey.name&&i.push({type:"idle",key:this._selectedKey}),"selected"===t&&(this._selectedKey=s.key),this._scanner.update(i)}}_isLoadedState(e){return"loaded"===e.type||"loading-keys"===e.type}}class l extends t{constructor(e,t,s){super(t,s),this._kvClient=e}getId(e){return e.key.name}matchFilter(t){return(null==this._filter?.type||t.key.type===this._filter.type)&&(e(this._filter?.pattern)||i(this._filter.pattern,t.key.name))}async fetch(e,t){const s=await this._kvClient.scanKeys({cursor:this._cursor,count:e,type:this._filter?.type,match:this._filter?.pattern});return{cursor:s.cursor,total:s.total,elements:s.keys.map((e=>({type:"idle",key:e})))}}}export{a as KvKeysCtrl};
//# sourceMappingURL=kv-keys-ctrl.js.map
