var e;import{__decorate as t}from"tslib";import i from"@arcgis/core/core/Collection";import{EntityBase as s}from"../Entity.js";import{InvalidOperationError as n}from"../InvalidOperationError.js";import{isMenu as r,ItemType as a}from"../ItemType.js";import{serializable as o}from"../support/Serializable.js";import{objectKeys as u}from"../utilities/object.js";import{Menu as l}from"./Menu.js";let h=class extends s{static{e=this}static factory;_menu;_itemType=a.MENU_ITEM;_isToggled=!1;_submenu;_iconId;_isEnabled=!0;_description;_title;_hideOnDisable=!1;_action;_alternate;constructor(e){super(e),this._isToggled=e?.isToggled??!1}get canBeToggled(){return!!this.alternate}get isToggled(){return this._isToggled}get submenu(){return this._submenu}set submenu(e){if(e&&this._createsCircularReference(this._allSubmenus(e)))throw new n("Menu is already part of the menu hierarchy.");this._submenu&&delete this._submenu._parentItem,this._submenu=e,this._submenu&&(this._submenu._parentItem=this)}get menu(){return this._menu}get root(){return this.menu?.parentItem?.root?this.menu.parentItem.root:this.menu}get iconId(){return this._iconId}set iconId(e){this._iconId=e}get title(){return this._title}set title(e){this._title=e}get description(){return this._description}set description(e){this._description=e}get isEnabled(){return this._isEnabled}set isEnabled(e){this._isEnabled=e}get hideOnDisable(){return this._hideOnDisable}set hideOnDisable(e){this._hideOnDisable=e}get action(){return this._action}set action(e){this._action=e}get alternate(){return this._alternate}set alternate(e){this._alternate=e}static create(t){if(!e.factory)throw new n("Cannot create menu item: no menu item factory has been registered.");return e.factory.create(t)}toggle(){if(!this.canBeToggled)throw new n("This menu item cannot be toggled. Set `alternate` to enable toggling.");this._isToggled=!this._isToggled;for(const e of u(this.alternate)){const t=this.alternate[e];this.alternate[e]=this[e],this[e]=t}}async _onInitialize(){await super._onInitialize(),this.submenu&&await this.submenu.initialize()}_getSerializableProperties(){return{...super._getSerializableProperties(),iconId:"initial",title:"initial",description:"initial",isEnabled:{serializeModes:["initial"],default:!0},hideOnDisable:{serializeModes:["initial"],default:!1},isToggled:{serializeModes:["initial"],deserialize:!1,default:!1},action:"initial",alternate:{serializeModes:["initial"],deserialize:e=>{this.alternate=e?{...e}:void 0}},submenu:{serializeModes:["initial"],deserialize:e=>{e?r(e)?this.submenu=e:this.submenu?this.submenu.assignProperties(e):this.submenu=l.create(e):this.submenu=void 0}}}}_createsCircularReference(e,t,i){if(!t&&!this.menu)return!1;const s=t??this.menu,n=i??(this.menu.parentItem?.root?this.menu.parentItem.root:this.menu);if(e.includes(s))return!0;if(s===n)return!1;const r=s.parentItem.menu;return this._createsCircularReference(e,r,n)}_allSubmenus(e){const t=new i;t.add(e);for(const i of e.items.toArray())i.submenu&&t.addMany(this._allSubmenus(i.submenu));return t}};h=e=t([o],h);export{h as MenuItem};h.factory={create:e=>new h(e)};