"use strict";

exports.__esModule = true;
exports.default = void 0;

var _tslib = require("tslib");

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

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

var _view = _interopRequireDefault(require("../view"));

var _icon2 = _interopRequireDefault(require("../icon"));

var _item = _interopRequireDefault(require("./item"));

var _context = _interopRequireDefault(require("./context"));

var _utils = require("../utils");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var SubNav = function (props, ref) {
  var _classNames;

  var className = props.className,
      icon = props.icon,
      _props$label = props.label,
      label = _props$label === void 0 ? '' : _props$label,
      _props$level = props.level,
      level = _props$level === void 0 ? 1 : _props$level,
      _props$expanded = props.expanded,
      expanded = _props$expanded === void 0 ? false : _props$expanded,
      _props$itemKey = props.itemKey,
      itemKey = _props$itemKey === void 0 ? '' : _props$itemKey,
      dataSource = props.dataSource,
      children = props.children,
      others = (0, _tslib.__rest)(props, ["className", "icon", "label", "level", "expanded", "itemKey", "dataSource", "children"]);
  var ctx = (0, _react.useContext)(_context.default);
  var onItemSelect = ctx.onItemSelect,
      clsPrefix = ctx.clsPrefix,
      model = ctx.model,
      type = ctx.type,
      size = ctx.size;

  var _useState = (0, _react.useState)(expanded),
      isExpanded = _useState[0],
      setExpanded = _useState[1];

  var keyCounter = 0;
  return /*#__PURE__*/(0, _react.createElement)(_react.Fragment, null, /*#__PURE__*/(0, _react.createElement)(_item.default, _extends({
    ref: ref
  }, others, {
    icon: icon,
    label: label,
    level: level,
    onClick: function handleSelect() {
      setExpanded(!isExpanded);
    },
    className: (0, _classnames.default)(className, (_classNames = {}, _classNames[clsPrefix + "--" + type + "-" + model + "-active"] = isExpanded, _classNames)),
    disableSelect: true,
    extra: /*#__PURE__*/(0, _react.createElement)(_icon2.default, {
      name: isExpanded ? 'arrow-up' : 'arrow-down',
      size: "s"
    })
  })), isExpanded ? /*#__PURE__*/(0, _react.createElement)(_view.default, null, dataSource ? function renderChildrenByData() {
    if (!dataSource) {
      return null;
    }

    return dataSource.map(function (item) {
      var newLevel = level + 1;
      var _itemKey = item.key;
      var _icon = item.icon,
          iconUri = item.iconUri,
          _children = item.children;
      return (0, _utils.isValidArray)(_children) ? /*#__PURE__*/(0, _react.createElement)(SubNav, {
        key: _itemKey,
        itemKey: _itemKey,
        icon: icon,
        iconUri: iconUri,
        label: item.label,
        level: newLevel,
        dataSource: _children
      }) : /*#__PURE__*/(0, _react.createElement)(_item.default, {
        key: _itemKey,
        itemKey: _itemKey,
        level: newLevel,
        icon: _icon,
        iconUri: iconUri,
        label: item.label
      });
    });
  }() : _react.Children.map(children, function renderChild(child) {
    keyCounter++;

    var _key = child.key || itemKey + "-" + keyCounter;

    if (!(0, _react.isValidElement)(child)) {
      return null;
    }

    return (0, _react.cloneElement)(child, {
      key: _key,
      itemKey: _key,
      size: size,
      level: level + 1,
      onItemSelect: onItemSelect
    });
  })) : null);
};

SubNav.displayName = 'Nav.SubNav';

var _default = /*#__PURE__*/(0, _react.forwardRef)(SubNav);

exports.default = _default;