function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Icon } from '../../../index';
import './style.css';

var TeaserOrg = function (_Component) {
  _inherits(TeaserOrg, _Component);

  function TeaserOrg(props) {
    _classCallCheck(this, TeaserOrg);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    var allOrgs = props.orgs.split(',');
    _this.state = {
      shouldTruncate: allOrgs.length > 3,
      truncateOrgs: allOrgs.length > 3,
      showAll: false
    };
    _this.handleClick = _this.handleClick.bind(_this);
    _this.handleKeyPress = _this.handleKeyPress.bind(_this);
    if (process.env.NODE_ENV === 'development') {
      /* eslint-disable-next-line no-console */
      console.warn('This component is deprecated and will be archived in v10. Use the GenTeaser Organism instead.');
    }
    return _this;
  }

  TeaserOrg.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var allOrgs = nextProps.orgs.split(',');
    this.setState({ shouldTruncate: allOrgs.length > 3, truncateOrgs: allOrgs.length > 3, showAll: false });
  };

  TeaserOrg.prototype.handleClick = function handleClick() {
    this.setState(function (prevState) {
      return { showAll: !prevState.showAll, truncateOrgs: !prevState.truncateOrgs };
    });
  };

  TeaserOrg.prototype.handleKeyPress = function handleKeyPress(e) {
    if (e.key === 'Enter') {
      this.setState(function (prevState) {
        return { showAll: !prevState.showAll, truncateOrgs: !prevState.truncateOrgs };
      });
    }
  };

  TeaserOrg.prototype.render = function render() {
    var _this2 = this;

    var orgs = this.props.orgs;

    var teaserOrgs = orgs.split(',');
    if (!this.state.shouldTruncate) {
      return React.createElement(
        'span',
        { className: 'ma__general-teaser__org' },
        teaserOrgs.join(', ')
      );
    }
    var shownOrgs = teaserOrgs.slice(0, 3);
    var hiddenOrgs = teaserOrgs.slice(3);
    var toggleText = this.state.truncateOrgs ? ' & ' + hiddenOrgs.length + ' more' : ' Show fewer';
    var toggleProps = {
      role: 'button',
      tabIndex: 0,
      style: { color: '#14558f', cursor: 'pointer' },
      className: 'ma__general-teaser__org__show-more',
      onClick: function onClick(e) {
        return _this2.handleClick(e);
      },
      onKeyPress: function onKeyPress(e) {
        return _this2.handleKeyPress(e);
      }
    };
    if (!this.state.truncateOrgs) {
      toggleProps.className += ' show-fewer';
    }
    var toggle = React.createElement(
      'span',
      toggleProps,
      toggleText,
      ' ',
      React.createElement(Icon, { name: 'chevron', svgWidth: '16', svgHeight: '16' })
    );
    var displayedOrgs = this.state.showAll ? teaserOrgs.join(', ') : shownOrgs.join(', ');
    return React.createElement(
      'span',
      { className: 'ma__general-teaser__org' },
      displayedOrgs,
      toggle
    );
  };

  return TeaserOrg;
}(Component);

TeaserOrg.propTypes = process.env.NODE_ENV !== "production" ? {
  orgs: PropTypes.string
} : {};

var GeneralTeaser = function GeneralTeaser(generalTeaser) {
  var imageClass = generalTeaser.image ? 'ma__general-teaser ma__general-teaser--image' : 'ma__general-teaser';
  var Element = 'h' + (generalTeaser.level || 2);
  var secondaryInfoClass = !generalTeaser.primaryInfo && generalTeaser.secondaryInfo ? 'ma__general-teaser__secondary' : 'ma__general-teaser__secondary--border';
  return React.createElement(
    'section',
    { className: imageClass },
    generalTeaser.image && generalTeaser.image.src && React.createElement(
      'a',
      {
        className: 'ma__general-teaser__image',
        href: generalTeaser.title.href,
        title: generalTeaser.title.text
      },
      React.createElement('span', {
        'aria-label': generalTeaser.image.alt,
        role: 'img',
        style: { backgroundImage: 'url(' + generalTeaser.image.src + ')' }
      })
    ),
    React.createElement(
      'div',
      { className: 'ma__general-teaser__details' },
      generalTeaser.eyebrow && React.createElement(
        'div',
        { className: 'ma__general-teaser__eyebrow' },
        React.createElement(
          'span',
          null,
          generalTeaser.eyebrow
        )
      ),
      React.createElement(
        Element,
        { className: 'ma__general-teaser__title' },
        generalTeaser.title
      ),
      (generalTeaser.date || generalTeaser.org) && React.createElement(
        'div',
        { className: 'ma__general-teaser__details' },
        generalTeaser.date && React.createElement(
          'span',
          { className: 'ma__general-teaser__date' },
          generalTeaser.date
        ),
        generalTeaser.org && React.createElement(TeaserOrg, { orgs: generalTeaser.org })
      ),
      generalTeaser.description && React.createElement(
        'div',
        { className: 'ma__general-teaser__description' },
        generalTeaser.description
      ),
      generalTeaser.subLinks && React.createElement(
        'div',
        { className: 'ma__general-teaser__sublink' },
        generalTeaser.subLinks.map(function (sublink) {
          return sublink;
        })
      ),
      (generalTeaser.secondaryInfo || generalTeaser.primaryInfo) && React.createElement(
        'div',
        { className: 'ma__general-teaser__moreinfo' },
        generalTeaser.primaryInfo && React.createElement(
          'div',
          { className: 'ma__general-teaser__pimaryinfo' },
          generalTeaser.primaryInfo
        ),
        generalTeaser.secondaryInfo && React.createElement(
          'div',
          { className: secondaryInfoClass },
          generalTeaser.secondaryInfo.map(function (info, infoKey) {
            return (
              // eslint-disable-next-line react/no-array-index-key
              React.createElement(
                'div',
                { key: infoKey, className: 'ma__general-teaser__secondaryinfo' },
                info
              )
            );
          })
        )
      )
    )
  );
};

GeneralTeaser.propTypes = process.env.NODE_ENV !== "production" ? {
  image: PropTypes.shape({
    src: PropTypes.string,
    alt: PropTypes.string
  }),
  eyebrow: PropTypes.string,
  // eslint-disable-next-line consistent-return
  title: function title(props, propName, componentName) {
    var component = props[propName];
    var isValid = function isValid(comp) {
      if (!comp) {
        return false;
      }
      if (typeof comp.type === 'string') {
        return comp.type === 'DecorativeLink';
      }
      return comp.type.name && comp.type.name === 'DecorativeLink';
    };
    if (!isValid(component)) {
      return new Error('Invalid prop ' + propName + ' supplied to ' + componentName + '. Got: ' + component.type.name + '. Validation failed.');
    }
  },
  level: PropTypes.number,
  date: PropTypes.string,
  org: PropTypes.string,
  // eslint-disable-next-line consistent-return
  description: function description(props, propName, componentName) {
    var component = props[propName];
    var isValid = function isValid(comp) {
      if (!comp) {
        return false;
      }
      if (typeof comp.type === 'string') {
        return comp.type === 'Paragraph';
      }
      return comp.type.name && comp.type.name === 'Paragraph';
    };
    if (component && !isValid(component)) {
      return new Error('Invalid prop ' + propName + ' supplied to ' + componentName + '. Got: ' + component.type.name + '. Validation failed.');
    }
  },
  // eslint-disable-next-line consistent-return
  subLinks: function subLinks(props, propName, componentName) {
    var component = props[propName];
    var isValid = function isValid(comp) {
      if (!comp) {
        return false;
      }
      if (!Array.isArray(comp)) {
        return false;
      }
      var valid = false;
      comp.every(function (child) {
        if (!child) {
          return valid;
        }
        if (typeof child.type === 'string') {
          valid = child.type === 'DecorativeLink';
        }
        valid = child.type.name && child.type.name === 'DecorativeLink';
        return valid;
      });
      return valid;
    };
    if (component && !isValid(component)) {
      return new Error('Invalid prop ' + propName + ' supplied to ' + componentName + '. Got: ' + component.type.name + '. Validation failed.');
    }
  },
  // eslint-disable-next-line consistent-return
  primaryInfo: function primaryInfo(props, propName, componentName) {
    var component = props[propName];
    var isValid = function isValid(comp) {
      if (!comp) {
        return false;
      }
      if (typeof comp.type === 'string') {
        return comp.type === 'ContactGroup';
      }
      return comp.type.name && comp.type.name === 'ContactGroup';
    };
    if (component && !isValid(component)) {
      return new Error('Invalid prop ' + propName + ' supplied to ' + componentName + '. Got: ' + component.type.name + '. Validation failed.');
    }
  },
  // eslint-disable-next-line consistent-return
  secondaryInfo: function secondaryInfo(props, propName, componentName) {
    var component = props[propName];
    var isValid = function isValid(comp) {
      if (!comp) {
        return false;
      }
      if (!Array.isArray(comp)) {
        return false;
      }
      var valid = false;
      comp.every(function (child) {
        if (!child) {
          return valid;
        }
        if (typeof child.type === 'string') {
          valid = child.type === 'IconLink';
        }
        valid = child.type.name && child.type.name === 'IconLink';
        return valid;
      });
      return valid;
    };
    if (component && !isValid(component)) {
      return new Error('Invalid prop ' + propName + ' supplied to ' + componentName + '. Got: ' + component.type.name + '. Validation failed.');
    }
  }
} : {};

GeneralTeaser.defaultProps = {
  subLinks: null,
  secondaryInfo: null,
  primaryInfo: null,
  description: null
};

export default GeneralTeaser;