import { ToolbarName } from "../../../modules/toolbars/constants.js";
import { commonmark, defaultPreset, full, yfm, zero } from "../../../modules/toolbars/presets.js";
import { ToolbarDataType } from "../types.js";
import { flattenPreset } from "./flattenPreset.js";
const defaultPresets = {
    zero,
    commonmark,
    default: defaultPreset,
    yfm,
    full,
};
const transformItem = (type, item, id = 'unknown') => {
    if (!item) {
        console.warn(`Toolbar item "${id}" not found, it might not have been added to the items dictionary.`);
        return {};
    }
    const isListButton = item.view.type === ToolbarDataType.ListButton;
    const isSingleButton = item.view.type === ToolbarDataType.SingleButton;
    return {
        type: item.view.type ?? ToolbarDataType.SingleButton,
        id,
        title: item.view.title,
        hint: item.view.hint,
        icon: item.view.icon,
        hotkey: item.view.hotkey,
        doNotActivateList: item.view.doNotActivateList,
        ...(isSingleButton && { preview: item.view.preview }),
        ...(isListButton && { withArrow: item.view.withArrow }),
        ...(type === 'wysiwyg' && item.wysiwyg && { ...item.wysiwyg }),
        ...(type === 'markup' && item.markup && { ...item.markup }),
    };
};
export const createToolbarConfig = (editorType, toolbarPreset, toolbarName) => {
    const preset = typeof toolbarPreset === 'string'
        ? defaultPresets[toolbarPreset] || defaultPresets.default
        : toolbarPreset;
    const orders = preset.orders[toolbarName] ?? [[]];
    const { items } = preset;
    const toolbarData = orders.map((group) => group.map((action) => {
        return typeof action === 'string'
            ? transformItem(editorType, items[action], action)
            : {
                ...transformItem(editorType, items[action.id], action.id),
                data: action.items.map((id) => transformItem(editorType, items[id], id)),
            };
    }));
    return toolbarData;
};
export const getToolbarsConfigs = ({ toolbarsPreset, props, preset }) => {
    const wysiwygToolbarConfig = toolbarsPreset
        ? createToolbarConfig('wysiwyg', toolbarsPreset, ToolbarName.wysiwygMain)
        : props.wysiwygToolbarConfig ??
            createToolbarConfig('wysiwyg', preset, ToolbarName.wysiwygMain);
    const markupToolbarConfig = toolbarsPreset
        ? createToolbarConfig('markup', toolbarsPreset, ToolbarName.markupMain)
        : props.markupToolbarConfig ??
            createToolbarConfig('markup', preset, ToolbarName.markupMain);
    const wysiwygHiddenActionsConfig = toolbarsPreset
        ? flattenPreset(createToolbarConfig('wysiwyg', toolbarsPreset, ToolbarName.wysiwygHidden))
        : props.wysiwygHiddenActionsConfig ??
            flattenPreset(createToolbarConfig('wysiwyg', preset, ToolbarName.wysiwygHidden));
    const markupHiddenActionsConfig = toolbarsPreset
        ? flattenPreset(createToolbarConfig('markup', toolbarsPreset, ToolbarName.markupHidden))
        : props.markupHiddenActionsConfig ??
            flattenPreset(createToolbarConfig('markup', preset, ToolbarName.markupHidden));
    return {
        wysiwygToolbarConfig,
        markupToolbarConfig,
        wysiwygHiddenActionsConfig,
        markupHiddenActionsConfig,
    };
};
//# sourceMappingURL=toolbarsConfigs.js.map
