import React from 'react';

interface SeoJsonLdProps {
  data: object | object[];
}

export default function SeoJsonLd({ data }: SeoJsonLdProps) {
  const jsonLdArray = Array.isArray(data) ? data : [data];

  return (
    <>
      {jsonLdArray.map((item, index) => {
        // Only add @context if it doesn't already exist in the item
        const schema = (item as any)['@context']
          ? item
          : { '@context': 'https://schema.org', ...item };

        return (
          <script
            key={index}
            type="application/ld+json"
            dangerouslySetInnerHTML={{
              __html: JSON.stringify(schema, null, 0),
            }}
          />
        );
      })}
    </>
  );
}
