import { UMB_SECTION_SIDEBAR_MENU_GLOBAL_CONTEXT } from '../../global-context/section-sidebar-menu.global-context.token.js';
import { UmbControllerBase } from '../../../../../../libs/class-api/index.js';
import { UMB_SECTION_CONTEXT } from '../../../../section/index.js';
import { UmbEntityExpansionManager } from '../../../../utils/index.js';
/**
 * Manages the expansion state of a section sidebar menu.
 * @exports
 * @class UmbSectionSidebarMenuSectionExpansionManager
 * @augments {UmbControllerBase}
 */
export class UmbSectionSidebarMenuSectionExpansionManager extends UmbControllerBase {
    #manager;
    #sectionContext;
    #globalContext;
    #currentSectionAlias;
    constructor(host) {
        super(host);
        this.#manager = new UmbEntityExpansionManager(this);
        this.expansion = this.#manager.expansion;
        this.consumeContext(UMB_SECTION_CONTEXT, (sectionContext) => {
            this.#sectionContext = sectionContext;
            this.#observeCurrentSectionAlias();
        });
        this.consumeContext(UMB_SECTION_SIDEBAR_MENU_GLOBAL_CONTEXT, (globalContext) => {
            this.#globalContext = globalContext;
            this.#observeGlobalMenuExpansion();
        });
    }
    #observeCurrentSectionAlias() {
        this.observe(this.#sectionContext?.alias, (alias) => {
            if (!alias)
                return;
            this.#currentSectionAlias = alias;
        });
    }
    #observeGlobalMenuExpansion() {
        if (!this.#globalContext || !this.#currentSectionAlias)
            return;
        this.observe(this.#globalContext?.expansion.expansionBySectionAlias(this.#currentSectionAlias), (expansion) => {
            this.#manager.setExpansion(expansion);
        });
    }
    /**
     * 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);
        const entries = this.#bindEntriesToSection(expansion);
        this.#globalContext?.expansion.setExpansion(entries);
    }
    /**
     * 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);
        const entries = this.#bindEntriesToSection([entry]);
        this.#globalContext?.expansion.expandItem(entries[0]);
    }
    /**
     * Expands multiple entities
     * @param {UmbEntityExpansionModel<UmbMenuItemExpansionEntryModel>} entries The entities to open
     * @memberof UmbEntityExpansionManager
     * @returns {void}
     */
    expandItems(entries) {
        this.#manager.expandItems(entries);
        const entriesWithSectionAlias = this.#bindEntriesToSection(entries);
        this.#globalContext?.expansion.expandItems(entriesWithSectionAlias);
    }
    /**
     * Closes a menu item
     * @param {UmbMenuItemExpansionEntryModel} entry The item to close
     * @memberof UmbSectionSidebarMenuSectionExpansionManager
     * @returns {Promise<void>}
     */
    async collapseItem(entry) {
        this.#manager.collapseItem(entry);
        const entries = this.#bindEntriesToSection([entry]);
        this.#globalContext?.expansion.collapseItem(entries[0]);
    }
    /**
     * Closes all menu items
     * @memberof UmbSectionSidebarMenuSectionExpansionManager
     * @returns {Promise<void>}
     */
    async collapseAll() {
        // Collapse all items in the global context matching the current section
        const entries = this.#bindEntriesToSection(this.#manager.getExpansion());
        this.#manager.collapseAll();
        this.#globalContext?.expansion.collapseItems(entries);
    }
    #bindEntriesToSection(expansion) {
        return expansion.map((item) => ({
            ...item,
            sectionAlias: this.#currentSectionAlias,
        }));
    }
}
