var e;import{__decorate as t}from"tslib";import i from"@arcgis/core/core/Collection";import{EntityBase as r}from"../Entity.js";import{InvalidOperationError as s}from"../InvalidOperationError.js";import{isMenuItem as n,ItemType as o}from"../ItemType.js";import{serializable as m}from"../support/Serializable.js";import{CollectionProxy as a}from"../utilities/CollectionProxy.js";import{checkArg as u}from"../utilities/checkArg.js";import{compare as l}from"../utilities/collection.js";import{getLogger as c}from"../utilities/log.js";import{MenuItem as p}from"./MenuItem.js";const d=c("geocortex.api.menus.Menu");let h=class extends r{static{e=this}get parentItem(){return this._parentItem}static factory;get title(){return this._title}set title(e){this._title=e}get items(){return this._items}_parentItem;_itemType=o.MENU;_title;_items;constructor(e){super(e),this._items=new f(this)}static create(t){if(!e.factory)throw new s("Cannot create menu: no menu factory has been registered.");return e.factory.create(t)}*_initializableChildCollections(){yield*super._initializableChildCollections(),yield this.items}_getSerializableProperties(){return{...super._getSerializableProperties(),items:{serializeModes:["initial"],default:[],deserialize:e=>{const t=new Map;for(const i of e)n(i)?t.set(i.toJSON(),i):t.set(i,void 0);l(this.items,t.keys(),{onNew:e=>{this.items.add(t.get(e)||p.create(e))},onMissing:e=>{this.items.remove(e)},onMatch:(e,i,r,s)=>{r!==s&&this.items.reorder(e,s),t.get(i)!==e&&e.assignProperties(i)},equals:(e,t)=>e.id===t.id})}},title:"initial"}}};h=e=t([m],h);export{h as Menu};class f extends a{_menu;constructor(e){super(new i),this._menu=e}add(e,t){if(u("menuItem",e).isNotMissing(),this._createsCircularReference(this._allSubmenus(e)))throw new s("Adding menu item would create circular menu dependency.");return e.menu&&(e.menu!==this._menu&&d.debug("A MenuItem can only have one parent. The MenuItem has been removed from its original Menu."),e.menu.items.remove(e)),e._menu=this._menu,super.add(e,t)}removeAt(e){const t=super.removeAt(e);return t&&delete t._menu,t}_createsCircularReference(e,t,i){const r=t??this._menu,s=i??(this._menu.parentItem?.root?this._menu.parentItem.root:this._menu);if(e.includes(r))return!0;if(r===s)return!1;const n=r.parentItem.menu;return this._createsCircularReference(e,n,s)}_allSubmenus(e){const t=new i;if(e.submenu){t.add(e.submenu);for(const i of e.submenu.items.toArray())t.addMany(this._allSubmenus(i))}return t}}h.factory={create:e=>new h(e)};