'use strict';

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

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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _reactImageGallery = require('react-image-gallery');

var _reactImageGallery2 = _interopRequireDefault(_reactImageGallery);

var _cspaceInput = require('cspace-input');

var _ImageContainer = require('../../containers/media/ImageContainer');

var _ImageContainer2 = _interopRequireDefault(_ImageContainer);

var _blobHelpers = require('../../helpers/blobHelpers');

var _MediaViewer = require('../../../styles/cspace-ui/MediaViewer.css');

var _MediaViewer2 = _interopRequireDefault(_MediaViewer);

require('!style-loader!css-loader!react-image-gallery/styles/css/image-gallery-no-icon.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; } /* global window */

/* eslint-disable import/imports-first, import/no-unresolved */


/* eslint-enable import/imports-first, import/no-unresolved */

var MiniButton = _cspaceInput.baseComponents.MiniButton;


var renderItem = function renderItem(item) {
  return _react2.default.createElement(
    'div',
    { className: 'image-gallery-image' },
    _react2.default.createElement(_ImageContainer2.default, {
      src: item.original,
      alt: item.originalAlt,
      srcSet: item.srcSet,
      sizes: item.sizes,
      title: item.originalTitle,
      'data-csid': item.blobCsid
    })
  );
};

var renderThumbInner = function renderThumbInner(item) {
  return _react2.default.createElement(
    'div',
    null,
    _react2.default.createElement(_ImageContainer2.default, {
      src: item.thumbnail,
      alt: item.thumbnailAlt,
      title: item.thumbnailTitle
    }),
    _react2.default.createElement(
      'div',
      { className: 'image-gallery-thumbnail-label' },
      item.thumbnailLabel
    )
  );
};

var renderLeftNav = function renderLeftNav(onClick, disabled) {
  return _react2.default.createElement(
    MiniButton,
    { name: 'mediaViewerPrev', disabled: disabled, onClick: onClick },
    '<'
  );
};

var renderRightNav = function renderRightNav(onClick, disabled) {
  return _react2.default.createElement(
    MiniButton,
    { name: 'mediaViewerNext', disabled: disabled, onClick: onClick },
    '>'
  );
};

var propTypes = {
  carouselDerivative: _propTypes2.default.string,
  thumbnailDerivative: _propTypes2.default.string,
  popupDerivative: _propTypes2.default.string,
  config: _propTypes2.default.object.isRequired,
  isSearchPending: _propTypes2.default.bool,
  listType: _propTypes2.default.string,
  ownBlobCsid: _propTypes2.default.string,
  searchResult: _propTypes2.default.instanceOf(_immutable2.default.Map)
};

var defaultProps = {
  carouselDerivative: _blobHelpers.DERIVATIVE_MEDIUM,
  thumbnailDerivative: _blobHelpers.DERIVATIVE_THUMBNAIL,
  popupDerivative: _blobHelpers.DERIVATIVE_ORIGINAL,
  listType: 'common'
};

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

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

    var _this = _possibleConstructorReturn(this, (MediaViewer.__proto__ || Object.getPrototypeOf(MediaViewer)).call(this));

    _this.handleImageGalleryClick = _this.handleImageGalleryClick.bind(_this);
    return _this;
  }

  _createClass(MediaViewer, [{
    key: 'handleImageGalleryClick',
    value: function handleImageGalleryClick(event) {
      var target = event.target;


      if (target.nodeName === 'IMG') {
        var _props = this.props,
            config = _props.config,
            popupDerivative = _props.popupDerivative;


        var popupImagePath = (0, _blobHelpers.getDerivativePath)(target.dataset.csid, popupDerivative);

        window.open((0, _blobHelpers.getImageViewerPath)(config, popupImagePath), _blobHelpers.VIEWER_WINDOW_NAME);
      }
    }
  }, {
    key: 'createGalleryImage',
    value: function createGalleryImage(blobCsid) {
      var _props2 = this.props,
          carouselDerivative = _props2.carouselDerivative,
          thumbnailDerivative = _props2.thumbnailDerivative;


      return {
        blobCsid: blobCsid,
        original: (0, _blobHelpers.getDerivativePath)(blobCsid, carouselDerivative),
        thumbnail: (0, _blobHelpers.getDerivativePath)(blobCsid, thumbnailDerivative)
      };
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props3 = this.props,
          config = _props3.config,
          isSearchPending = _props3.isSearchPending,
          listType = _props3.listType,
          ownBlobCsid = _props3.ownBlobCsid,
          searchResult = _props3.searchResult;


      var images = [];

      if (ownBlobCsid) {
        images.push(this.createGalleryImage(ownBlobCsid));
      }

      if (searchResult) {
        var listTypeConfig = config.listTypes[listType];

        var listNodeName = listTypeConfig.listNodeName,
            itemNodeName = listTypeConfig.itemNodeName;


        var list = searchResult.get(listNodeName);
        var totalItems = parseInt(list.get('totalItems'), 10);

        if (!isNaN(totalItems)) {
          var items = list.get(itemNodeName);

          if (items) {
            if (!_immutable2.default.List.isList(items)) {
              items = _immutable2.default.List.of(items);
            }

            items.forEach(function (item) {
              var blobCsid = item.get('blobCsid');

              if (blobCsid) {
                images.push(_this2.createGalleryImage(blobCsid));
              }
            });
          }
        }
      }

      if (images.length > 0) {
        return _react2.default.createElement(
          'div',
          { className: _MediaViewer2.default.normal },
          _react2.default.createElement(_reactImageGallery2.default, {
            items: images,
            lazyLoad: true,
            renderLeftNav: renderLeftNav,
            renderRightNav: renderRightNav,
            showThumbnails: images.length > 1,
            showFullscreenButton: false,
            showPlayButton: false,
            onClick: this.handleImageGalleryClick,
            renderItem: renderItem,
            renderThumbInner: renderThumbInner
          })
        );
      }

      if (isSearchPending) {
        return null;
      }

      return _react2.default.createElement('div', { className: _MediaViewer2.default.empty });
    }
  }]);

  return MediaViewer;
}(_react.Component);

exports.default = MediaViewer;


MediaViewer.propTypes = propTypes;
MediaViewer.defaultProps = defaultProps;