"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getToolbarsConfigs = exports.createToolbarConfig = void 0;
const constants_1 = require("../../../modules/toolbars/constants.js");
const presets_1 = require("../../../modules/toolbars/presets.js");
const types_1 = require("../types.js");
const flattenPreset_1 = require("./flattenPreset.js");
const defaultPresets = {
    zero: presets_1.zero,
    commonmark: presets_1.commonmark,
    default: presets_1.defaultPreset,
    yfm: presets_1.yfm,
    full: presets_1.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 === types_1.ToolbarDataType.ListButton;
    const isSingleButton = item.view.type === types_1.ToolbarDataType.SingleButton;
    return {
        type: item.view.type ?? types_1.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,
            replaceActiveIcon: item.view.replaceActiveIcon,
        }),
        ...(type === 'wysiwyg' && item.wysiwyg && { ...item.wysiwyg }),
        ...(type === 'markup' && item.markup && { ...item.markup }),
    };
};
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;
};
exports.createToolbarConfig = createToolbarConfig;
const getToolbarsConfigs = ({ toolbarsPreset, props, preset }) => {
    const wysiwygToolbarConfig = toolbarsPreset
        ? (0, exports.createToolbarConfig)('wysiwyg', toolbarsPreset, constants_1.ToolbarName.wysiwygMain)
        : props.wysiwygToolbarConfig ??
            (0, exports.createToolbarConfig)('wysiwyg', preset, constants_1.ToolbarName.wysiwygMain);
    const markupToolbarConfig = toolbarsPreset
        ? (0, exports.createToolbarConfig)('markup', toolbarsPreset, constants_1.ToolbarName.markupMain)
        : props.markupToolbarConfig ??
            (0, exports.createToolbarConfig)('markup', preset, constants_1.ToolbarName.markupMain);
    const wysiwygHiddenActionsConfig = toolbarsPreset
        ? (0, flattenPreset_1.flattenPreset)((0, exports.createToolbarConfig)('wysiwyg', toolbarsPreset, constants_1.ToolbarName.wysiwygHidden))
        : props.wysiwygHiddenActionsConfig ??
            (0, flattenPreset_1.flattenPreset)((0, exports.createToolbarConfig)('wysiwyg', preset, constants_1.ToolbarName.wysiwygHidden));
    const markupHiddenActionsConfig = toolbarsPreset
        ? (0, flattenPreset_1.flattenPreset)((0, exports.createToolbarConfig)('markup', toolbarsPreset, constants_1.ToolbarName.markupHidden))
        : props.markupHiddenActionsConfig ??
            (0, flattenPreset_1.flattenPreset)((0, exports.createToolbarConfig)('markup', preset, constants_1.ToolbarName.markupHidden));
    return {
        wysiwygToolbarConfig,
        markupToolbarConfig,
        wysiwygHiddenActionsConfig,
        markupHiddenActionsConfig,
    };
};
exports.getToolbarsConfigs = getToolbarsConfigs;
//# sourceMappingURL=toolbarsConfigs.js.map
