import{ElementBuilder}from"../base/builder.mjs";import{MutexLock}from"../base/mutex.mjs";import{isEmpty,deepClone}from"../base/helpers.mjs";const E=new ElementBuilder({tabs:"enfugue-tabs",tab:"enfugue-tab",tabContent:"enfugue-tab-content"});class View{static tagName="div";static className="view";static loaderClassName="loader";static classList=[];constructor(t){this.config=t,this.additionalClasses=deepClone(this.constructor.classList),this.hidden=!1,this.lock=new MutexLock}resize(){}hide(){return this.hidden=!0,this.lock.acquire().then((t=>{void 0!==this.node&&this.node.hide(),t()})),this}show(){return this.hidden=!1,this.lock.acquire().then((t=>{void 0!==this.node&&this.node.show(),t()})),this}addClass(t){return this.additionalClasses.push(t),this.lock.acquire().then((e=>{void 0!==this.node&&this.node.addClass(t),e()})),this}removeClass(t){return this.additionalClasses=this.additionalClasses.filter((e=>e!==t)),this.lock.acquire().then((e=>{void 0!==this.node&&this.node.removeClass(t),e()})),this}hasClass(t){return-1!==this.additionalClasses.indexOf(t)||void 0!==this.node&&this.node.hasClass(t)}toggleClass(t){return this.hasClass(t)?this.removeClass(t):this.addClass(t)}async build(){return this.lock.acquire().then((t=>{let e=E.createElement(this.constructor.tagName);isEmpty(this.constructor.className)||e.addClass(this.constructor.className);for(let t of this.additionalClasses)e.addClass(t);return this.hidden&&e.hide(),t(),e}))}async getNode(){return void 0===this.node&&(this.node=await this.build()),this.node}async render(){return(await this.getNode()).render()}async loading(){return this.addClass(this.constructor.loaderClassName).addClass("loading").addClass("disabled"),this}async doneLoading(){return this.removeClass("loading").removeClass("disabled"),this}}class ShadowView extends View{static tagName=null;async build(){return this.lock.acquire().then((t=>{let e=E.createShadow();return t(),e}))}}class ParentView extends View{constructor(t){super(t),this.children=[]}getChild(t){return this.children[t]}isEmpty(){return 0===this.children.length}async empty(){return this.children=[],void 0!==this.node&&this.node.empty(),this}async insertChild(t,e){let s;if("function"==typeof e)s=new e(this.config,...Array.from(arguments).slice(2));else{if(!(e instanceof View))throw"Cannot add child of type "+typeof e;s=e}return s.parent=this,this.children=this.children.slice(0,t).concat([s]).concat(this.children.slice(t)),void 0!==this.node&&this.node.insert(t,await s.getNode()),s}async addChild(t){return this.insertChild(this.children.length,t,...Array.from(arguments).slice(1))}removeChild(t){for(let e of this.children)if(e==t)return this.children=this.children.filter((e=>e!=t)),void(void 0!==this.node&&this.node.remove(t.node));throw"Cannot find child to remove."}async build(){let t=await super.build();for(let e of this.children)t.append(await e.getNode());return t}}class TabbedView extends View{static tagName="enfugue-tabbed-view";constructor(t){super(t),this.tabs={},this.activeTab=null}async addTab(t,e){this.tabs[t]=e}async activateTab(t){if(this.activeTab=t,void 0!==this.node){let e=this.node.findAll(E.getCustomTag("tab"));for(let s of e)s.getText()==t?s.addClass("active"):s.removeClass("active");let s=this.tabs[t];s instanceof View&&(s=await s.getNode()),this.node.find(E.getCustomTag("tabContent")).content(s)}return this}async build(){let t=await super.build(),e=E.tabs(),s=E.tabContent();for(let t in this.tabs){let i=E.tab().content(t).on("click",(()=>this.activateTab(t)));if(e.append(i),null===this.activeTab&&(this.activeTab=t),this.activeTab===t){i.addClass("active");let e=this.tabs[t];e instanceof View&&(e=await e.getNode()),s.content(e)}}return t.content(e,s)}}export{View,ParentView,TabbedView,ShadowView};
