'use client';

/**
 * Link block — renders a `LinkPreviewCard`. If the block carries resolved
 * `data` it shows immediately; otherwise it pulls the host resolver from
 * chat context (`config.linkPreview.resolve`) and unfurls lazily.
 */

import { LinkPreviewCard } from '../../../../../common/link-preview';
import { useChatContextOptional } from '../../../context';
import type { LinkBlock } from '../../../types/block';
import type { BlockRendererProps } from './types';

export default function LinkBlockRenderer({ block, ctx }: BlockRendererProps<LinkBlock>) {
  const chat = useChatContextOptional();
  const resolver = chat?.config.linkPreview?.resolve;

  // No data and no resolver → nothing to render (card returns null too).
  if (!block.data && !resolver) return null;

  // The link payload (`url` + optional pre-resolved `data`) is validated
  // centrally at the registry dispatch point (MessageBlocks) before this
  // renderer runs, so we can trust it here.
  return (
    <LinkPreviewCard
      url={block.url}
      data={block.data}
      resolver={resolver}
      appearance={ctx.appearance}
    />
  );
}
