// components/minimal-editor.js

import React, { useRef } from "react";
import { Editor } from "@tinymce/tinymce-react";

function MinimalEditor(props) {
  const editorRef = useRef(null);

  const handleInit = (evt, editor) => {
    editorRef.current = editor;
    
    // Set initial content if provided
    if (props.initialData) {
      editor.setContent(props.initialData);
    }
    
    // Simple onChange handler - no debouncing, no complex logic
    editor.on('change', () => {
      if (props.onChange) {
        props.onChange(editor.getContent());
      }
    });
  };

  return (
    <Editor
      onInit={handleInit}
      apiKey={process.env.NEXT_PUBLIC_TINYMCE_API_KEY}
      init={{
        height: 400,
        menubar: false,
        plugins: [
          'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'preview',
          'anchor', 'searchreplace', 'visualblocks', 'code', 'fullscreen',
          'insertdatetime', 'media', 'table', 'code', 'help', 'wordcount'
        ],
        toolbar: 'undo redo | blocks | ' +
          'bold italic forecolor | alignleft aligncenter ' +
          'alignright alignjustify | bullist numlist outdent indent | ' +
          'removeformat | table | code | help',
        content_style: `
          body { 
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; 
            font-size: 14px; 
            line-height: 1.6; 
            color: #333; 
          }
          .mce-content-body { 
            padding: 16px; 
            min-height: 368px; 
          }
        `,
        placeholder: 'Start typing...',
        branding: false,
        elementpath: false,
        resize: false,
        statusbar: false,
        // Minimal settings to prevent cursor jumping
        auto_focus: false,
        forced_root_block: 'p',
        entity_encoding: 'raw',
        // Disable problematic features
        verify_html: false,
        cleanup: false,
        cleanup_on_startup: false,
        auto_resize: false,
        // Basic content handling
        paste_as_text: false,
        paste_enable_default_filters: true,
        // Mobile support
        mobile: {
          theme: 'silver',
          plugins: ['lists', 'autolink', 'link', 'image', 'table'],
          toolbar: 'bold italic | bullist numlist | link image'
        }
      }}
    />
  );
}

export default MinimalEditor; 