import{isEmpty,deepClone}from"../../base/helpers.mjs";import{Controller}from"../base.mjs";import{ParentView}from"../../view/base.mjs";import{TableView,ModelTableView}from"../../view/table.mjs";import{ButtonInputView}from"../../forms/input.mjs";import{ModelFormView}from"../../forms/enfugue/models.mjs";class NewModelInputView extends ButtonInputView{static defaultValue="New Model Configuration";static className="new-model-input-view"}class ModelManagerController extends Controller{static modelWindowWidth=400;static modelWindowHeight=1e3;static managerWindowWidth=800;static managerWindowHeight=600;async showEditModel(e){let t=e.getAttributes();if(t.checkpoint=t.model,t.lora=isEmpty(e.lora)?[]:e.lora.map((e=>e.getAttributes())),t.lycoris=isEmpty(e.lycoris)?[]:e.lycoris.map((e=>e.getAttributes())),t.inversion=isEmpty(e.inversion)?[]:e.inversion.map((e=>e.model)),t.vae=isEmpty(e.vae)?null:e.vae[0].name,t.motion_module=isEmpty(e.motion_module)?null:e.motion_module[0].name,isEmpty(e.refiner)||(t.refiner=e.refiner[0].model),isEmpty(e.inpainter)||(t.inpainter=e.inpainter[0].model),!isEmpty(e.config)){let i={};for(let t of e.config)i[t.configuration_key]=t.configuration_value;t={...t,...i},isEmpty(i.prompt_2)||(t.prompt=[t.prompt,i.prompt_2]),isEmpty(i.negative_prompt_2)||(t.negative_prompt=[i.negative_prompt,i.negative_prompt_2])}isEmpty(e.scheduler)||(t.scheduler=e.scheduler[0].name);let i,o=new ModelFormView(this.config,deepClone(t));o.onChange((async e=>{isEmpty(o.values.refiner)?o.removeClass("show-refiner"):o.addClass("show-refiner"),isEmpty(o.values.inpainter)?o.removeClass("show-inpainter"):o.addClass("show-inpainter")})),o.onSubmit((async t=>{Array.isArray(t.prompt)&&(t.prompt_2=t.prompt[1],t.prompt=t.prompt[0]),Array.isArray(t.negative_prompt)&&(t.negative_prompt_2=t.negative_prompt[1],t.negative_prompt=t.negative_prompt[0]);try{await this.model.patch(`/models/${e.name}`,null,null,t),isEmpty(i)||i.remove(),isEmpty(this.tableView)||this.tableView.requery()}catch(e){let t=isEmpty(e)?"Couldn't communicate with server.":isEmpty(e.detail)?`${e}`:e.detail;this.notify("error","Couldn't update model",t),o.enable()}})),o.onCancel((()=>i.remove())),i=await this.spawnWindow(`Edit ${e.name}`,o,this.constructor.modelWindowWidth,this.constructor.modelWindowHeight)}async createManager(){this.tableView=new ModelTableView(this.config,this.model.DiffusionModel),this.buttonView=new NewModelInputView(this.config),this.tableView.setColumns({name:"Name",model:"Model",prompt:"Prompt",negative_prompt:"Negative Prompt"}),this.tableView.setSearchFields(["name","prompt","negative_prompt","model"]),this.tableView.setFormatter("size",(e=>`${e}px`)),this.tableView.addButton("Edit","fa-solid fa-edit",(e=>this.showEditModel(e))),this.tableView.addButton("Delete","fa-solid fa-trash",(async e=>{try{await this.model.delete(`/models/${e.name}`),this.tableView.requery()}catch(e){let t=isEmpty(e)?"Couldn't communicate with server.":isEmpty(e.detail)?`${e}`:e.detail;this.notify("error","Couldn't delete model",t),modelForm.enable()}})),this.buttonView.onChange((()=>{this.showNewModel()}));let e=new ParentView(this.config);return e.addChild(this.tableView),e.addChild(this.buttonView),e}async createModelForm(){let e=new ModelFormView(this.config);return e.onSubmit((async t=>{Array.isArray(t.prompt)&&(t.prompt_2=t.prompt[1],t.prompt=t.prompt[0]),Array.isArray(t.negative_prompt)&&(t.negative_prompt_2=t.negative_prompt[1],t.negative_prompt=t.negative_prompt[0]);try{await this.model.post("/models",null,null,t);isEmpty(this.newModelWindow)||(this.newModelWindow.remove(),this.newModelWindow=null),isEmpty(this.managerWindow)||isEmpty(this.tableView)||this.tableView.requery()}catch(t){let i=isEmpty(t)?"Couldn't communicate with server.":isEmpty(t.detail)?`${t}`:t.detail;this.notify("Error","Couldn't create model",i),e.enable()}})),e.onChange((async t=>{isEmpty(e.values.refiner)?e.removeClass("show-refiner"):e.addClass("show-refiner"),isEmpty(e.values.inpainter)?e.removeClass("show-inpainter"):e.addClass("show-inpainter")})),e.onCancel((()=>{this.newModelWindow.remove(),this.newModelWindow=null})),e}async showNewModel(){isEmpty(this.newModelWindow)?(this.newModelWindow=await this.spawnWindow("New Configuration",this.createModelForm(),this.constructor.modelWindowWidth,this.constructor.modelWindowHeight),this.newModelWindow.onClose((()=>{delete this.newModelWindow}))):this.newModelWindow.focus()}async showManager(){isEmpty(this.managerWindow)?(this.managerWindow=await this.spawnWindow("Model Configuration Manager",this.createManager(),this.constructor.managerWindowWidth,this.constructor.managerWindowHeight),this.managerWindow.onClose((()=>{delete this.managerWindow}))):this.managerWindow.focus()}}export{ModelManagerController};
