"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = TutorialCard;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

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

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

var _tutorials = require("../Modal/proptypes/tutorials");

// 3rd-party libs
// Components
// Style
var classes = {};
/* istanbul ignore next */

if (typeof window !== 'undefined' || typeof _COMPILED_ === 'undefined') classes = {
  "slate": "#384248",
  "blue": "#118CFD",
  "green": "#12CA93",
  "red": "#E95F6A",
  "yellow": "#F7C543",
  "purple": "#8470BE",
  "graphite": "#4F5A66",
  "shale": "#939EAE",
  "chalk": "#c6cbd3",
  "ivory": "#F0F0F0",
  "lightGray": "#F8F8F8",
  "white": "#FFF",
  "TutorialCard": "_10-zB-TutorialCard",
  "TutorialCard_unpublished": "_2_EJ9-TutorialCard_unpublished",
  "TutorialCard-Avatar": "Bg2v6-TutorialCard-Avatar",
  "TutorialCard-Title": "_3hYLv-TutorialCard-Title",
  "TutorialCard-Description": "yk496-TutorialCard-Description",
  "TutorialCard-Steps": "_3FddU-TutorialCard-Steps",
  "TutorialCard-Button": "OWBKo-TutorialCard-Button",
  "TutorialCard-Status": "_2jtvB-TutorialCard-Status",
  "TutorialCard-Status_featured": "Z_jS2-TutorialCard-Status_featured",
  "TutorialCard-Status-Icon": "_2iC7J-TutorialCard-Status-Icon",
  "TutorialCard-Top": "M4Ift-TutorialCard-Top",
  "TutorialCard-Toolbar": "qT2lm-TutorialCard-Toolbar",
  "TutorialCard-Toolbar-Wrapper": "_3liuE-TutorialCard-Toolbar-Wrapper",
  "TutorialCard-Toolbar-Button": "_3UP7c-TutorialCard-Toolbar-Button",
  "TutorialCard-Toolbar-Arrow": "ZGq3H-TutorialCard-Toolbar-Arrow",
  "TutorialCard-Toolbar-Button_red": "_2naGF-TutorialCard-Toolbar-Button_red"
};

var CardButtonBar = function CardButtonBar(_ref) {
  var isAdmin = _ref.isAdmin,
      tutorial = _ref.tutorial,
      makeHero = _ref.makeHero,
      shiftOrder = _ref.shiftOrder,
      numTutorials = _ref.numTutorials,
      openTutorial = _ref.openTutorial,
      publish = _ref.publish,
      deleteTutorial = _ref.deleteTutorial;
  var _id = tutorial._id,
      published = tutorial.published,
      title = tutorial.title;
  if (!isAdmin) return false;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("button", {
    className: classes['TutorialCard-Toolbar-Arrow'],
    disabled: tutorial.order === 0,
    onClick: function onClick() {
      return shiftOrder({
        _id: _id,
        direction: -1
      });
    }
  }, /*#__PURE__*/_react["default"].createElement("i", {
    className: "fa fa-arrow-left"
  })), /*#__PURE__*/_react["default"].createElement("button", {
    className: classes['TutorialCard-Toolbar-Arrow'],
    disabled: tutorial.order === numTutorials - 1,
    onClick: function onClick() {
      return shiftOrder({
        _id: _id,
        direction: 1
      });
    }
  }, /*#__PURE__*/_react["default"].createElement("i", {
    className: "fa fa-arrow-right"
  })), /*#__PURE__*/_react["default"].createElement("button", {
    className: classes['TutorialCard-Toolbar-Button'],
    onClick: function onClick() {
      return openTutorial('Edit', tutorial);
    }
  }, "Edit"), /*#__PURE__*/_react["default"].createElement("button", {
    className: classes['TutorialCard-Toolbar-Button'],
    disabled: tutorial.hero,
    onClick: function onClick() {
      return publish({
        _id: _id,
        published: !published
      });
    },
    title: tutorial.hero ? 'You can’t unpublish a featured Recipe' : ''
  }, tutorial.published ? 'Unpublish' : 'Publish'), !tutorial.hero && /*#__PURE__*/_react["default"].createElement("button", {
    className: classes['TutorialCard-Toolbar-Button'],
    disabled: tutorial.hero,
    onClick: function onClick() {
      return makeHero({
        _id: _id,
        published: published
      });
    }
  }, "Feature"), /*#__PURE__*/_react["default"].createElement("button", {
    className: [classes['TutorialCard-Toolbar-Button'], classes['TutorialCard-Toolbar-Button_red']].join(' '),
    disabled: tutorial.hero && numTutorials > 1,
    onClick: function onClick() {
      return deleteTutorial({
        _id: _id,
        title: title
      });
    },
    title: tutorial.hero ? numTutorials > 1 && 'You can’t delete a featured Recipe' : ''
  }, "Delete"));
};

CardButtonBar.propTypes = {
  deleteTutorial: _propTypes["default"].func,
  isAdmin: _propTypes["default"].bool,
  makeHero: _propTypes["default"].func,
  numTutorials: _propTypes["default"].number,
  openTutorial: _propTypes["default"].func,
  publish: _propTypes["default"].func,
  shiftOrder: _propTypes["default"].func,
  tutorial: _tutorials.TutorialProp
};

var renderStatus = function renderStatus(isAdmin, tutorial) {
  var hero = tutorial.hero,
      published = tutorial.published;
  if (!isAdmin) return false;

  if (hero || !published) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: [classes['TutorialCard-Status'], hero ? classes['TutorialCard-Status_featured'] : ''].join(' ')
    }, /*#__PURE__*/_react["default"].createElement("i", {
      className: [classes['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,
      publish = _ref2.publish,
      shiftOrder = _ref2.shiftOrder,
      deleteTutorial = _ref2.deleteTutorial;
  var TutorialCardClasses = [className, classes.TutorialCard, !tutorial.published && !tutorialReview && classes.TutorialCard_unpublished].filter(function (e) {
    return e;
  }).join(' ');
  return /*#__PURE__*/_react["default"].createElement("section", {
    className: TutorialCardClasses
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: classes['TutorialCard-Top']
  }, /*#__PURE__*/_react["default"].createElement(_Tutorials.PermissionContext.Consumer, null, function (isAdmin) {
    return renderStatus(isAdmin && !tutorialReview, tutorial);
  }), tutorial.emoji && /*#__PURE__*/_react["default"].createElement(_components.Avatar, {
    className: classes['TutorialCard-Avatar'],
    hexColor: tutorial.backgroundColor
  }, tutorial.emoji), /*#__PURE__*/_react["default"].createElement("div", {
    className: classes['TutorialCard-Title']
  }, tutorial.title), /*#__PURE__*/_react["default"].createElement("div", {
    className: classes['TutorialCard-Description']
  }, tutorial.description, " "), /*#__PURE__*/_react["default"].createElement("div", {
    className: classes['TutorialCard-Steps']
  }, tutorial.stepCount, " Steps"), /*#__PURE__*/_react["default"].createElement(_components.Button, {
    bem: {
      blue: true
    },
    className: classes['TutorialCard-Button'],
    onClick: function onClick() {
      return openTutorial('View', tutorial);
    }
  }, "Open Recipe")), /*#__PURE__*/_react["default"].createElement(_Tutorials.PermissionContext.Consumer, null, function (isAdmin) {
    return /*#__PURE__*/_react["default"].createElement("footer", {
      className: classes['TutorialCard-Toolbar']
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: classes['TutorialCard-Toolbar-Wrapper']
    }, /*#__PURE__*/_react["default"].createElement(CardButtonBar, {
      deleteTutorial: deleteTutorial,
      isAdmin: isAdmin && !tutorialReview,
      makeHero: makeHero,
      numTutorials: numTutorials,
      openTutorial: openTutorial,
      publish: publish,
      shiftOrder: shiftOrder,
      tutorial: tutorial
    })));
  }));
}

TutorialCard.propTypes = {
  deleteTutorial: _propTypes["default"].func,
  makeHero: _propTypes["default"].func,
  numTutorials: _propTypes["default"].number,
  openTutorial: _propTypes["default"].func,
  publish: _propTypes["default"].func,
  shiftOrder: _propTypes["default"].func,
  tutorial: _tutorials.TutorialProp,
  tutorialReview: _propTypes["default"].bool
};
TutorialCard.defaultProps = {
  tutorialReview: false
};