/**
 * SidebarRegistry manages registration and lookup of sidebars.
 *
 * Sidebars are panels that can be opened within the Editor workspace.
 * This registry provides a clean API for registering sidebars from core and plugins.
 */
export class SidebarRegistry {
    sidebars = new Map();
    /**
     * Register a sidebar definition.
     * @param sidebar The sidebar definition to register
     */
    register(sidebar) {
        if (this.sidebars.has(sidebar.id)) {
            console.warn(`SidebarRegistry: Sidebar "${sidebar.id}" is already registered. Overwriting.`);
        }
        this.sidebars.set(sidebar.id, sidebar);
    }
    /**
     * Register multiple sidebars at once.
     * @param sidebars Array of sidebar definitions
     */
    registerAll(sidebars) {
        for (const sidebar of sidebars) {
            this.register(sidebar);
        }
    }
    /**
     * Get a sidebar by ID.
     * @param id The sidebar ID
     * @returns The sidebar definition or undefined if not found
     */
    get(id) {
        return this.sidebars.get(id);
    }
    /**
     * Get all registered sidebars.
     * @returns Array of all sidebar definitions
     */
    getAll() {
        return Array.from(this.sidebars.values());
    }
    /**
     * Get all sidebars for a specific position.
     * @param position "left" or "right"
     * @returns Array of sidebar definitions for that position
     */
    getByPosition(position) {
        return this.getAll()
            .filter((sidebar) => sidebar.position === position)
            .sort((a, b) => (a.sortOrder ?? 100) - (b.sortOrder ?? 100));
    }
    /**
     * Get all visible sidebars for the current context.
     * Filters based on hidden, visible(), requiresItem, and requiresLayout.
     * @param editContext The current edit context
     * @returns Array of visible sidebar definitions, sorted by sortOrder
     */
    getVisible(editContext) {
        const hasItem = !!editContext.item;
        const hasLayout = !!editContext.item?.hasLayout;
        return this.getAll()
            .filter((sidebar) => {
            // Filter out hidden sidebars
            if (sidebar.hidden)
                return false;
            // Check dynamic visibility
            if (sidebar.visible && !sidebar.visible(editContext))
                return false;
            // Check item requirements
            if (sidebar.requiresItem && !hasItem)
                return false;
            if (sidebar.requiresLayout && !hasLayout)
                return false;
            return true;
        })
            .sort((a, b) => (a.sortOrder ?? 100) - (b.sortOrder ?? 100));
    }
    /**
     * Get visible sidebars for a specific position.
     * @param position "left" or "right"
     * @param editContext The current edit context
     * @returns Array of visible sidebar definitions for that position
     */
    getVisibleByPosition(position, editContext) {
        return this.getVisible(editContext).filter((sidebar) => sidebar.position === position);
    }
    /**
     * Resolve a sidebar definition by materializing any panel factories.
     * @param sidebar The sidebar definition
     * @param editContext The current edit context
     * @returns Resolved sidebar with all panels materialized
     */
    resolve(sidebar, editContext) {
        const resolvedPanels = sidebar.panels.map((panel) => {
            if (typeof panel === "function") {
                return panel(editContext);
            }
            return panel;
        });
        return {
            ...sidebar,
            panels: resolvedPanels,
        };
    }
    /**
     * Resolve multiple sidebars.
     * @param sidebarIds Array of sidebar IDs to resolve
     * @param editContext The current edit context
     * @returns Array of resolved sidebars (excludes not found)
     */
    resolveMany(sidebarIds, editContext) {
        return sidebarIds
            .map((id) => this.get(id))
            .filter((sidebar) => sidebar !== undefined)
            .map((sidebar) => this.resolve(sidebar, editContext));
    }
    /**
     * Check if a sidebar exists.
     * @param id The sidebar ID
     * @returns True if the sidebar is registered
     */
    has(id) {
        return this.sidebars.has(id);
    }
    /**
     * Remove a sidebar by ID.
     * @param id The sidebar ID to remove
     * @returns True if the sidebar was removed
     */
    unregister(id) {
        return this.sidebars.delete(id);
    }
    /**
     * Clear all registered sidebars.
     */
    clear() {
        this.sidebars.clear();
    }
    /**
     * Get the count of registered sidebars.
     */
    get size() {
        return this.sidebars.size;
    }
}
// Global singleton instance
let globalSidebarRegistry = null;
/**
 * Get the global sidebar registry instance.
 * Creates it if it doesn't exist.
 */
export function getSidebarRegistry() {
    if (!globalSidebarRegistry) {
        globalSidebarRegistry = new SidebarRegistry();
    }
    return globalSidebarRegistry;
}
/**
 * Reset the global sidebar registry.
 * Useful for testing.
 */
export function resetSidebarRegistry() {
    globalSidebarRegistry = null;
}
//# sourceMappingURL=SidebarRegistry.js.map