import { UMB_MENU_CONTEXT } from '../../menu/constants.js';
import { UmbControllerBase } from '../../../../../../libs/class-api/index.js';
import { createObservablePart, } from '../../../../../../libs/observable-api/index.js';
import { UmbEntityExpansionManager } from '../../../../utils/index.js';
/**
 * Manages the expansion state of a menu item
 * @exports
 * @class UmbMenuItemExpansionManager
 * @augments {UmbControllerBase}
 */
export class UmbMenuItemExpansionManager extends UmbControllerBase {
    #manager;
    #menuItemAlias;
    #menuContext;
    #observerController;
    constructor(host) {
        super(host);
        this.#manager = new UmbEntityExpansionManager(this);
        this.expansion = this.#manager.expansion;
        this.consumeContext(UMB_MENU_CONTEXT, (menuContext) => {
            this.#menuContext = menuContext;
            this.#observeMenuExpansion();
        });
    }
    #observeMenuExpansion() {
        if (!this.#menuContext || !this.#menuItemAlias) {
            this.#observerController?.destroy();
            return;
        }
        this.#observerController = this.observe(createObservablePart(this.#menuContext.expansion.expansion, (items) => items?.filter((item) => item.menuItemAlias === this.#menuItemAlias) || []), (itemsForMenuItem) => {
            this.#manager.setExpansion(itemsForMenuItem);
        }, 'observeMenuExpension');
    }
    setMenuItemAlias(alias) {
        this.#menuItemAlias = alias;
        this.#observeMenuExpansion();
    }
    /**
     * Checks if an entry is expanded
     * @param {UmbMenuItemExpansionEntryModel} entry The entry to check
     * @returns {Observable<boolean>} True if the entry is expanded
     * @memberof UmbSectionSidebarMenuSectionExpansionManager
     */
    isExpanded(entry) {
        return this.#manager.isExpanded(entry);
    }
    /**
     * Sets the expansion state
     * @param {UmbEntityExpansionModel<UmbMenuItemExpansionEntryModel> | undefined} expansion The expansion state
     * @memberof UmbSectionSidebarMenuSectionExpansionManager
     * @returns {void}
     */
    setExpansion(expansion) {
        this.#manager.setExpansion(expansion);
        this.#menuContext?.expansion.setExpansion(expansion);
    }
    /**
     * Gets the expansion state
     * @memberof UmbSectionSidebarMenuSectionExpansionManager
     * @returns {UmbEntityExpansionModel<UmbMenuItemExpansionEntryModel>} The expansion state
     */
    getExpansion() {
        return this.#manager.getExpansion();
    }
    /**
     * Opens a menu item
     * @param {UmbMenuItemExpansionEntryModel} entry The item to open
     * @memberof UmbSectionSidebarMenuSectionExpansionManager
     * @returns {Promise<void>}
     */
    async expandItem(entry) {
        this.#manager.expandItem(entry);
        this.#menuContext?.expansion.expandItem(entry);
    }
    /**
     * Expands multiple entities
     * @param {UmbEntityExpansionModel<UmbMenuItemExpansionEntryModel>} entries The entities to open
     * @memberof UmbEntityExpansionManager
     * @returns {void}
     */
    expandItems(entries) {
        this.#manager.expandItems(entries);
        this.#menuContext?.expansion.expandItems(entries);
    }
    /**
     * Closes a menu item
     * @param {UmbMenuItemExpansionEntryModel} entry The item to close
     * @memberof UmbSectionSidebarMenuSectionExpansionManager
     * @returns {Promise<void>}
     */
    async collapseItem(entry) {
        this.#manager.collapseItem(entry);
        this.#menuContext?.expansion.collapseItem(entry);
    }
    /**
     * Closes all menu items
     * @memberof UmbSectionSidebarMenuSectionExpansionManager
     * @returns {Promise<void>}
     */
    async collapseAll() {
        const localEntries = this.#manager.getExpansion();
        this.#manager.collapseAll();
        this.#menuContext?.expansion.collapseItems(localEntries);
    }
}
