import{View,ParentView}from"./base.mjs";import{ElementBuilder}from"../base/builder.mjs";import{isEmpty}from"../base/helpers.mjs";const E=new ElementBuilder({categoryHeader:"enfugue-menu-category-header",categoryItems:"enfugue-menu-category-items",categoryButton:"enfugue-menu-category-button",toolbarOperations:"enfugue-toolbar-operations",toolbarInputs:"enfugue-toolbar-inputs",toolbarItem:"enfugue-toolbar-item"});class MenuView extends ParentView{static tagName="enfugue-menu";hideCategories(){for(let e of this.children)e instanceof MenuCategoryView&&e.removeClass("active")}toggleCategory(e){let t,i=!1;for(let a of this.children)a instanceof MenuCategoryView&&(a.name===e?(i=!0,t=!a.hasClass("active"),a.toggleClass("active")):a.removeClass("active"));return i||console.warn("No category named",e),t}addCategory(e){let t=0;for(;this.children[t]instanceof MenuCategoryView;)t++;return this.insertChild(t,MenuCategoryView,e)}async build(){let e=await super.build();return e.on("click",(e=>e.stopPropagation())),window.addEventListener("click",(()=>this.hideCategories())),e}}class MenuCategoryView extends ParentView{static tagName="enfugue-menu-category";constructor(e,t){super(e),this.name=t,this.buttons=[]}setName(e){this.name=e,void 0!==this.node&&this.node.find(E.getCustomTag("categoryHeader")).find("span").content(this.name)}addButton(e,t){let i=E.categoryButton().content(E.i().class(e)).on("click",(e=>{e.preventDefault(),e.stopPropagation(),t()}));return this.buttons.push(i),void 0!==this.node&&this.node.find(E.getCustomTag("categoryHeader")).append(i),i}async insertChild(e,t){let i=new t(this.config,...Array.from(arguments).slice(2));return this.children=this.children.slice(0,e).concat([i]).concat(this.children.slice(e)),void 0!==this.node&&this.node.insert(e+1,await i.getNode()),i}async addCategory(e){let t=0;for(;this.children[t]instanceof MenuCategoryView;)t++;return this.insertChild(t,MenuCategoryView,e)}async addItem(e,t){return this.addChild(IconItemView,e,t)}async build(){let e=await super.build(),t=E.categoryHeader().content(E.span().content(this.name));for(let e of this.buttons)t.append(e);return e.prepend(t).on("click",(()=>this.parent.toggleCategory(this.name))),e}}class MenuItemView extends View{static tagName="enfugue-menu-item";constructor(e,t){super(e),this.name=t,this.callbacks=[]}onClick(e){this.callbacks.push(e)}setName(e){this.name=e,void 0!==this.node&&this.node.find("span").content(this.name)}async build(){let e=await super.build();return e.prepend(E.span().content(this.name)),e.on("click",(async t=>{if(t.preventDefault(),!e.hasClass("disabled")){e.addClass("disabled").addClass("loading");for(let e of this.callbacks)await e();e.removeClass("disabled").removeClass("loading")}})),e}}class IconItemView extends MenuItemView{constructor(e,t,i){super(e,t),this.icon=i}async build(){let e=await super.build();return isEmpty(this.icon)||(this.icon.startsWith("http")||this.icon.startsWith("/")?e.prepend(E.img().src(this.icon)):e.prepend(E.i().class(this.icon))),e}}class SidebarView extends ParentView{static tagName="enfugue-sidebar";addItem(e,t){return this.addChild(SidebarItemView,e,t)}}class SidebarItemView extends IconItemView{static tagName="enfugue-sidebar-item"}class ToolbarView extends ParentView{static tagName="enfugue-toolbar";addItem(e,t){return this.addChild(ToolbarItemView,e,t)}}class ToolbarItemView extends IconItemView{static tagName="enfugue-toolbar-item";async build(){let e=await super.build();return e.data("tooltip",this.name),e}}export{MenuView,MenuItemView,MenuCategoryView,IconItemView,SidebarView,SidebarItemView,ToolbarView,ToolbarItemView};
