"use strict";

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

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

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

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

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

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

var _find = _interopRequireDefault(require("lodash/find"));

var makeLink = function makeLink() {
  for (var _len = arguments.length, parts = new Array(_len), _key = 0; _key < _len; _key++) {
    parts[_key] = arguments[_key];
  }

  return parts.join('/').replace(/\/{2,}/, '/');
};

var SideBarListItem = function SideBarListItem(_ref) {
  var activeDoc = _ref.activeDoc,
      baseUrl = _ref.baseUrl,
      page = _ref.page,
      children = _ref.children,
      _ref$className = _ref.className,
      className = _ref$className === void 0 ? '' : _ref$className;
  var slug = page.slug,
      title = page.title,
      linkUrl = page.link_url,
      linkExternal = page.link_external;
  var isActiveDoc = slug === activeDoc ? 'active' : '';
  var isActiveSub = children && (0, _find["default"])(page.children, {
    slug: activeDoc
  }) || false;
  return /*#__PURE__*/_react["default"].createElement("li", {
    className: (isActiveDoc || isActiveSub) && children ? 'subnav-expanded' : ''
  }, /*#__PURE__*/_react["default"].createElement("a", {
    className: "".concat(className, " text-overflow ").concat(isActiveDoc),
    href: linkUrl || makeLink(baseUrl, 'docs', slug),
    target: linkExternal ? '_blank' : ''
  }, (children || linkExternal) && /*#__PURE__*/_react["default"].createElement("span", {
    className: "fa fa-".concat(linkExternal ? 'external-link-square external' : 'chevron-right')
  }), title), children);
};

var SideBarList = function SideBarList(_ref2) {
  var pages = _ref2.pages,
      _ref2$className = _ref2.className,
      className = _ref2$className === void 0 ? '' : _ref2$className,
      props = (0, _objectWithoutProperties2["default"])(_ref2, ["pages", "className"]);
  return (
    /*#__PURE__*/

    /**
     * @todo: some of the destructuring herein is _very_ confusing...
     */
    _react["default"].createElement("ul", {
      className: className
    }, pages.map(function (page) {
      var hasChildren = 'children' in page && page.children.length > 0;
      if (page.hidden) return '';
      if (hasChildren) return /*#__PURE__*/_react["default"].createElement(SideBarListItem, (0, _extends2["default"])({}, props, {
        key: "page-".concat(page.slug),
        page: page
      }), /*#__PURE__*/_react["default"].createElement(SideBarList, (0, _extends2["default"])({}, props, {
        className: "subpages",
        pages: page.children
      })));
      if (className === 'subpages') props.className = 'subpage';
      return /*#__PURE__*/_react["default"].createElement(SideBarListItem, (0, _extends2["default"])({}, props, {
        key: "page-".concat(page.slug),
        page: page
      }));
    }))
  );
};

var SideBarSection = function SideBarSection(_ref3) {
  var activeDoc = _ref3.activeDoc,
      baseUrl = _ref3.baseUrl,
      title = _ref3.title,
      pages = _ref3.pages;
  return (
    /*#__PURE__*/

    /** @todo: This component is probably simple enough to be combined
     *         into the SideBarList or root SideBar component directly.
     */
    _react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("h3", null, title), /*#__PURE__*/_react["default"].createElement(SideBarList, {
      activeDoc: activeDoc,
      baseUrl: baseUrl,
      pages: pages
    }))
  );
};

var SideBar = function SideBar(_ref4) {
  var activeDoc = _ref4.activeDoc,
      baseUrl = _ref4.baseUrl,
      categories = _ref4.categories;
  return Array.isArray(categories) && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, categories.map(function (category) {
    return /*#__PURE__*/_react["default"].createElement(SideBarSection, (0, _extends2["default"])({}, category, {
      key: category.slug,
      activeDoc: activeDoc,
      baseUrl: baseUrl
    }));
  }));
};

SideBar.propTypes = {
  /**
   * Slug for the current page.
   */
  activeDoc: _propTypes["default"].string,

  /**
   * Project's generated base URL.
   */
  baseUrl: _propTypes["default"].string,

  /**
   * Collection of ReadMe docs or endpoint reference pages.
   */
  categories: _propTypes["default"].array
};
SideBarSection.propTypes = {
  activeDoc: _propTypes["default"].string,
  baseUrl: _propTypes["default"].string,
  pages: _propTypes["default"].array,
  title: _propTypes["default"].string
};
SideBarList.propTypes = {
  activeDoc: _propTypes["default"].string,
  baseUrl: _propTypes["default"].string,
  className: _propTypes["default"].string,
  pages: _propTypes["default"].array
};
SideBarListItem.propTypes = {
  activeDoc: _propTypes["default"].string,
  baseUrl: _propTypes["default"].string,
  page: _propTypes["default"].object
};
var _default = SideBar;
exports["default"] = _default;