import { Editor } from '@tiptap/core'
import { StarterKit } from '@tiptap/starter-kit'
import { Link } from '@tiptap/extension-link'
import { Image } from '@tiptap/extension-image'
import { Table } from '@tiptap/extension-table'
import { TableRow } from '@tiptap/extension-table-row'
import { TableHeader } from '@tiptap/extension-table-header'
import { TableCell } from '@tiptap/extension-table-cell'
import { TaskList } from '@tiptap/extension-task-list'
import { TaskItem } from '@tiptap/extension-task-item'
import { Youtube } from '@tiptap/extension-youtube'
import { CodeBlockLowlight } from '@tiptap/extension-code-block-lowlight'
import { lowlight } from 'lowlight'

try {
    window.setupRichEditor = (element, content, onUpdate) => {
        const editor = new Editor({
            element: element,
            extensions: [
                StarterKit.configure({
                    codeBlock: false,
                    heading: {
                        levels: [1, 2, 3],
                    },
                }),
                Link.configure({
                    openOnClick: false,
                }),
                Image,
                Table.configure({
                    resizable: true,
                }),
                TableRow,
                TableHeader,
                TableCell,
                TaskList,
                TaskItem.configure({
                    nested: true,
                }),
                Youtube,
                CodeBlockLowlight.configure({
                    lowlight,
                }),
            ],
            content: content,
            onUpdate({ editor }) {
                onUpdate(editor.getHTML())
            },
            editorProps: {
                attributes: {
                    class: 'prose dark:prose-invert prose-sm sm:prose-base focus:outline-none max-w-none min-h-[200px] px-0 py-3',
                },
            },
        })

        return editor
    }
    console.log('✅ Task Editor loaded successfully');
} catch (error) {
    console.error('❌ Failed to load Task Editor:', error);
}
