/* eslint-disable */
import { assertExists } from '@blockeditor/global/utils';
import { INLINE_ROOT_ATTR } from '@blockeditor/inline';
import { AIIcon } from '../../icons/index.js';
import { toggleAIPopup } from '../../inline/presets/nodes/ai-node/ai-popup/toggle-ai-popup.js';
import { commonActiveWhen } from '../text-format/utils.js';
import { FORMAT_NATIVE_SUPPORT_FLAVOURS } from '../text-format/consts.js';
import { BLOCK_ID_ATTR } from '../../consts.js';
export const aiPolishConfigs = [
    {
        id: 'ai',
        name: 'Ask AI',
        icon: AIIcon,
        hotkey: 'Mod-a',
        activeWhen: host => commonActiveWhen(host, 'ai'),
        action: host => {
            let result = [];
            host.std.command
                .pipe()
                .withHost()
                .try(chain => [
                chain
                    .getTextSelection()
                    .getSelectedBlocks()
                    .inline((ctx, next) => {
                    const { currentTextSelection, selectedBlocks } = ctx;
                    assertExists(currentTextSelection);
                    assertExists(selectedBlocks);
                    if (currentTextSelection.isCollapsed())
                        return;
                    const selectedInlineEditors = selectedBlocks.flatMap(el => {
                        const inlineRoot = el.querySelector(`[${INLINE_ROOT_ATTR}]`);
                        if (inlineRoot && inlineRoot.inlineEditor.getInlineRange()) {
                            return inlineRoot.inlineEditor;
                        }
                        return [];
                    });
                    result = selectedInlineEditors
                        .filter(inlineEditor => {
                        if (!inlineEditor)
                            return false;
                        const targetInlineRange = inlineEditor.getInlineRange();
                        assertExists(targetInlineRange);
                        if (targetInlineRange.length === 0)
                            return false;
                        return inlineEditor;
                    })
                        .map(inlineEditor => inlineEditor);
                    next();
                }),
                chain.inline(() => {
                    const selectedInlineEditors = Array.from(host.querySelectorAll(`[${INLINE_ROOT_ATTR}]`))
                        .filter(el => {
                        const selection = document.getSelection();
                        if (!selection || selection.rangeCount === 0)
                            return false;
                        const range = selection.getRangeAt(0);
                        return range.intersectsNode(el);
                    })
                        .filter(el => {
                        const blockElement = el.closest(`[${BLOCK_ID_ATTR}]`);
                        if (blockElement) {
                            return FORMAT_NATIVE_SUPPORT_FLAVOURS.includes(blockElement.model.flavour);
                        }
                        return false;
                    })
                        .filter(el => {
                        const inlineEditor = el.inlineEditor;
                        const targetInlineRange = inlineEditor.getInlineRange();
                        assertExists(targetInlineRange);
                        if (targetInlineRange.length === 0)
                            return false;
                        return el;
                    })
                        .map(el => el.inlineEditor);
                    result = selectedInlineEditors;
                }),
            ])
                .run();
            let targetInlineRanges = [];
            result.forEach(inlineEditor => {
                const targetInlineRange = inlineEditor.getInlineRange();
                assertExists(targetInlineRange);
                targetInlineRanges.push(targetInlineRange);
            });
            toggleAIPopup(result, 'create', targetInlineRanges);
            // const selection = document.getSelection();
            // if (!selection || selection.rangeCount === 0) return;
            // const range = selection.getRangeAt(0);
            // const inlineRoot = range.commonAncestorContainer.parentElement?.closest<
            // 	InlineRootElement<AffineTextAttributes>
            // >(`[${INLINE_ROOT_ATTR}]`);
            // if (!inlineRoot) return;
            // const inlineEditor = inlineRoot.inlineEditor;
            // const targetInlineRange = inlineEditor.getInlineRange();
            // assertExists(targetInlineRange);
            // if (targetInlineRange.length === 0) return;
            // const format = inlineEditor.getFormat(targetInlineRange);
            // if (format.ai) {
            // 	inlineEditor.formatText(targetInlineRange, { ai: null });
            // 	return;
            // }
            // toggleAIPopup(inlineEditor, 'create', targetInlineRange);
        },
    },
];
//# sourceMappingURL=config.js.map