import { Arr } from '@ephox/katamari';
import * as AlloyParts from '../../parts/AlloyParts';
import * as SplitToolbarUtils from '../../toolbar/SplitToolbarUtils';
import * as SplitSlidingToolbarSchema from '../../ui/schema/SplitSlidingToolbarSchema';
import * as AddEventsBehaviour from '../behaviour/AddEventsBehaviour';
import { Coupling } from '../behaviour/Coupling';
import { Sliding } from '../behaviour/Sliding';
import { Toggling } from '../behaviour/Toggling';
import * as GuiFactory from '../component/GuiFactory';
import * as SketchBehaviours from '../component/SketchBehaviours';
import * as AlloyEvents from '../events/AlloyEvents';
import * as AlloyTriggers from '../events/AlloyTriggers';
import { Button } from './Button';
import * as Sketcher from './Sketcher';
import { Toolbar } from './Toolbar';
import { ToolbarGroup } from './ToolbarGroup';
const isOpen = (toolbar, detail) => AlloyParts.getPart(toolbar, detail, 'overflow').map(Sliding.hasGrown).getOr(false);
const toggleToolbar = (toolbar, detail) => {
    // Make sure that the toolbar needs to toggled by checking for overflow button presence
    AlloyParts.getPart(toolbar, detail, 'overflow-button')
        .bind(() => AlloyParts.getPart(toolbar, detail, 'overflow'))
        .each((overf) => {
        refresh(toolbar, detail);
        Sliding.toggleGrow(overf);
    });
};
const refresh = (toolbar, detail) => {
    AlloyParts.getPart(toolbar, detail, 'overflow').each((overflow) => {
        SplitToolbarUtils.refresh(toolbar, detail, (groups) => {
            const builtGroups = Arr.map(groups, (g) => GuiFactory.premade(g));
            Toolbar.setGroups(overflow, builtGroups);
        });
        AlloyParts.getPart(toolbar, detail, 'overflow-button').each((button) => {
            if (Sliding.hasGrown(overflow)) {
                Toggling.on(button);
            }
        });
        Sliding.refresh(overflow);
    });
};
const factory = (detail, components, spec, externals) => {
    const toolbarToggleEvent = 'alloy.toolbar.toggle';
    const doSetGroups = (toolbar, groups) => {
        const built = Arr.map(groups, toolbar.getSystem().build);
        detail.builtGroups.set(built);
    };
    return {
        uid: detail.uid,
        dom: detail.dom,
        components,
        behaviours: SketchBehaviours.augment(detail.splitToolbarBehaviours, [
            Coupling.config({
                others: {
                    overflowGroup: (toolbar) => {
                        return ToolbarGroup.sketch({
                            ...externals['overflow-group'](),
                            items: [
                                Button.sketch({
                                    ...externals['overflow-button'](),
                                    action: (_button) => {
                                        AlloyTriggers.emit(toolbar, toolbarToggleEvent);
                                    }
                                })
                            ]
                        });
                    }
                }
            }),
            AddEventsBehaviour.config('toolbar-toggle-events', [
                AlloyEvents.run(toolbarToggleEvent, (toolbar) => {
                    toggleToolbar(toolbar, detail);
                })
            ])
        ]),
        apis: {
            setGroups: (toolbar, groups) => {
                doSetGroups(toolbar, groups);
                refresh(toolbar, detail);
            },
            refresh: (toolbar) => refresh(toolbar, detail),
            toggle: (toolbar) => toggleToolbar(toolbar, detail),
            isOpen: (toolbar) => isOpen(toolbar, detail)
        },
        domModification: {
            attributes: { role: 'group' }
        }
    };
};
const SplitSlidingToolbar = Sketcher.composite({
    name: 'SplitSlidingToolbar',
    configFields: SplitSlidingToolbarSchema.schema(),
    partFields: SplitSlidingToolbarSchema.parts(),
    factory,
    apis: {
        setGroups: (apis, toolbar, groups) => {
            apis.setGroups(toolbar, groups);
        },
        refresh: (apis, toolbar) => {
            apis.refresh(toolbar);
        },
        toggle: (apis, toolbar) => {
            apis.toggle(toolbar);
        },
        isOpen: (apis, toolbar) => apis.isOpen(toolbar)
    }
});
export { SplitSlidingToolbar };
//# sourceMappingURL=SplitSlidingToolbar.js.map