'use strict';

var stringify = require('./stringify-f7a37a8d.js');
var _extends = require('./extends-7c86182f.js');
var toConsumableArray = require('./toConsumableArray-d7797c2b.js');
var React = require('react');
var urlFor = require('./urlFor.js');
require('./_commonjsHelpers-06173234.js');
require('./core.get-iterator-method-5643aa10.js');
require('./_object-pie-083f2dd6.js');
require('./_iter-detect-b1df62f0.js');
require('./slicedToArray-067dd3bc.js');
require('./web.dom.iterable-d98303e0.js');
require('./index-d48b231c.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

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._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._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 React__default['default'].createElement('script', { type: 'application/ld+json', dangerouslySetInnerHTML: { __html: stringify._JSON$stringify(generateSchema(props)) } });
};

module.exports = Schema;
