import { _ as _JSON$stringify } from './stringify-2b084bee.js';
import { _ as _extends } from './extends-1559c147.js';
import { _ as _toConsumableArray } from './toConsumableArray-f7074d7c.js';
import React__default from 'react';
import urlFor from './urlFor.js';
import './_commonjsHelpers-0c4b6f40.js';
import './core.get-iterator-method-ea258bb1.js';
import './_iter-detect-98db3b07.js';
import './slicedToArray-6ebcbfea.js';
import './web.dom.iterable-ab4dea5c.js';
import './index-3f5c03b2.js';

function toPlainText() {
  var blocks = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

  return blocks
  // loop through each block
  .map(function (block) {
    // if it's not a text block with children,
    // return nothing
    if (block._type !== 'block' || !block.children) {
      return '';
    }
    // loop through the children spans, and join the
    // text strings
    return block.children.map(function (child) {
      return child.text;
    }).join('');
  })
  // join the paragraphs leaving split by two linebreaks
  .join('\n\n');
}

function generateSchema(props) {
    var _props$article = props.article,
        title = _props$article.title,
        body = _props$article.body,
        summary = _props$article.summary,
        url = _props$article.url,
        content_placement = _props$article.content_placement,
        authorDetails = _props$article.authorDetails,
        thumbnail = _props$article.thumbnail,
        published = _props$article.published,
        _updatedAt = _props$article._updatedAt,
        seoTag = _props$article.seoTag,
        _props$Website = props.Website,
        liveDomain = _props$Website.liveDomain,
        websiteTitle = _props$Website.title,
        logo = _props$Website.logo,
        client = props.client;

    var authors = authorDetails && authorDetails.length > 0 && authorDetails.reduce(function (result, author) {
        if (author.displayName) return [].concat(_toConsumableArray(result), [{
            '@type': 'Person',
            name: author.displayName
        }]);else return result;
    }, []);
    var articleSection = content_placement && content_placement.map(function (cp) {
        return cp.name;
    });
    var image = thumbnail && thumbnail.asset && client ? urlFor({ source: thumbnail, client: client }) : liveDomain && logo && 'https://' + liveDomain + logo;

    if (title && published && _updatedAt) {
        return _extends({
            '@context': 'https://schema.org',
            '@type': 'NewsArticle',
            headline: title,
            datePublished: published,
            dateModified: _updatedAt,
            inLanguage: 'en-US'
        }, image && { image: image }, liveDomain && url && url.current && {
            mainEntityOfPage: {
                '@type': 'WebPage',
                '@id': 'https://' + liveDomain + '/view/' + url.current
            }
        }, websiteTitle && liveDomain && logo && {
            publisher: {
                '@type': 'Organization',
                name: websiteTitle,
                logo: {
                    '@type': 'ImageObject',
                    url: 'https://' + liveDomain + logo
                }
            }
        }, articleSection && { articleSection: articleSection }, seoTag && seoTag.length > 0 && { keywords: seoTag.join() }, body && body.length > 0 && { articleBody: toPlainText(body) }, summary && { description: summary }, authors && authors.length > 0 && { author: authors });
    }
    return null;
}

var Schema = function Schema(props) {
    return generateSchema(props) !== null && React__default.createElement('script', { type: 'application/ld+json',
        dangerouslySetInnerHTML: { __html: _JSON$stringify(generateSchema(props)) }
    });
};

export { Schema as default };
