import { type FC } from 'react';

import { type ShareMetadata } from 'src/types/posts';
import { withShareMetadata } from 'src/utils/sharedPost';

import SharedPostEmbedded from './SharedPostEmbedded';
import SharedPostPlaceholder from './SharedPostPlaceholder';
import SharedPostSkeleton from './SharedPostSkeleton';
import { useSharedPost } from './useSharedPost';

type SharedPostProps = {
  metadata: ShareMetadata | null | undefined;
};

export const SharedPost: FC<SharedPostProps> = ({ metadata }) => {
  const { post, isLoading, notFound, isError } = useSharedPost(metadata);

  if (!metadata || !withShareMetadata.isShare(metadata)) return null;

  if (notFound || isError) return <SharedPostPlaceholder />;
  if (isLoading || !post) return <SharedPostSkeleton />;

  return <SharedPostEmbedded post={post} />;
};

export default SharedPost;
