"use strict";

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

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

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

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

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

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

var _Accordion = require("../Accordion");

var _Box = require("../Box");

var _Flex = require("../Flex");

var _Navigation = require("../Navigation");

var _Tooltip = require("../Tooltip");

var __jsx = _react["default"].createElement;

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; }

function CanvasMenu(props) {
  var _items$header, _items$content, _items$footer;

  var isMinified = props.isMinified,
      isVisible = props.isVisible,
      _props$items = props.items,
      items = _props$items === void 0 ? {} : _props$items,
      _props$as = props.as,
      as = _props$as === void 0 ? 'aside' : _props$as,
      children = props.children;

  var renderNavItem = function renderNavItem(_ref) {
    var label = _ref.label,
        href = _ref.href,
        _ref$icon = _ref.icon,
        icon = _ref$icon === void 0 ? null : _ref$icon,
        _ref$media = _ref.media,
        media = _ref$media === void 0 ? null : _ref$media,
        _ref$meta = _ref.meta,
        meta = _ref$meta === void 0 ? null : _ref$meta,
        _ref$exact = _ref.exact,
        exact = _ref$exact === void 0 ? true : _ref$exact,
        _ref$isAccordion = _ref.isAccordion,
        isAccordion = _ref$isAccordion === void 0 ? false : _ref$isAccordion,
        unstyled = _ref.unstyled,
        rest = (0, _objectWithoutProperties2["default"])(_ref, ["label", "href", "icon", "media", "meta", "exact", "isAccordion", "unstyled"]);

    if (isMinified && rest.isSubmenuItem) {
      return null;
    }

    var showFullItem = !isMinified && isVisible;
    return __jsx(_Navigation.Navigation.Item, (0, _extends2["default"])({
      href: href,
      exact: isAccordion ? false : exact,
      key: label + href
    }, rest), media && __jsx(_Tooltip.Tooltip, {
      label: label,
      placement: "right",
      closeOnClick: true,
      showTooltip: isMinified
    }, __jsx(_Navigation.Navigation.ItemMedia, {
      icon: icon,
      mr: !isMinified && 4,
      unstyled: unstyled
    }, media)), showFullItem && __jsx(_Navigation.Navigation.ItemText, null, label), meta && showFullItem && __jsx(_Navigation.Navigation.ItemMeta, null, meta));
  };

  var renderAccordion = function renderAccordion(accProps) {
    return __jsx(_Navigation.Navigation.Item, {
      key: accProps.children
    }, __jsx(_Accordion.Accordion, {
      allowToggle: true,
      defaultIndex: -1
    }, __jsx(_Accordion.AccordionItem, {
      defaultIsOpen: false
    }, __jsx(_Accordion.AccordionHeader, {
      py: 0,
      borderBottomWidth: 0
    }, renderNavItem(_objectSpread(_objectSpread({}, accProps), {}, {
      mb: 0,
      isParent: true,
      isMinified: isMinified
    })), !isMinified && __jsx(_Accordion.AccordionIcon, null)), accProps.children && accProps.children.map(function (child) {
      return __jsx(_Accordion.AccordionPanel, {
        p: 0,
        key: child
      }, renderNavItem(_objectSpread(_objectSpread({}, child), {}, {
        isSubmenuItem: true
      })));
    }))));
  };

  var renderItem = function renderItem(item) {
    // allows for passing and rendering components, rather than menu item objects
    if (!item || !(item !== null && item !== void 0 && item.label) && !(item !== null && item !== void 0 && item.media) && !(item !== null && item !== void 0 && item.icon)) {
      if (item && typeof item === 'function') {
        var itemCopy = item;
        return itemCopy({
          isMinified: isMinified,
          isVisible: isVisible
        });
      }

      return item;
    }

    if (item.isAccordion) {
      return renderAccordion(item);
    }

    return renderNavItem(item);
  };

  return __jsx(_Flex.Flex, {
    as: as,
    flex: 1,
    direction: "column"
  }, (items === null || items === void 0 ? void 0 : (_items$header = items.header) === null || _items$header === void 0 ? void 0 : _items$header.length) > 0 && __jsx(_Box.Box, null, items.header.map(function (item) {
    return __jsx(_Box.Box, {
      key: (item === null || item === void 0 ? void 0 : item.label) || item
    }, renderItem(typeof item === 'function' ? item : _objectSpread(_objectSpread({}, item), {}, {
      align: 'center',
      unstyled: true,
      minH: null,
      pb: 'spacing'
    })));
  })), __jsx(_Box.Box, {
    flexGrow: "1"
  }, (items === null || items === void 0 ? void 0 : (_items$content = items.content) === null || _items$content === void 0 ? void 0 : _items$content.length) > 0 && items.content.map(function (item) {
    return __jsx(_Box.Box, {
      key: (item === null || item === void 0 ? void 0 : item.label) || item
    }, renderItem(item));
  }), children), (items === null || items === void 0 ? void 0 : (_items$footer = items.footer) === null || _items$footer === void 0 ? void 0 : _items$footer.length) > 0 && __jsx(_Box.Box, null, items.footer.map(function (item) {
    return __jsx(_Box.Box, {
      key: (item === null || item === void 0 ? void 0 : item.label) || item
    }, renderItem(item));
  })));
}
//# sourceMappingURL=CanvasMenu.js.map