import{store as t}from"../haxcms-site-store.js";import{HAXStore as e}from"../../../../hax-body/lib/hax-store.js";import{HAXCMSButton as i}from"../utils/HAXCMSButton.js";import{SimpleToolbarButtonBehaviors as s}from"../../../../simple-toolbar/lib/simple-toolbar-button.js";import{toJS as a}from"../../../../../mobx/dist/mobx.esm.js";export class HAXCMSButtonAdd extends(s(i)){static get tag(){return"haxcms-button-add"}constructor(){super(),this.windowControllers=new AbortController,this.t=this.t||{},this.t.newPageAdded="New page added",this.t.newPage="Page",this.t.copy="Copy",this.t.newChildPage="Child",this.t.duplicatePage="Clone",this.icon=null,this.voiceCommand="add page",this.type="sibling",this.autoEdit=!1,this.showTextLabel=!1,this.iconPosition="left",this.actionId=null,this.alignHorizontal="left",this.addEventListener("click",this.HAXCMSButtonClick)}static get styles(){return[super.styles,...this.iconStyles,...this.labelStyles,...this.tooltipStyles,...this.simpleButtonCoreStyles,...this.simpleButtonLayoutStyles,...this.simpleButtonThemeStyles]}static get properties(){return{...super.properties,autoEdit:{type:Boolean,attribute:"auto-edit"},actionId:{type:String,attribute:"action-id"},type:{type:String}}}firstUpdated(t){switch(super.firstUpdated(t),this.type){case"sibling":this.icon||(this.icon="hax:add-page"),this.label||(this.label=this.t.newPage);break;case"child":this.icon||(this.icon="hax:add-child-page"),this.label||(this.label=this.t.newChildPage);break;case"duplicate":this.icon||(this.icon="hax:duplicate"),this.label||(this.label=this.t.duplicatePage)}}render(){return this.buttonTemplate}connectedCallback(){super.connectedCallback(),globalThis.addEventListener("haxcms-create-node-success",this.HAXCMSButtonClickResponse.bind(this),{signal:this.windowControllers.signal})}disconnectedCallback(){this.windowControllers.abort(),super.disconnectedCallback()}async HAXCMSButtonClick(){t.playSound("click");let e=null,i=this.t.newPage,s=null,l={};if(l=this.actionId?"null"==this.actionId||null==this.actionId?a(await t.getLastChildItem(null)):await t.findItemAsObject(this.actionId):a(t.activeItem),l)if("sibling"===this.type)s=l.parent,e=parseInt(l.order)+1;else if("child"===this.type){s=l.id;let i=a(await t.getLastChildItem(l.id));e=0,i.order&&(e=parseInt(i.order)+1)}else"duplicate"===this.type&&(i=l.title+" "+this.t.copy,s=l.parent,e=parseInt(l.order)+1);""===i&&(i="New");var o={node:{title:i,location:""},order:e,parent:s};"duplicate"===this.type&&(o.node.duplicate=l.id),this.HAXCMSFireButtonEvent("haxcms-create-node",this,o)}HAXCMSButtonClickResponse(i){this===i.detail.originalTarget&&this.autoEdit&&(e.haxTray.collapsed=!1,setTimeout((()=>{t.editMode=!0}),250))}}customElements.define(HAXCMSButtonAdd.tag,HAXCMSButtonAdd);