/**
 * Original file: https://github.com/facebook/docusaurus/blob/main/packages/docusaurus-theme-classic/src/theme/BlogTagsPostsPage/index.tsx
 * Copyright (c) Facebook, Inc. and its affiliates under the MIT license.
 *
 * Modified by David Garcia for Finboot.
 * 
 * Modifications:
 * - Add external link and no index support.
 */

import clsx from 'clsx';
import React from 'react';

import type {Props} from '@theme/BlogPostItem/Header/Title';

import Link from '@docusaurus/Link';
import { PageMetadata } from '@docusaurus/theme-common';
// @ts-ignore the api exists on the package but is not exposed
import { useBlogPost } from '@docusaurus/theme-common/internal';

import styles from './styles.module.css';

export default function BlogPostItemHeaderTitle({
  className,
}: Props): JSX.Element {
  const {metadata, isBlogPostPage} = useBlogPost();
  const {permalink, title} = metadata;
  const TitleHeading = isBlogPostPage ? 'h1' : 'h2';

  let link = (<Link itemProp="url" to={permalink}>{title}</Link>);
  if (metadata.frontMatter.externalUrl) {
    // @ts-ignore externalUrl is a custom property
    link = (<><Link itemProp="url" href={metadata.frontMatter.externalUrl}>{title}</Link></>);
  }
  
  let noindex;
  if(metadata.frontMatter.externalUrl && isBlogPostPage) {
    noindex = (<PageMetadata>
    <meta name="robot" content="noindex"/>
  </PageMetadata>);
  }

  return (
    <TitleHeading className={clsx(styles.title, className)} itemProp="headline">
      {isBlogPostPage ? (
        title
      ) : (
        link
      )}
      {noindex}
    </TitleHeading>    
  );
}
