import{isStringEmpty as t}from"./lib/utils.js";import{KvKeyEditorCtrl as e}from"./kv-key-editor-ctrl.js";import{KvScanner as a}from"./kv-scanner.js";import{matchKvPattern as s}from"./kv-utils.js";class i extends e{constructor(t,e,a,s,i){super(t,e,a,s),this._scanner=new r(t,s,i)}getType(){return"set"}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.createSetKey(t.name,t.elements),this._scanner.update(t.elements.map((t=>({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({pattern: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 deleteElement(t){this._updateEditorElementState(t,"deleting");try{await this._kvClient.deleteSetElement(this.keyName,t),this._scanner.delete(t),this._updateEditor((t=>{t.elements=this._scanner.elements}))}catch(e){throw this._updateEditorElementState(t,"idle"),e}}async addElement(t){this._updateAddForm({type:"adding"});try{const e=await this._kvClient.addSetElement(this.keyName,t);return this.component.resetEditorForm(),e.added?(this._scanner.reset(),await this.load()):this._updateAddForm({type:"idle"}),e.added}catch(t){throw this._updateAddForm({type:"idle"}),t}}_updateEditorElementState(t,e){const a=this._scanner.getElement(t);null!=a&&this._scanner.update([{...a,type:e}]),this._updateEditor((t=>{t.elements=this._scanner.elements}))}_updateAddForm(t){this._updateEditor((e=>{e.addForm=t}))}_updateEditor(t){this._updateEditorState(t)}}class r extends a{constructor(t,e,a){super(a),this._keyName=t,this._kvClient=e}getId(t){return t.value}matchFilter(e){return t(this._filter?.pattern)||s(this._filter.pattern,e.value)}async fetch(t,e){const a=await this._kvClient.scanSet(this._keyName,e,{cursor:this._cursor,count:t,match:this._filter?.pattern});return{cursor:a.cursor,total:a.total,elements:a.elements.map((t=>({value:t,type:"idle"})))}}}export{i as KvKeyEditorSetCtrl,r as KvSetElementsScanner};
//# sourceMappingURL=kv-key-editor-set-ctrl.js.map
