"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

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

var _Item = _interopRequireDefault(require("../../../../components/Menu/Item"));

var _apiEndpoints = require("../../proptypes/apiEndpoints");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

// Style
var classes = {};
/* istanbul ignore next */

if (typeof window !== 'undefined') classes = require("./style.module.scss");

var TutorialReferencePicker = function TutorialReferencePicker(_ref) {
  var endpoints = _ref.endpoints,
      referenceDisplay = _ref.referenceDisplay,
      setReferenceSelection = _ref.setReferenceSelection;

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      subMenu = _useState2[0],
      toggleSubMenu = _useState2[1];

  var _useState3 = (0, _react.useState)({}),
      _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
      endpoint = _useState4[0],
      setEndpoint = _useState4[1];

  var _useState5 = (0, _react.useState)({}),
      _useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
      selection = _useState6[0],
      setSelection = _useState6[1];

  var paintReferenceSelections = function paintReferenceSelections() {
    var selectionMap = endpoints.reduce(function (obj, ep) {
      var active = ep.children.reduce(function (acc, _ref2) {
        var _id = _ref2._id;
        if (referenceDisplay.includes(_id)) acc[_id] = true;
        return acc;
      }, {});
      obj[ep.categoryId] = _objectSpread({}, active);
      return obj;
    }, {});
    setSelection(selectionMap);
  };

  (0, _react.useEffect)(function () {
    paintReferenceSelections();
  }, [referenceDisplay.length]);

  if (subMenu) {
    return /*#__PURE__*/_react["default"].createElement(_components.Menu, {
      className: classes.TutorialReferencePicker
    }, /*#__PURE__*/_react["default"].createElement(_Item["default"], {
      onClick: function onClick() {
        setEndpoint({});
        toggleSubMenu(false);
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: classes['TutorialReferencePicker-Header']
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: classes['TutorialReferencePicker-Header-BackArrow']
    }, "\u25C0"), /*#__PURE__*/_react["default"].createElement("span", {
      className: classes['TutorialReferencePicker-Item-Name']
    }, endpoint.title))), endpoint.children && endpoint.children.map(function (child, index) {
      return /*#__PURE__*/_react["default"].createElement(_Item["default"], {
        key: "TutorialReferencePicker-Child-".concat(index),
        onClick: function onClick() {
          return setReferenceSelection(child._id);
        }
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: classes['TutorialReferencePicker-Item']
      }, /*#__PURE__*/_react["default"].createElement("i", {
        className: ["fa fa-check", classes['TutorialReferencePicker-Check'], selection[endpoint.categoryId][child._id] ? classes['TutorialReferencePicker-Check_selected'] : ''].join(' ')
      }), /*#__PURE__*/_react["default"].createElement("span", {
        className: classes['TutorialReferencePicker-Item-Name'],
        title: child.title
      }, /*#__PURE__*/_react["default"].createElement(_components.Method, {
        className: classes['TutorialReferencePicker-Item-Name-Method'],
        type: child.api.method.toLowerCase(),
        width: 40
      }), child.title)));
    }));
  }

  return /*#__PURE__*/_react["default"].createElement(_components.Menu, {
    className: classes.TutorialReferencePicker
  }, endpoints && endpoints.map(function (ep, index) {
    return /*#__PURE__*/_react["default"].createElement(_Item["default"], {
      key: "TutorialReferencePicker-Parent-".concat(index),
      className: "Menu-Item_arrow",
      onClick: function onClick() {
        setEndpoint(ep);
        toggleSubMenu(true);
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: classes['TutorialReferencePicker-Item']
    }, /*#__PURE__*/_react["default"].createElement("i", {
      className: ["fa fa-check", classes['TutorialReferencePicker-Check'], selection[ep.categoryId] && Object.keys(selection[ep.categoryId]).length ? classes['TutorialReferencePicker-Check_selected'] : ''].join(' ')
    }), /*#__PURE__*/_react["default"].createElement("span", {
      className: classes['TutorialReferencePicker-Item-Name']
    }, ep.title)));
  }));
};

TutorialReferencePicker.propTypes = {
  endpoints: _propTypes["default"].arrayOf(_apiEndpoints.ApiEndpointProp),
  referenceDisplay: _propTypes["default"].arrayOf(_propTypes["default"].string),
  setReferenceSelection: _propTypes["default"].func
};
var _default = TutorialReferencePicker;
exports["default"] = _default;