import{isEmpty}from"../../base/helpers.mjs";import{ElementBuilder}from"../../base/builder.mjs";import{Controller}from"../base.mjs";import{TweaksFormView,SchedulerConfigurationFormView}from"../../forms/enfugue/tweaks.mjs";const E=new ElementBuilder;class AdvancedTweaksFormView extends TweaksFormView{static schedulerWindowWidth=400;static schedulerWindowHeight=140;getSchedulerForm(){return isEmpty(this.schedulerForm)&&(this.schedulerForm=new SchedulerConfigurationFormView(this.config),this.schedulerForm.onSubmit((e=>{this.values={...this.values,...e},this.submit()}))),this.schedulerForm}async setValues(e,s){return this.getSchedulerForm().setValues(e,!1),await super.setValues(e,s)}async showSchedulerConfiguration(){if(isEmpty(this.schedulerWindow)){let e=this.getSchedulerForm();e.setValues(this.values,!1),this.schedulerWindow=await this.spawnWindow("More Scheduler Configuration",e,this.constructor.schedulerWindowWidth,this.constructor.schedulerWindowHeight),this.schedulerWindow.onClose((()=>{delete this.schedulerWindow}))}else this.schedulerWindow.focus()}async build(){let e=await super.build(),s=E.i().class("fa-solid fa-gear").css({position:"absolute",cursor:"pointer",top:0,right:0}).data("tooltip","More Scheduler Configuration").on("click",(e=>{this.showSchedulerConfiguration()}));return e.find(".scheduler-input-view").append(s),e}}class TweaksController extends Controller{getState(e=!0){return{tweaks:this.tweaksForm.values}}setState(e){isEmpty(e.tweaks)||this.tweaksForm.setValues(e.tweaks).then((()=>this.tweaksForm.submit()))}getDefaultState(){return{tweaks:{guidanceScale:this.config.model.invocation.guidanceScale,inferenceSteps:this.config.model.invocation.inferenceSteps,scheduler:null,clipSkip:0,enableFreeU:!1,injectDpo:!1,freeUBackbone1:1.5,freeUBackbone2:1.6,freeUSkip1:.9,freeUSkip2:.2,noiseOffset:0,noiseMethod:"simplex",noiseBlendMethod:"inject",betaStart:null,betaEnd:null,betaSchedule:null}}}async initialize(){let e=this.getDefaultState().tweaks;this.tweaksForm=new AdvancedTweaksFormView(this.config),this.tweaksForm.spawnWindow=(e,s,i,t,n,o)=>this.spawnWindow(e,s,i,t,n,o),this.tweaksForm.onSubmit((async s=>{this.engine.guidanceScale=s.guidanceScale,this.engine.inferenceSteps=s.inferenceSteps,this.engine.scheduler=s.scheduler,this.engine.clipSkip=s.clipSkip,this.engine.noiseOffset=s.noiseOffset,this.engine.noiseMethod=s.noiseMethod,this.engine.noiseBlendMethod=s.noiseBlendMethod,this.engine.injectDpo=s.injectDpo,this.engine.betaStart=s.betaStart,this.engine.betaEnd=s.betaEnd,this.engine.betaSchedule=s.betaSchedule,s.enableFreeU?this.engine.freeUFactors=[isEmpty(s.freeUBackbone1)?e.freeUBackbone1:s.freeUBackbone1,isEmpty(s.freeUBackbone2)?e.freeUBackbone2:s.freeUBackbone2,isEmpty(s.freeUSkip1)?e.freeUSkip1:s.freeUSkip1,isEmpty(s.freeUSkip2)?e.freeUSkip2:s.freeUSkip2]:this.engine.freeUFactors=null})),this.subscribe("modelPickerChange",(e=>{if(!isEmpty(e)){let s=e.defaultConfiguration,i={};isEmpty(s.guidance_scale)||(i.guidanceScale=s.guidance_scale),isEmpty(s.num_inference_steps)||(i.inferenceSteps=s.num_inference_steps),isEmpty(s.clip_skip)||(i.clipSkip=s.clip_skip),isEmpty(s.noise_offset)||(i.noiseOffset=s.noise_offset),isEmpty(s.noise_method)?i.noiseMethod=this.tweaksForm.values.noiseMethod:i.noiseMethod=s.noise_method,isEmpty(s.noise_blend_method)?i.noiseBlendMethod=this.tweaksForm.values.noiseBlendMethod:i.noiseBlendMethod=s.noise_blend_method,isEmpty(s.freeu_factors)?i.enableFreeU=!1:(i.enableFreeU=!0,i.freeUBackbone1=s.freeu_factors[0],i.freeUBackbone2=s.freeu_factors[1],i.freeUSkip1=s.freeu_factors[2],i.freeUSkip2=s.freeu_factors[3]),isEmpty(e.scheduler)?i.scheduler=this.tweaksForm.values.scheduler:i.scheduler=e.scheduler[0].name,isEmpty(i)||this.tweaksForm.setValues(i)}})),this.application.sidebar.addChild(this.tweaksForm)}}export{TweaksController as SidebarController};
