import{KvKeyEditorCtrl as t}from"./kv-key-editor-ctrl.js";import{KvScanner as e}from"./kv-scanner.js";class i extends t{constructor(t,e,i,a,n){super(t,e,i,a),this._scanner=new s(t,a,n)}getType(){return"list"}async load(){this._updateEditorState({type:"loading"}),await this._scanner.loadMore(),this._updateEditorState({type:"loaded",elements:this._scanner.elements,addForm:{type:"idle"}})}async create(t){await this._kvClient.createListKey(t.name,t.elements),this._scanner.update(t.elements.map(((t,e)=>({index:e,value:t,type:"idle"})))),this._updateEditorState({type:"loaded",elements:this._scanner.elements,addForm:{type:"idle"}})}async filter(t){this._updateEditorState((t=>t.type="filtering"));try{this._scanner.setFilter({index:t}),await this._scanner.loadMore(),this._updateEditor((t=>{t.type="loaded",t.elements=this._scanner.elements}))}catch(t){throw this._updateEditorState((t=>t.type="loaded")),t}}async loadMore(){this._updateEditorState((t=>t.type="loading-more"));try{await this._scanner.loadMore(),this._updateEditor((t=>{t.type="loaded",t.elements=this._scanner.elements}))}catch(t){throw this._updateEditorState((t=>t.type="loaded")),t}}async updateElement(t,e){this._updateEditorElementState(t,"updating");try{await this._kvClient.updateListElement(this.keyName,t,e),this._scanner.update([{type:"idle",index:t,value:e}]),this._updateEditor((t=>{t.elements=this._scanner.elements}))}catch(e){throw this._updateEditorElementState(t,"editing"),e}}async addElement(t,e){this._updateAddForm({type:"adding"});try{const i=await this._kvClient.pushListElement(this.keyName,t,e);return this.component.resetEditorForm(),this._scanner.reset(),await this.load(),i.index}catch(t){throw this._updateAddForm({type:"idle"}),t}}_updateEditorElementState(t,e){const i=this._scanner.getElement(String(t));null!=i&&this._scanner.update([{...i,type:e}]),this._updateEditor((t=>{t.elements=this._scanner.elements}))}_updateAddForm(t){this._updateEditor((e=>{e.addForm=t}))}_updateEditor(t){this._updateEditorState(t)}}class s extends e{constructor(t,e,i){super(i),this._kvClient=e,this._keyName=t}getId(t){return String(t.index)}matchFilter(t){return null==this._filter?.index||t.index===this._filter.index}async fetch(t,e){if(null==this._filter?.index){const i=await this._kvClient.scanList(this._keyName,e,{cursor:this._cursor,count:t,match:null});return{cursor:i.cursor,total:i.total,elements:i.elements.map((t=>({...t,type:"idle"})))}}if(isNaN(this._filter.index))return{cursor:0,total:0,elements:[]};try{const t=await this._kvClient.getListElementAt(this._keyName,this._filter.index,e);return null==t.value?{cursor:0,total:0,elements:[]}:{cursor:0,total:0,elements:[{type:"idle",index:t.index,value:t.value}]}}catch(t){const e=t;if("clever.redis-http.list-element-not-found"===e?.responseBody?.code)return{cursor:0,total:0,elements:[]};throw t}}}export{i as KvKeyEditorListCtrl,s as KvListElementsScanner};
//# sourceMappingURL=kv-key-editor-list-ctrl.js.map
