import { type LinkPreviewResponse } from 'src/types/previewLink';
import { type PostRequestContent } from 'src/types/posts';

type PostPendingContentBase = {
  body: string;
  bodyHtml?: string;
  bodyAttributes?: PostRequestContent['bodyAttributes'];
  linkPreviews: Record<string, LinkPreviewResponse>;
  publicationDatetime: string;
  updatedAt: string;
};

/**
 * Merges pending approval request content onto a post entity for display.
 */
export const getPostWithPendingContent = <T extends PostPendingContentBase>(
  post: T,
  postContent: PostRequestContent | null,
  requestUpdatedAt: string,
): T => {
  return {
    ...post,
    body: postContent?.body ?? post.body,
    bodyHtml: postContent?.bodyHtml ?? post.bodyHtml,
    bodyAttributes: postContent?.bodyAttributes ?? post.bodyAttributes,
    linkPreviews: postContent?.linkPreviews ?? post.linkPreviews,
    publicationDatetime:
      postContent?.contentPublicationDatetime ?? post.publicationDatetime,
    updatedAt: requestUpdatedAt,
  };
};
