"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

var _tslib = require("tslib");

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

var _consoleComponents = require("@alicloud/console-components");

var _classnames = _interopRequireDefault(require("classnames"));

var _Header = _interopRequireDefault(require("./Header"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var PrimaryMenu = function PrimaryMenu(props) {
  var className = props.className,
      rest = (0, _tslib.__rest)(props, ["className"]);
  return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Nav, (0, _tslib.__assign)({
    className: (0, _classnames["default"])("lp-primary-menu", className),
    openMode: "multiple"
  }, rest, {
    type: "normal",
    direction: "ver",
    activeDirection: null,
    mode: "inline",
    triggerType: "click",
    inlineIndent: 16,
    popupAlign: "follow",
    hasArrow: true
  }));
};

var SecondaryMenu = function SecondaryMenu(props) {
  var className = props.className,
      rest = (0, _tslib.__rest)(props, ["className"]);
  return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Nav, (0, _tslib.__assign)({}, rest, {
    type: "line",
    className: (0, _classnames["default"])("lp-sencondary-menu", className),
    direction: "ver",
    activeDirection: "right",
    triggerType: "click",
    inlineIndent: 8
  }));
};

function mapItemToJSX(item) {
  var key = item.key,
      label = item.label,
      _a = item.disabled,
      disabled = _a === void 0 ? false : _a,
      _b = item.visible,
      visible = _b === void 0 ? true : _b,
      render = item.render,
      subItems = item.children,
      navProps = item.navProps;

  if (visible) {
    if (subItems) {
      return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Nav.SubNav, (0, _tslib.__assign)({
        key: key,
        label: label
      }, navProps), subItems.map(mapItemToJSX));
    }

    return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Nav.Item, (0, _tslib.__assign)({
      key: key,
      disabled: disabled
    }, navProps), render ? render(item) : label);
  }

  return null;
}

function getPathMap(item, path, container) {
  if (path === void 0) {
    path = [];
  }

  if (container === void 0) {
    container = {};
  }

  if (item.children) {
    path.push(item.key);
    item.children.forEach(function (subItem) {
      return getPathMap(subItem, path, container);
    });
  } else {
    container[item.key] = path;
  }
}

var useOpenKeys = function useOpenKeys(activeKey, defaultActiveKey, dataSource) {
  var pathMap = {};
  dataSource && dataSource.forEach(function (subItem) {
    return getPathMap(subItem, [], pathMap);
  });
  var openKeys_ = undefined;
  var defaultOpenKeys = undefined;

  if (activeKey) {
    openKeys_ = Array.isArray(activeKey) ? activeKey.map(function (item) {
      return pathMap[item];
    }).join().split(",") : pathMap[activeKey];
  }

  if (defaultActiveKey) {
    defaultOpenKeys = Array.isArray(defaultActiveKey) ? defaultActiveKey.map(function (item) {
      return pathMap[item];
    }).join().split(",") : pathMap[defaultActiveKey];
  }

  return {
    openKeys_: openKeys_,
    defaultOpenKeys: defaultOpenKeys
  };
};

var NavMenu = function NavMenu(_a) {
  var _b = _a.type,
      type = _b === void 0 ? 'primary' : _b,
      header = _a.header,
      headers = _a.headers,
      onSelectHeader = _a.onSelectHeader,
      dataSource = _a.dataSource,
      children = _a.children,
      activeKey = _a.activeKey,
      defaultActiveKey = _a.defaultActiveKey,
      _c = _a.isSildeNav,
      isSildeNav = _c === void 0 ? true : _c,
      restProps = (0, _tslib.__rest)(_a, ["type", "header", "headers", "onSelectHeader", "dataSource", "children", "activeKey", "defaultActiveKey", "isSildeNav"]);
  var ExactMenuComponent = type === 'secondary' ? SecondaryMenu : PrimaryMenu;

  var _d = useOpenKeys(activeKey, defaultActiveKey, dataSource),
      openKeys_ = _d.openKeys_,
      defaultOpenKeys = _d.defaultOpenKeys;

  var _e = (0, _react.useState)(openKeys_),
      openKeys = _e[0],
      setOpenKey = _e[1];

  return /*#__PURE__*/_react["default"].createElement(ExactMenuComponent, (0, _tslib.__assign)({
    openKeys: openKeys,
    defaultOpenKeys: defaultOpenKeys,
    onOpen: function onOpen(v) {
      return setOpenKey(v);
    }
  }, restProps, {
    header: header && /*#__PURE__*/_react["default"].createElement(_Header["default"], {
      isSildeNav: true,
      header: header,
      headers: headers,
      onSelectHeader: onSelectHeader
    }),
    selectedKeys: activeKey,
    defaultSelectedKeys: defaultActiveKey
  }), Array.isArray(dataSource) && dataSource.map(mapItemToJSX), children);
};

NavMenu.Item = _consoleComponents.Nav.Item;
NavMenu.SubMenu = _consoleComponents.Nav.SubNav;
var _default = NavMenu;
exports["default"] = _default;