import{MenuController}from"../menu.mjs";import{isEmpty}from"../../base/helpers.mjs";import{FormView}from"../../view/forms/base.mjs";import{ParentView}from"../../view/base.mjs";import{ModelTableView}from"../../view/table.mjs";import{CheckboxInputView,StringInputView,PasswordInputView,ButtonInputView}from"../../view/forms/input.mjs";class NewUserButtonInputView extends ButtonInputView{static className="new-user-input-view";static defaultValue="New User"}class UserForm extends FormView{static fieldSets={User:{username:{label:"Username",class:StringInputView,config:{required:!0,editable:!1,pattern:"^[a-zA-Z0-9_]{2,}$",minlength:2,maxlength:128}},first_name:{label:"First Name",class:StringInputView},last_name:{label:"Last Name",class:StringInputView}},Permissions:{admin:{label:"Administrator",class:CheckboxInputView}},Password:{new_password:{label:"New Password",class:PasswordInputView},repeat_password:{label:"Repeat Password",class:PasswordInputView}}};async build(){let e=await super.build();return isEmpty(this.values)||"enfugue"===this.values.username&&this.inputViews.filter((e=>"admin"===e.fieldName))[0].disable(),e}}class UsersTableView extends ModelTableView{static columns={username:"Username",name:"Name",type:"Type"};static columnFormatters={type:(e,s)=>!isEmpty(s.permission_groups)&&s.permission_groups.reduce(((e,s)=>e||"admin"===s.group[0].label),!1)?"Admin":"User",name:(e,s)=>`${s.first_name} ${s.last_name}`.trim()};static searchFields=["username","first_name","last_name"]}class UsersController extends MenuController{static menuName="Users";static menuIcon="fa-solid fa-users";static userWindowWidth=600;static userWindowHeight=500;getUser(){return this.model.get("/user")}updateUser(e){return this.model.post("/user",null,null,e)}static isDisabled(){return super.isDisabled()||isEmpty(window.enfugue)||"noauth"===window.enfugue.user}async getUserView(){isEmpty(this.tableView)&&(this.tableView=new UsersTableView(this.config,this.model.User),this.tableView.addButton("Edit","fa-solid fa-edit",(async e=>{let s=e.getAttributes();s.admin=!isEmpty(e.permission_groups)&&e.permission_groups.reduce(((e,s)=>e||"admin"===s.group[0].label),!1);let t=new UserForm(this.config,s),i=await this.spawnWindow(`Edit ${e.username}`,t,this.constructor.userWindowWidth,this.constructor.userWindowHeight);t.onSubmit((async s=>{t.clearError();try{if(!isEmpty(s.new_password)&&!isEmpty(s.repeat_password)){if(s.new_password!==s.repeat_password)throw"Passwords do not match.";e.stageChanges({new_password:s.new_password,repeat_password:s.repeat_password})}e.first_name=s.first_name,e.last_name=s.last_name,"enfugue"!==e.username&&e.stageChange("admin",s.admin),await e.save(),this.notify("info","Success",`User ${e.username} updated.`),i.remove(),isEmpty(this.tableView)||this.tableView.requery()}catch(e){let s=`${e}`;isEmpty(e.detail)?isEmpty(e.title)||(s=e.title):s=e.detail,t.setError(s),t.enable()}})),t.onCancel((()=>i.remove()))})),this.tableView.addButton("Delete","fa-solid fa-trash",(async e=>{if("enfugue"===e.username)return this.notify("warn","Forbidden","Deleting the default user is not allowed.");await e.delete(),isEmpty(this.tableView)||this.tableView.requery()}))),isEmpty(this.newUserView)&&(this.newUserView=new NewUserButtonInputView(this.config),this.newUserView.onChange((async()=>{if(isEmpty(this.newUserWindow)){let e=new UserForm(this.config);this.newUserWindow=await this.spawnWindow("New User",e,this.constructor.userWindowWidth,this.constructor.userWindowHeight),e.onSubmit((async s=>{e.clearError();try{if(isEmpty(s.new_password)||isEmpty(s.repeat_password))throw"Password is required.";if(s.new_password!==s.repeat_password)throw"Passwords do not match.";await this.model.User.create(s),this.notify("info","Success",`User ${s.username} created.`),this.newUserWindow.remove(),isEmpty(this.tableView)||this.tableView.requery()}catch(s){let t=`${s}`;isEmpty(s.detail)?isEmpty(s.title)||(t=s.title):t=s.detail,e.setError(t),e.enable()}})),e.onCancel((()=>this.newUserWindow.remove())),this.newUserWindow.onClose((()=>{this.newUserWindow=null}))}else this.newUserWindow.focus()})));let e=new ParentView(this.config);return await e.addChild(this.tableView),await e.addChild(this.newUserView),e}async showUserManager(){isEmpty(this.userManager)?(this.userManager=await this.spawnWindow("Users",await this.getUserView(),this.constructor.userWindowWidth,this.constructor.userWindowHeight),this.userManager.onClose((()=>{this.userManager=null}))):this.userManager.focus()}async onClick(){this.showUserManager()}}export{UsersController as MenuController};
