'use strict';

exports.__esModule = true;
exports.default = TutorialCard;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _Tutorials = require('../../../views/Tutorials');

var _components = require('../../components');

var _tutorials = require('../TutorialModal/proptypes/tutorials');

var _styleModule = require('./style.module.scss');

var _styleModule2 = _interopRequireDefault(_styleModule);

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

// Components
// 3rd-party libs
var CardButtonBar = function CardButtonBar(_ref) {
  var isAdmin = _ref.isAdmin,
      tutorial = _ref.tutorial,
      makeHero = _ref.makeHero,
      shiftOrder = _ref.shiftOrder,
      numTutorials = _ref.numTutorials,
      openTutorial = _ref.openTutorial,
      updateTutorial = _ref.updateTutorial,
      deleteTutorial = _ref.deleteTutorial;
  var _id = tutorial._id;

  if (!isAdmin) return false;

  return _react2.default.createElement(
    _react2.default.Fragment,
    null,
    _react2.default.createElement(
      'button',
      {
        className: _styleModule2.default['TutorialCard-Toolbar-Arrow'],
        disabled: tutorial.order === 0,
        onClick: function onClick() {
          return shiftOrder({ _id: _id, direction: -1 });
        }
      },
      _react2.default.createElement('i', { className: 'fa fa-arrow-left' })
    ),
    _react2.default.createElement(
      'button',
      {
        className: _styleModule2.default['TutorialCard-Toolbar-Arrow'],
        disabled: tutorial.order === numTutorials - 1,
        onClick: function onClick() {
          return shiftOrder({ _id: _id, direction: 1 });
        }
      },
      _react2.default.createElement('i', { className: 'fa fa-arrow-right' })
    ),
    _react2.default.createElement(
      'button',
      { className: _styleModule2.default['TutorialCard-Toolbar-Button'], onClick: function onClick() {
          return openTutorial('Edit', _id);
        } },
      'Edit'
    ),
    _react2.default.createElement(
      'button',
      {
        className: _styleModule2.default['TutorialCard-Toolbar-Button'],
        onClick: function onClick() {
          return updateTutorial({ _id: _id, published: !tutorial.published });
        }
      },
      tutorial.published ? 'Unpublish' : 'Publish'
    ),
    !tutorial.hero && _react2.default.createElement(
      'button',
      {
        className: _styleModule2.default['TutorialCard-Toolbar-Button'],
        disabled: tutorial.hero,
        onClick: function onClick() {
          return makeHero({ _id: _id });
        }
      },
      'Feature'
    ),
    _react2.default.createElement(
      'button',
      {
        className: [_styleModule2.default['TutorialCard-Toolbar-Button'], _styleModule2.default['TutorialCard-Toolbar-Button_red']].join(' '),
        onClick: function onClick() {
          return deleteTutorial({ _id: _id });
        }
      },
      'Delete'
    )
  );
};

// Style


CardButtonBar.propTypes = process.env.NODE_ENV !== "production" ? {
  deleteTutorial: _propTypes2.default.func,
  isAdmin: _propTypes2.default.bool,
  makeHero: _propTypes2.default.func,
  numTutorials: _propTypes2.default.number,
  openTutorial: _propTypes2.default.func,
  shiftOrder: _propTypes2.default.func,
  tutorial: _tutorials.TutorialProp,
  updateTutorial: _propTypes2.default.func
} : {};

var renderStatus = function renderStatus(isAdmin, tutorial) {
  var hero = tutorial.hero,
      published = tutorial.published;

  if (!isAdmin) return false;
  if (hero || !published) {
    return _react2.default.createElement(
      'div',
      { className: [_styleModule2.default['TutorialCard-Status'], hero ? _styleModule2.default['TutorialCard-Status_featured'] : ''].join(' ') },
      _react2.default.createElement('i', { className: [_styleModule2.default['TutorialCard-Status-Icon'], hero ? 'fa fa-star' : 'icon icon-eye-crossed'].join(' ') }),
      hero ? 'Featured' : 'Not Published'
    );
  }
  return false;
};

function TutorialCard(_ref2) {
  var tutorialReview = _ref2.tutorialReview,
      className = _ref2.className,
      numTutorials = _ref2.numTutorials,
      tutorial = _ref2.tutorial,
      makeHero = _ref2.makeHero,
      openTutorial = _ref2.openTutorial,
      updateTutorial = _ref2.updateTutorial,
      shiftOrder = _ref2.shiftOrder,
      deleteTutorial = _ref2.deleteTutorial;

  var TutorialCardClasses = [className, _styleModule2.default.TutorialCard, !tutorial.published && _styleModule2.default.TutorialCard_unpublished].filter(function (e) {
    return e;
  }).join(' ');
  return _react2.default.createElement(
    'section',
    { className: TutorialCardClasses },
    _react2.default.createElement(
      'div',
      { className: _styleModule2.default['TutorialCard-Top'] },
      _react2.default.createElement(
        _Tutorials.PermissionContext.Consumer,
        null,
        function (isAdmin) {
          return renderStatus(isAdmin && !tutorialReview, tutorial);
        }
      ),
      tutorial.emoji && _react2.default.createElement(
        _components.Avatar,
        { className: _styleModule2.default['TutorialCard-Avatar'], hexColor: tutorial.backgroundColor },
        tutorial.emoji
      ),
      _react2.default.createElement(
        'div',
        { className: _styleModule2.default['TutorialCard-Title'] },
        tutorial.title
      ),
      _react2.default.createElement(
        'div',
        { className: _styleModule2.default['TutorialCard-Description'] },
        tutorial.description,
        ' '
      ),
      _react2.default.createElement(
        'div',
        { className: _styleModule2.default['TutorialCard-Steps'] },
        tutorial.stepCount,
        ' Steps'
      ),
      _react2.default.createElement(
        _components.Button,
        {
          bem: { blue: true },
          className: _styleModule2.default['TutorialCard-Button'],
          onClick: function onClick() {
            return openTutorial('View', tutorial._id);
          }
        },
        'Start Tutorial'
      )
    ),
    _react2.default.createElement(
      _Tutorials.PermissionContext.Consumer,
      null,
      function (isAdmin) {
        return _react2.default.createElement(
          'footer',
          { className: _styleModule2.default['TutorialCard-Toolbar'] },
          _react2.default.createElement(
            'div',
            { className: _styleModule2.default['TutorialCard-Toolbar-Wrapper'] },
            _react2.default.createElement(CardButtonBar, {
              deleteTutorial: deleteTutorial,
              isAdmin: isAdmin && !tutorialReview,
              makeHero: makeHero,
              numTutorials: numTutorials,
              openTutorial: openTutorial,
              shiftOrder: shiftOrder,
              tutorial: tutorial,
              updateTutorial: updateTutorial
            })
          )
        );
      }
    )
  );
}

TutorialCard.propTypes = process.env.NODE_ENV !== "production" ? {
  deleteTutorial: _propTypes2.default.func,
  makeHero: _propTypes2.default.func,
  numTutorials: _propTypes2.default.number,
  openTutorial: _propTypes2.default.func,
  shiftOrder: _propTypes2.default.func,
  tutorial: _tutorials.TutorialProp,
  tutorialReview: _propTypes2.default.bool,
  updateTutorial: _propTypes2.default.func
} : {};

TutorialCard.defaultProps = {
  tutorialReview: false
};
module.exports = exports['default'];