import { BimButtonGroup } from '../components/button-group';
import type { ButtonGroupOptions } from '../components/button-group/index.type';
import type { ThemeConfig } from '../themes/types';
import { BimComponent } from '../core/component';
import type { BimEngine } from '../bim-engine';

/**
 * 通用按钮组管理器 (ButtonGroupManager)
 * 负责创建和管理通用的按钮组实例。
 */
export class ButtonGroupManager extends BimComponent {
    private groups: Map<string, BimButtonGroup> = new Map();
    private container: HTMLElement;

    constructor(engine: BimEngine, container: HTMLElement) {
        super(engine);
        this.container = container;
    }

    public create(id: string, options: Omit<ButtonGroupOptions, 'container'>): BimButtonGroup {
        const group = new BimButtonGroup({
            container: this.container,
            ...options
        });

        // @ts-ignore
        group.setEngine(this.engine);

        group.init();
        this.groups.set(id, group);
        return group;
    }

    public get(id: string): BimButtonGroup | undefined {
        return this.groups.get(id);
    }

    public updateTheme(theme: ThemeConfig) {
        this.groups.forEach(group => group.setTheme(theme));
    }

    public destroy() {
        this.groups.forEach(group => group.destroy());
        this.groups.clear();
    }
}
