/**
 * PostCard 主组件
 * 帖子卡片组件 - 支持列表模式和网格模式
 *
 * @example
 * // 新 API 使用方式
 * <PostCard
 *   post={post}
 *   onAction={(action) => handleAction(post, action)}
 *   variant="list"
 *   features="full"
 * />
 */

import React from 'react';
import { PostCardProps, LegacyPostCardProps } from './types';
import PostCardList from './PostCardList';
import PostCardGrid from './PostCardGrid';
import { usePostCardFeatures } from './hooks/usePostCardFeatures';
import { createLegacyAdapter, isLegacyProps } from './legacyAdapter';

/**
 * PostCard 主组件
 * 支持新旧两种 API
 */
const PostCard: React.FC<PostCardProps | LegacyPostCardProps> = (props) => {
  // 检测是否使用旧 API
  const isLegacy = isLegacyProps(props as LegacyPostCardProps);

  // 兼容层：转换旧 Props 到新 Props
  const normalizedProps: PostCardProps = isLegacy
    ? createLegacyAdapter(props as LegacyPostCardProps)
    : (props as PostCardProps);

  const {
    post,
    onAction,
    variant = 'list',
    features: featuresConfig,
    isPostAuthor = false,
    isAuthor = false,
    index,
    style,
  } = normalizedProps;

  // 解析 Features 配置
  const resolvedFeatures = usePostCardFeatures(featuresConfig);

  // 根据 variant 选择渲染模式
  if (variant === 'grid') {
    return (
      <PostCardGrid
        post={post}
        onAction={onAction}
        features={resolvedFeatures}
        style={style}
      />
    );
  }

  return (
    <PostCardList
      post={post}
      onAction={onAction}
      features={resolvedFeatures}
      isPostAuthor={isPostAuthor}
      isAuthor={isAuthor}
      index={index}
      style={style}
    />
  );
};

export default PostCard;