import type { ReactElement } from "react";
import type {
  BlockType,
  ExerciseBlockData,
  RichTextBlockData,
  TextStudyBlockData,
} from "../types";
import { RichTextBlock } from "../components/blocks/rich-text-block";
import { ExerciseBlock } from "../components/blocks/exercise-block";
import { TextStudyBlock } from "../components/blocks/text-study-block";
import { ReflectionBlock } from "../components/blocks/reflection-block";

/**
 * Block 注册表：配置驱动渲染。
 * 新增 Block 类型只需在此注册，无需修改 NodeEditPanel。
 */

export interface BlockRenderProps {
  blockId: string;
  data: RichTextBlockData | ExerciseBlockData | TextStudyBlockData;
  textbookId?: string;
  chapterId?: string;
  classes?: { id: string; name: string }[];
  onUpdate: (data: RichTextBlockData | ExerciseBlockData | TextStudyBlockData) => void;
}

export interface BlockRegistryEntry {
  /** 是否为富文本类（共享 RichTextBlock 编辑器） */
  isRichText?: boolean;
}

const RICH_TEXT_TYPES: BlockType[] = [
  "objective",
  "key_point",
  "import",
  "new_teaching",
  "consolidation",
  "summary",
  "homework",
  "blackboard",
  "rich_text",
];

/**
 * Block 注册表元数据（用于查询 isRichText 等属性）。
 * 组件渲染由 BlockRenderer 统一处理，避免在 render 中动态获取组件引用。
 */
export const BLOCK_REGISTRY: Record<BlockType, BlockRegistryEntry> = {
  objective: { isRichText: true },
  key_point: { isRichText: true },
  import: { isRichText: true },
  new_teaching: { isRichText: true },
  consolidation: { isRichText: true },
  summary: { isRichText: true },
  homework: { isRichText: true },
  blackboard: { isRichText: true },
  rich_text: { isRichText: true },
  exercise: {},
  text_study: {},
  reflection: {},
};

export function isRichTextBlock(type: BlockType): boolean {
  return RICH_TEXT_TYPES.includes(type);
}

/**
 * 静态 Block 渲染组件。
 * 根据 type 从注册表查找并渲染对应 Block，所有组件引用均为模块顶层静态声明，
 * 满足 react-hooks/static-components 规则。
 * 新增 Block 类型时，在此 switch 中添加对应 case 即可。
 */
export function BlockRenderer(props: BlockRenderProps & { type: BlockType }): ReactElement | null {
  const { type, ...rest } = props;
  switch (type) {
    case "exercise":
      return (
        <ExerciseBlock
          blockId={rest.blockId}
          data={rest.data as ExerciseBlockData}
          classes={rest.classes ?? []}
          textbookId={rest.textbookId}
          chapterId={rest.chapterId}
        />
      );
    case "text_study":
      return <TextStudyBlock blockId={rest.blockId} data={rest.data as TextStudyBlockData} />;
    case "reflection":
      return <ReflectionBlock data={rest.data as RichTextBlockData} onUpdate={rest.onUpdate} />;
    case "objective":
    case "key_point":
    case "import":
    case "new_teaching":
    case "consolidation":
    case "summary":
    case "homework":
    case "blackboard":
    case "rich_text":
      return (
        <RichTextBlock
          data={rest.data as RichTextBlockData}
          textbookId={rest.textbookId}
          chapterId={rest.chapterId}
          onUpdate={rest.onUpdate}
        />
      );
    default:
      return null;
  }
}
