"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BundlePreset = void 0;
const behavior_1 = require("../extensions/behavior/index.js");
const EditorModeKeymap_1 = require("../extensions/behavior/EditorModeKeymap/index.js");
const specs_1 = require("../extensions/specs.js");
const placeholder_1 = require("../i18n/placeholder/index.js");
const commonmark_1 = require("../presets/commonmark.js");
const default_1 = require("../presets/default.js");
const full_1 = require("../presets/full.js");
const yfm_1 = require("../presets/yfm.js");
const zero_1 = require("../presets/zero.js");
const shortcuts_1 = require("../shortcuts/index.js");
const wysiwyg_1 = require("./config/wysiwyg.js");
const emoji_1 = require("./emoji.js");
const DEFAULT_IGNORED_KEYS = ['Tab', 'Shift-Tab'];
const BundlePreset = (builder, opts) => {
    builder.context.set('directiveSyntax', opts.directiveSyntax);
    const dropCursor = {
        color: 'var(--g-color-line-brand)',
        width: 2,
    };
    const zeroOptions = {
        ...opts,
        baseStyles: {
            attributes: {
                // for disable setting attrs inside pm-view from floating-ui from uikit
                // see https://github.com/floating-ui/floating-ui/discussions/3213
                // and https://github.com/floating-ui/floating-ui/pull/3202
                'aria-live': '',
                ...opts.baseStyles?.attributes,
            },
        },
        cursor: { dropOptions: dropCursor },
        clipboard: { pasteFileHandler: opts.fileUploadHandler, ...opts.clipboard },
        selectionContext: { config: wysiwyg_1.wSelectionMenuConfigByPreset.zero, ...opts.selectionContext },
        commandMenu: { actions: wysiwyg_1.wCommandMenuConfigByPreset.zero, ...opts.commandMenu },
        history: { undoKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Undo), redoKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Redo), ...opts.history },
        baseSchema: {
            paragraphKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Text),
            paragraphPlaceholder: (node, parent) => {
                const { value, behavior } = opts.placeholderOptions || {};
                const emptyEntries = {
                    'empty-row': !node.text,
                    'empty-row-top-level': !node.text && parent?.type.name === specs_1.BaseNode.Doc,
                    'empty-doc': !node.text && parent?.type.name === specs_1.BaseNode.Doc && parent.childCount === 1,
                };
                const showPlaceholder = emptyEntries[behavior || 'empty-doc'];
                if (!showPlaceholder)
                    return null;
                return typeof value === 'function'
                    ? value()
                    : value ?? (0, placeholder_1.i18n)('doc_empty');
            },
            preserveEmptyRows: opts.preserveEmptyRows,
            ...opts.baseSchema,
        },
    };
    const commonMarkOptions = {
        ...zeroOptions,
        selectionContext: {
            config: wysiwyg_1.wSelectionMenuConfigByPreset.commonmark,
            ...opts.selectionContext,
        },
        commandMenu: { actions: wysiwyg_1.wCommandMenuConfigByPreset.commonmark, ...opts.commandMenu },
        breaks: {
            preferredBreak: (opts.mdBreaks ? 'soft' : 'hard'),
            ...opts.breaks,
        },
        bold: { boldKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Bold), ...opts.bold },
        italic: { italicKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Italic), ...opts.italic },
        code: { codeKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Code), ...opts.code },
        codeBlock: {
            codeBlockKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.CodeBlock),
            ...opts.codeBlock,
        },
        blockquote: { qouteKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Quote), ...opts.blockquote },
        link: { linkKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Link), ...opts.link },
        lists: {
            ulKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.BulletList),
            olKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.OrderedList),
            ulInputRules: { plus: false },
            ...opts.lists,
        },
        image: {
            parseInsertedUrlAsImage: opts.imgSize?.parseInsertedUrlAsImage,
        },
    };
    const defaultOptions = {
        ...commonMarkOptions,
        selectionContext: { config: wysiwyg_1.wSelectionMenuConfigByPreset.default, ...opts.selectionContext },
        commandMenu: { actions: wysiwyg_1.wCommandMenuConfigByPreset.default, ...opts.commandMenu },
        strike: { strikeKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Strike), ...opts.strike },
    };
    const yfmOptions = {
        ...defaultOptions,
        yfmConfigs: { disableAttrs: opts.disableMdAttrs, ...opts.yfmConfigs },
        selectionContext: { config: wysiwyg_1.wSelectionMenuConfigByPreset.yfm, ...opts.selectionContext },
        commandMenu: { actions: wysiwyg_1.wCommandMenuConfigByPreset.yfm, ...opts.commandMenu },
        underline: { underlineKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Underline), ...opts.underline },
        imgSize: {
            imageUploadHandler: opts.fileUploadHandler,
            needToSetDimensionsForUploadedImages: opts.needToSetDimensionsForUploadedImages,
            enableNewImageSizeCalculation: opts.enableNewImageSizeCalculation,
            ...opts.imgSize,
        },
        checkbox: { checkboxLabelPlaceholder: () => (0, placeholder_1.i18n)('checkbox'), ...opts.checkbox },
        deflist: {
            deflistTermPlaceholder: () => (0, placeholder_1.i18n)('deflist_term'),
            deflistDescPlaceholder: () => (0, placeholder_1.i18n)('deflist_desc'),
        },
        yfmCut: {
            yfmCutKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Cut),
            yfmCutTitlePlaceholder: () => (0, placeholder_1.i18n)('cut_title'),
            yfmCutContentPlaceholder: () => (0, placeholder_1.i18n)('cut_content'),
            ...opts.yfmCut,
        },
        yfmNote: {
            yfmNoteKey: shortcuts_1.formatter.toPM(shortcuts_1.Action.Note),
            yfmNoteTitlePlaceholder: () => (0, placeholder_1.i18n)('note_title'),
            ...opts.yfmNote,
        },
        yfmTable: { yfmTableCellPlaceholder: () => (0, placeholder_1.i18n)('table_cell'), ...opts.yfmTable },
        yfmFile: {
            fileUploadHandler: opts.fileUploadHandler,
            needToSetDimensionsForUploadedImages: opts.needToSetDimensionsForUploadedImages,
            ...opts.yfmFile,
        },
        yfmHeading: {
            h1Key: shortcuts_1.formatter.toPM(shortcuts_1.Action.Heading1),
            h2Key: shortcuts_1.formatter.toPM(shortcuts_1.Action.Heading2),
            h3Key: shortcuts_1.formatter.toPM(shortcuts_1.Action.Heading3),
            h4Key: shortcuts_1.formatter.toPM(shortcuts_1.Action.Heading4),
            h5Key: shortcuts_1.formatter.toPM(shortcuts_1.Action.Heading5),
            h6Key: shortcuts_1.formatter.toPM(shortcuts_1.Action.Heading6),
            headingPlaceholder: (node) => `${(0, placeholder_1.i18n)('heading')} ${node.attrs[specs_1.YfmHeadingAttr.Level]}`, // todo: remove attrs import
            ...opts.yfmHeading,
        },
        placeholder: {
            [specs_1.YfmNoteNode.NoteContent]: () => (0, placeholder_1.i18n)('note_content'),
        },
    };
    const fullOptions = {
        ...yfmOptions,
        selectionContext: { config: wysiwyg_1.wSelectionMenuConfigByPreset.full, ...opts.selectionContext },
        commandMenu: { actions: wysiwyg_1.wCommandMenuConfigByPreset.full, ...opts.commandMenu },
        emoji: { defs: emoji_1.emojiDefs, ...opts.emoji },
    };
    const zeroIgnoreActions = [shortcuts_1.Action.Undo, shortcuts_1.Action.Redo];
    const commonMarkIgnoreActions = zeroIgnoreActions.concat(shortcuts_1.Action.Bold, shortcuts_1.Action.Italic, shortcuts_1.Action.Code, shortcuts_1.Action.Link, shortcuts_1.Action.Text, shortcuts_1.Action.Heading1, shortcuts_1.Action.Heading2, shortcuts_1.Action.Heading3, shortcuts_1.Action.Heading4, shortcuts_1.Action.Heading5, shortcuts_1.Action.Heading6, shortcuts_1.Action.BulletList, shortcuts_1.Action.OrderedList, shortcuts_1.Action.Quote, shortcuts_1.Action.CodeBlock);
    const defaultIgnoreActions = commonMarkIgnoreActions.concat(shortcuts_1.Action.Strike);
    const yfmIgnoreActions = defaultIgnoreActions.concat(shortcuts_1.Action.Underline, shortcuts_1.Action.Note, shortcuts_1.Action.Cut);
    const fullIgnoreActions = yfmIgnoreActions.concat();
    let ignoreActions;
    switch (opts.preset) {
        case 'zero': {
            ignoreActions = zeroIgnoreActions;
            builder.use(behavior_1.BehaviorPreset, zeroOptions).use(zero_1.ZeroPreset, zeroOptions);
            break;
        }
        case 'commonmark': {
            ignoreActions = commonMarkIgnoreActions;
            builder.use(behavior_1.BehaviorPreset, commonMarkOptions).use(commonmark_1.CommonMarkPreset, commonMarkOptions);
            break;
        }
        case 'default': {
            ignoreActions = defaultIgnoreActions;
            builder.use(behavior_1.BehaviorPreset, defaultOptions).use(default_1.DefaultPreset, defaultOptions);
            break;
        }
        case 'yfm': {
            ignoreActions = yfmIgnoreActions;
            builder.use(behavior_1.BehaviorPreset, yfmOptions).use(yfm_1.YfmPreset, yfmOptions);
            break;
        }
        default: {
            ignoreActions = fullIgnoreActions;
            builder.use(behavior_1.BehaviorPreset, fullOptions).use(full_1.FullPreset, fullOptions);
            break;
        }
    }
    const ignoreKeysList = opts.ignoreKeysList?.slice() ?? [];
    ignoreKeysList.push(...DEFAULT_IGNORED_KEYS);
    for (const action of ignoreActions) {
        const key = shortcuts_1.formatter.toPM(action);
        if (key)
            ignoreKeysList.push(key);
    }
    builder.use(EditorModeKeymap_1.EditorModeKeymap, {
        onSubmit: opts.onSubmit,
        onCancel: opts.onCancel,
        ignoreKeysList,
    });
};
exports.BundlePreset = BundlePreset;
//# sourceMappingURL=wysiwyg-preset.js.map
