'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _getClassName2 = require('babel-plugin-react-css-modules/dist/browser/getClassName');

var _getClassName3 = _interopRequireDefault(_getClassName2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactPlayer = require('react-player');

var _reactPlayer2 = _interopRequireDefault(_reactPlayer);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _tools = require('../_helpers/tools');

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

var _styleModuleImportMap = {
  './media.scss': {
    'media-wrapper': 'flive_media_media-wrapper',
    'media-wrapper--bordered': 'flive_media_media-wrapper--bordered',
    'media-container': 'flive_media_media-container',
    'media': 'flive_media_media',
    'description': 'flive_media_description',
    'media-img-wrapper': 'flive_media_media-img-wrapper',
    'media-img-wrapper--cover': 'flive_media_media-img-wrapper--cover',
    'media-img': 'flive_media_media-img',
    'media-img--cover': 'flive_media_media-img--cover',
    'media-wrapper--flex-container': 'flive_media_media-wrapper--flex-container',
    'media-img-wrapper--height-auto': 'flive_media_media-img-wrapper--height-auto',
    'media-img--height-auto': 'flive_media_media-img--height-auto'
  }
};

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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 Media = function (_PureComponent) {
  _inherits(Media, _PureComponent);

  function Media() {
    _classCallCheck(this, Media);

    return _possibleConstructorReturn(this, (Media.__proto__ || Object.getPrototypeOf(Media)).apply(this, arguments));
  }

  _createClass(Media, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          src = _props.src,
          ratio = _props.ratio,
          width = _props.width,
          type = _props.type,
          alt = _props.alt,
          name = _props.name,
          className = _props.className,
          cropped = _props.cropped,
          cover = _props.cover,
          flexContainer = _props.flexContainer,
          border = _props.border,
          children = _props.children,
          rest = _objectWithoutProperties(_props, ['src', 'ratio', 'width', 'type', 'alt', 'name', 'className', 'cropped', 'cover', 'flexContainer', 'border', 'children']);

      var ratioConverted = ratio < 0 ? 0 : 1 / ratio * 100;
      return _react2.default.createElement(
        'div',
        _extends({
          style: { width: width }
        }, rest, {
          className: (className ? className + ' ' : '') + (0, _getClassName3.default)((0, _classnames2.default)('media-wrapper', {
            'media-wrapper--bordered': border || children,
            'media-wrapper--flex-container': flexContainer
          }), _styleModuleImportMap)
        }),
        _react2.default.createElement(
          'div',
          {
            className: 'flive_media_media-container',
            style: { paddingTop: ratioConverted + '%' },
            itemType: 'http://schema.org/' + (0, _tools.capitalizeFirstLetter)(type) + 'Object',
            itemScope: true
          },
          type === 'image' && _react2.default.createElement(
            'div',
            {
              style: { backgroundImage: 'url("' + src + '")' },
              className: (0, _getClassName3.default)((0, _classnames2.default)('media', 'media-img-wrapper', {
                'media-img-wrapper--cover': cropped || cover,
                'media-img-wrapper--height-auto': ratio < 0
              }), _styleModuleImportMap)
            },
            _react2.default.createElement('img', {
              src: src,
              alt: alt,
              itemProp: 'contentUrl',
              className: (0, _getClassName3.default)((0, _classnames2.default)('media-img', {
                'media-img--cover': cropped || cover,
                'media-img--height-auto': ratio < 0
              }), _styleModuleImportMap)
            })
          ),
          type === 'video' && _react2.default.createElement(_reactPlayer2.default, {
            className: 'flive_media_media',
            url: src,
            youtubeConfig: { playerVars: { controls: 1 } },
            controls: true,
            itemProp: 'contentUrl',
            width: width,
            height: 'auto'
          }),
          !!name && _react2.default.createElement('span', { itemProp: 'caption', content: name })
        ),
        children && _react2.default.createElement(
          'div',
          { className: 'flive_media_description' },
          children
        )
      );
    }
  }]);

  return Media;
}(_react.PureComponent);

Media.propTypes = {
  /** @ignore */
  className: _propTypes2.default.string,
  /** @ignore */
  children: _propTypes2.default.node,
  src: _propTypes2.default.string.isRequired,
  ratio: _propTypes2.default.number,
  width: _propTypes2.default.string,
  type: _propTypes2.default.oneOf(['video', 'image']).isRequired,
  alt: _propTypes2.default.string,
  name: _propTypes2.default.string,
  cropped: _propTypes2.default.bool,
  cover: _propTypes2.default.bool,
  flexContainer: _propTypes2.default.bool,
  border: _propTypes2.default.bool
};
Media.defaultProps = {
  className: undefined,
  children: null,
  ratio: 1,
  width: null,
  alt: '',
  name: '',
  cropped: false,
  cover: false,
  flexContainer: false,
  border: false
};
exports.default = Media;
module.exports = exports['default'];