'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _index = require('../../../index');

require('./style.css');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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; }

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 _react2.default.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 = _react2.default.createElement(
      'span',
      toggleProps,
      toggleText,
      ' ',
      _react2.default.createElement(_index.Icon, { name: 'chevron', svgWidth: '16', svgHeight: '16' })
    );
    var displayedOrgs = this.state.showAll ? teaserOrgs.join(', ') : shownOrgs.join(', ');
    return _react2.default.createElement(
      'span',
      { className: 'ma__general-teaser__org' },
      displayedOrgs,
      toggle
    );
  };

  return TeaserOrg;
}(_react.Component);

TeaserOrg.propTypes = process.env.NODE_ENV !== "production" ? {
  orgs: _propTypes2.default.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 _react2.default.createElement(
    'section',
    { className: imageClass },
    generalTeaser.image && generalTeaser.image.src && _react2.default.createElement(
      'a',
      {
        className: 'ma__general-teaser__image',
        href: generalTeaser.title.href,
        title: generalTeaser.title.text
      },
      _react2.default.createElement('span', {
        'aria-label': generalTeaser.image.alt,
        role: 'img',
        style: { backgroundImage: 'url(' + generalTeaser.image.src + ')' }
      })
    ),
    _react2.default.createElement(
      'div',
      { className: 'ma__general-teaser__details' },
      generalTeaser.eyebrow && _react2.default.createElement(
        'div',
        { className: 'ma__general-teaser__eyebrow' },
        _react2.default.createElement(
          'span',
          null,
          generalTeaser.eyebrow
        )
      ),
      _react2.default.createElement(
        Element,
        { className: 'ma__general-teaser__title' },
        generalTeaser.title
      ),
      (generalTeaser.date || generalTeaser.org) && _react2.default.createElement(
        'div',
        { className: 'ma__general-teaser__details' },
        generalTeaser.date && _react2.default.createElement(
          'span',
          { className: 'ma__general-teaser__date' },
          generalTeaser.date
        ),
        generalTeaser.org && _react2.default.createElement(TeaserOrg, { orgs: generalTeaser.org })
      ),
      generalTeaser.description && _react2.default.createElement(
        'div',
        { className: 'ma__general-teaser__description' },
        generalTeaser.description
      ),
      generalTeaser.subLinks && _react2.default.createElement(
        'div',
        { className: 'ma__general-teaser__sublink' },
        generalTeaser.subLinks.map(function (sublink) {
          return sublink;
        })
      ),
      (generalTeaser.secondaryInfo || generalTeaser.primaryInfo) && _react2.default.createElement(
        'div',
        { className: 'ma__general-teaser__moreinfo' },
        generalTeaser.primaryInfo && _react2.default.createElement(
          'div',
          { className: 'ma__general-teaser__pimaryinfo' },
          generalTeaser.primaryInfo
        ),
        generalTeaser.secondaryInfo && _react2.default.createElement(
          'div',
          { className: secondaryInfoClass },
          generalTeaser.secondaryInfo.map(function (info, infoKey) {
            return (
              // eslint-disable-next-line react/no-array-index-key
              _react2.default.createElement(
                'div',
                { key: infoKey, className: 'ma__general-teaser__secondaryinfo' },
                info
              )
            );
          })
        )
      )
    )
  );
};

GeneralTeaser.propTypes = process.env.NODE_ENV !== "production" ? {
  image: _propTypes2.default.shape({
    src: _propTypes2.default.string,
    alt: _propTypes2.default.string
  }),
  eyebrow: _propTypes2.default.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: _propTypes2.default.number,
  date: _propTypes2.default.string,
  org: _propTypes2.default.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
};

exports.default = GeneralTeaser;
module.exports = exports['default'];