/* eslint-disable import/prefer-default-export */
import {
  breadcrumbsSchema,
  webPageSchema,
  organizationSchema,
  moduleSchemas,
  templateSchemas,
} from 'gatsby-core-theme/src/helpers/schema';

const knowsAbout = [
  {
    '@type': 'Thing',
    name: 'Norway',
    sameAs: [
      'https://no.wikipedia.org/wiki/Norge',
      'https://www.google.no/search?q=Norge&pws=0&nord=1&hl=no&gl=NO',
    ],
  },
  {
    '@type': 'Thing',
    name: 'LottStift',
    sameAs: [
      'https://no.wikipedia.org/wiki/Lotteri-_og_stiftelsestilsynet',
      'https://www.google.no/search?q=lottstift&pws=0&nord=1&hl=no&gl=NO',
    ],
  },
  {
    '@type': 'Thing',
    name: 'Kasino',
    sameAs: [
      'https://no.wikipedia.org/wiki/Kasino',
      'https://www.google.no/search?q=Kasino&pws=0&nord=1&hl=no&gl=NO',
    ],
  },
  {
    '@type': 'Thing',
    name: 'Norsk Tipping Kommisjonær',
    sameAs: [
      'https://no.wikipedia.org/wiki/Norsk_Tipping',
      'https://www.google.no/search?q=norsk+tipping&pws=0&nord=1&hl=no&gl=NO',
    ],
  },
];

export function schemaGenerator(page = {}, pageImage) {
  const isNSA = process.env.GATSBY_SITE_NAME === 'norskespilleautomater.com';
  const isHomepage = page.path === '/';
  if (isNSA) {
    page.knowsAbout = knowsAbout;
  }

  // Temporary Change for removing webPageSchema
  const hasWebPageSchema =
    (page.relation_type !== 'operator' && page.relation_type !== 'game') || isNSA;
  const hasOrganisationSchema = page.path === '/' || page.path === 'about-us';
  const jsonSchema = [
    // Page Schema
    page.seo_json_schema,
    // Breadcrumbs Schema
    !isHomepage ? breadcrumbsSchema(page) : null,
    hasWebPageSchema ? webPageSchema(page, pageImage) : null,
    hasOrganisationSchema ? organizationSchema(page, pageImage) : null,
    templateSchemas(page, pageImage),
    // Modules Schemas
    ...(page.sections?.main?.modules
      ? moduleSchemas(page.sections.main.modules, page.path, page, pageImage)
      : [null]),
  ];

  return jsonSchema;
}
