import{isEmpty,kebabCase}from"../../base/helpers.mjs";import{ElementBuilder}from"../../base/builder.mjs";import{Controller}from"../base.mjs";import{ThemeFormView}from"../../forms/enfugue/theme.mjs";const E=new ElementBuilder;class ThemeController extends Controller{static themeWindowWidth=750;static themeWindowHeight=450;get customTheme(){let e=this.application.session.getItem("customTheme");return isEmpty(e)&&(e={}),{...this.defaultTheme,...e}}get defaultTheme(){return this.config.themes[this.config.theme]}set customTheme(e){this.application.session.setItem("customTheme",e)}get theme(){let e=this.application.session.getItem("theme");return isEmpty(e)&&(e=this.config.theme),e}set theme(e){this.application.session.setItem("theme",e),this.setTheme(this.getTheme(e))}setTheme(e){let t=window.document.documentElement;for(let s in e)t.style.setProperty(`--${kebabCase(s)}`,e[s])}getTheme(e){return"Custom"===e?this.customTheme:this.config.themes[e]}resetTheme(){this.setTheme(this.getTheme(this.theme))}createThemeEditor(){let e=new ThemeFormView(this.config,this.customTheme);return e.onChange((()=>{this.setTheme(e.values)})),e.onSubmit((()=>{this.customTheme=e.values,this.publish("customThemeChanged")})),e}async showEditTheme(){if(!isEmpty(this.themeWindow))return void this.themeWindow.focus();let e=await this.createThemeEditor();this.themeWindow=await this.spawnWindow("Custom Theme",e,this.constructor.themeWindowWidth,this.constructor.themeWindowHeight),e.onSubmit((()=>{this.themeWindow.remove()})),this.themeWindow.onClose((()=>{delete this.themeWindow,this.resetTheme()}))}async initialize(){let e=this.application.menu.getCategory("Theme"),t=async(t,s)=>{let i=await e.addItem(t,null,t.substring(0,1).toLowerCase()),o=await i.getNode(),h=E.div().class("color-previews").content(E.span().css("background-color",s.themeColorPrimary),E.span().css("background-color",s.themeColorSecondary),E.span().css("background-color",s.themeColorTertiary));return o.prepend(h),i.onClick((()=>{this.theme=t})),i};for(let e in this.config.themes)await t(e,this.config.themes[e]);let s=await t("Custom",this.customTheme);s.onClick((()=>{this.showEditTheme()})),this.subscribe("customThemeChanged",(async()=>{let e=this.customTheme,t=(await s.getNode()).find(".color-previews");t.getChild(0).css("background-color",e.themeColorPrimary),t.getChild(1).css("background-color",e.themeColorSecondary),t.getChild(2).css("background-color",e.themeColorTertiary)})),this.resetTheme()}}export{ThemeController};
