/*!
 * Copyright (c) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE file in the project.
 */ import { Verb } from '@datashaper/schema';
import { ContextualMenuItemType, VerticalDivider } from '@fluentui/react';
import uniqueId from 'lodash-es/uniqueId.js';
import upperFirst from 'lodash-es/upperFirst.js';
import { useMemo } from 'react';
import { useHeaderCommandBarDefaults } from '../hooks/useHeaderCommandBarDefaults.js';
import { getVerbIcon } from '../verbIcons.js';
import { groupedColumnVerbs, groupedTableVerbs, mainColumnVerbs, mainTableVerbs } from './TableCommands.utils.js';
function getOverflowVerbItems(onCallStep, disabled, verbList, id) {
    return verbList.map((group)=>({
            key: `__section-${group.label}__`,
            itemType: ContextualMenuItemType.Section,
            sectionProps: {
                topDivider: true,
                title: group.label,
                items: group.verbs.map((verb)=>{
                    const found = Object.entries(Verb).find((v)=>v[1] === verb);
                    return {
                        key: found[1],
                        text: found[0],
                        onClick: onCallStep,
                        data: {
                            id
                        },
                        disabled: group?.alwaysEnabled ? false : disabled
                    };
                })
            }
        }));
}
function getMainVerbItems(onCallStep, verbList, disabled) {
    const items = [
        {
            key: 'divider' + uniqueId(),
            id: 'divider' + uniqueId(),
            commandBarButtonAs: VerticalDivider,
            buttonStyles: {
                wrapper: {
                    padding: '8px 0px',
                    height: '50%'
                }
            },
            itemType: ContextualMenuItemType.Divider
        }, 
    ];
    verbList.forEach((verb)=>{
        items.push({
            key: verb,
            text: upperFirst(verb),
            id: verb,
            iconProps: {
                iconName: getVerbIcon(verb)
            },
            onClick: onCallStep,
            disabled
        });
    });
    return items;
}
export function useColumnCommands(onCallStep, disabled, color, background) {
    const base = useMemo(()=>{
        const id = 'overflowColumn';
        return {
            items: getMainVerbItems(onCallStep, mainColumnVerbs, disabled),
            overflowItems: getOverflowVerbItems(onCallStep, disabled, groupedColumnVerbs, id),
            id
        };
    }, [
        onCallStep,
        disabled
    ]);
    return useHeaderCommandBarDefaults(base, false, {
        color,
        background
    });
}
export function useTableCommands(onCallStep, disabled, color, background) {
    const base = useMemo(()=>{
        const id = 'overflowTable';
        return {
            items: getMainVerbItems(onCallStep, mainTableVerbs, disabled),
            overflowItems: getOverflowVerbItems(onCallStep, disabled, groupedTableVerbs, id),
            id
        };
    }, [
        onCallStep,
        disabled
    ]);
    return useHeaderCommandBarDefaults(base, false, {
        color,
        background
    });
}
export function useUndoCommands(onUndoStep, disabled, color, background) {
    const base = useMemo(()=>({
            items: [
                {
                    key: 'undo',
                    iconOnly: true,
                    iconProps: icons.undo,
                    title: 'Undo last step',
                    onClick: onUndoStep,
                    disabled
                }, 
            ],
            styles: {
                root: {
                    width: 40
                }
            }
        }), [
        onUndoStep,
        disabled
    ]);
    return useHeaderCommandBarDefaults(base, false, {
        color,
        background
    });
}
const icons = {
    undo: {
        iconName: 'Undo'
    }
};
