import React, { useRef } from 'react'
import { Html } from '../components/html-render/task-html'
import { PPTEditToolBar } from '../components/toolbar-web'
import { PPTEditProvider } from '../context'
import { HtmlWithEditMode } from './baseEdit'
import type { TaskArtifact } from '@/components/nova-sdk/types'
import { useLoadContent } from '../hooks/useLoadContent'
import type { ArtifactEditState } from '../types'

function HtmlWeb({
  taskId,
  taskArtifact,
  onStateChange,
  editable = false,
}: {
  taskId: string
  taskArtifact: TaskArtifact
  onStateChange?: (state: ArtifactEditState) => void
  editable?: boolean
  }) {
  const containerRef = useRef<HTMLDivElement>(null)
  const content = useLoadContent(taskArtifact);

  if (!editable) {
    return <Html className='h-full' content={content} />
  }
  return (
    <PPTEditProvider>
      <div className='relative h-full'>
        <PPTEditToolBar containerRef={containerRef} />
        <div className='h-full' ref={containerRef}>
          <HtmlWithEditMode taskId={taskId} taskArtifact={taskArtifact} content={content} isDoc={false} onStateChange={onStateChange}/>
        </div>
      </div>
    </PPTEditProvider>
  )
}

export const TaskHtmlWeb = React.memo(HtmlWeb)
