"use strict";

exports.__esModule = true;
exports.default = void 0;
var _tslib = require("tslib");
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _utils = require("../utils");
var _tree = require("../utils/tree");
var _view = _interopRequireDefault(require("../view"));
var _context = _interopRequireDefault(require("./context"));
var _item = _interopRequireDefault(require("./item"));
var _subNav = _interopRequireDefault(require("./sub-nav"));
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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 getInitSelectKeys = function (props) {
  var ret = [];
  if ('selectedKeys' in props) {
    if ((0, _utils.isValidArray)(props.selectedKeys)) {
      ret = props.selectedKeys;
    } else if ((0, _utils.isString)(props.selectedKeys) && props.selectedKeys) {
      ret = [props.selectedKeys];
    }
  } else if ('defaultSelectedKeys' in props) {
    if ((0, _utils.isValidArray)(props.defaultSelectedKeys)) {
      ret = props.defaultSelectedKeys;
    } else if ((0, _utils.isString)(props.defaultSelectedKeys) && props.defaultSelectedKeys) {
      ret = [props.defaultSelectedKeys];
    }
  }
  return (0, _utils.isValidArray)(ret) ? ret : [];
};
var checkDataSourceKey = function (dataSource) {
  var rs = true;
  (0, _tree.dfs)(dataSource, function (node) {
    if (rs && !node.key) {
      rs = false;
    }
  });
  return rs;
};
var Nav = function (props, ref) {
  var _props$prefix = props.prefix,
    prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
    className = props.className,
    _props$size = props.size,
    size = _props$size === void 0 ? 'medium' : _props$size,
    _props$type = props.type,
    type = _props$type === void 0 ? 'normal' : _props$type,
    _props$model = props.model,
    model = _props$model === void 0 ? 'solid' : _props$model,
    _props$direction = props.direction,
    direction = _props$direction === void 0 ? 'ver' : _props$direction,
    defaultSelectedKeys = props.defaultSelectedKeys,
    selectedKeys = props.selectedKeys,
    _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? [] : _props$dataSource,
    children = props.children,
    onSelect = props.onSelect,
    others = (0, _tslib.__rest)(props, ["prefix", "className", "size", "type", "model", "direction", "defaultSelectedKeys", "selectedKeys", "dataSource", "children", "onSelect"]);
  var _useState = (0, _react.useState)(getInitSelectKeys(props)),
    curSelectedKeys = _useState[0],
    setCurrentSelectedKeys = _useState[1];
  var isControlled = ('selectedKeys' in props);
  var clsPrefix = prefix + "nav";
  var navClassNames = (0, _classnames.default)("" + clsPrefix, clsPrefix + "--" + direction, className);
  var childProps = {
    size: size,
    type: type,
    model: model,
    direction: direction
  };
  (0, _react.useEffect)(function () {
    if ((0, _utils.isValidArray)(dataSource) && !checkDataSourceKey(dataSource)) {
      console.error('each item in dataSource should have key property, plese check');
    }
  }, []);
  (0, _react.useEffect)(function () {
    if ('selectedKeys' in props) {
      if ((0, _utils.isUndef)(props.selectedKeys)) {
        var val = getInitSelectKeys(props);
        setCurrentSelectedKeys(val);
      } else if ((0, _utils.isValidArray)(props.selectedKeys)) {
        setCurrentSelectedKeys(props.selectedKeys);
      } else if ((0, _utils.isString)(props.selectedKeys) && props.selectedKeys) {
        setCurrentSelectedKeys([props.selectedKeys]);
      }
    }
  }, [selectedKeys]);
  return /*#__PURE__*/(0, _react.createElement)(_context.default.Provider, {
    value: {
      curSelectedKeys: curSelectedKeys,
      onItemSelect: function onItemSelect(itemKey, e) {
        if (!isControlled) {
          setCurrentSelectedKeys([itemKey]);
        }
        if (onSelect) {
          onSelect(itemKey, e);
        }
      },
      model: model,
      direction: direction,
      type: type,
      size: size,
      clsPrefix: clsPrefix
    }
  }, /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({}, others, {
    ref: ref,
    className: navClassNames
  }), dataSource.length ? function getRenderNode() {
    return dataSource.map(function (item, index) {
      var itemKey = item.key || prefix + "nav-" + index;
      var icon = item.icon,
        iconUri = item.iconUri,
        _children = item.children;
      return (0, _utils.isValidArray)(_children) ? /*#__PURE__*/(0, _react.createElement)(_subNav.default, {
        key: itemKey,
        itemKey: itemKey,
        icon: icon,
        iconUri: iconUri,
        label: item.label,
        dataSource: _children
      }) : /*#__PURE__*/(0, _react.createElement)(_item.default, {
        key: itemKey,
        itemKey: itemKey,
        icon: icon,
        iconUri: iconUri,
        label: item.label
      });
    });
  }() : function getChildrenNode() {
    var _key = 0;
    return _react.Children.map(children, function (child) {
      _key += 1;
      var itemKey = child.key || _key;
      return (0, _react.cloneElement)(child, Object.assign(Object.assign({}, childProps), {
        key: "" + _key,
        itemKey: "" + itemKey,
        size: size
      }));
    });
  }()));
};
Nav.displayName = 'Nav';
var _default = /*#__PURE__*/(0, _react.forwardRef)(Nav);
exports.default = _default;