"use strict";

exports.__esModule = true;
exports.default = void 0;
var _tslib = require("tslib");
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _meetReactComponentOne = require("@alifd/meet-react-component-one");
var _view = _interopRequireDefault(require("../view"));
var _icon = _interopRequireDefault(require("../icon"));
var _utils = require("../utils");
var _context = _interopRequireDefault(require("./context"));
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 ? 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 NavItem = function (props, ref) {
  var _classNames, _classNames2, _classNames3;
  var className = props.className,
    label = props.label,
    itemKey = props.itemKey,
    _props$disableSelect = props.disableSelect,
    disableSelect = _props$disableSelect === void 0 ? false : _props$disableSelect,
    _props$level = props.level,
    level = _props$level === void 0 ? 1 : _props$level,
    icon = props.icon,
    iconUri = props.iconUri,
    children = props.children,
    extra = props.extra,
    _props$onClick = props.onClick,
    onClick = _props$onClick === void 0 ? function () {} : _props$onClick,
    others = (0, _tslib.__rest)(props, ["className", "label", "itemKey", "disableSelect", "level", "icon", "iconUri", "children", "extra", "onClick"]);
  var ctx = (0, _react.useContext)(_context.default);
  var curSelectedKeys = ctx.curSelectedKeys,
    clsPrefix = ctx.clsPrefix,
    size = ctx.size,
    type = ctx.type,
    model = ctx.model,
    direction = ctx.direction,
    onItemSelect = ctx.onItemSelect;
  var selected = itemKey && (0, _utils.isValidArray)(curSelectedKeys) && curSelectedKeys.includes(itemKey);
  var clses = {
    item: (0, _classnames.default)(className, clsPrefix + "-item", clsPrefix + "-item--" + size, clsPrefix + "-item--" + type + "-" + model, clsPrefix + "--" + direction + "-item", clsPrefix + "-item--level" + level, (_classNames = {}, _classNames[clsPrefix + "-item--" + type + "-" + model + "--active"] = selected, _classNames)),
    icon: (0, _classnames.default)(clsPrefix + "-item-icon", clsPrefix + "-item-icon--" + size, clsPrefix + "-item--" + type + "-" + model + "-icon", (_classNames2 = {}, _classNames2[clsPrefix + "-item--" + type + "-" + model + "--active-icon"] = selected, _classNames2)),
    label: (0, _classnames.default)(clsPrefix + "-item-label", clsPrefix + "-item-label--" + size, clsPrefix + "-item--" + type + "-" + model + "-label", (_classNames3 = {}, _classNames3[clsPrefix + "-item--" + type + "-" + model + "--active-label"] = selected, _classNames3)),
    extra: clsPrefix + "-item-extra-wrapper--" + type
  };
  var iconCls = icon && icon.props ? icon.props.className : '';
  var iconProps = {};
  if ((0, _utils.isString)(icon)) {
    iconProps.name = icon;
  } else if (iconUri) {
    iconProps.uri = iconUri;
  }
  return /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({
    ref: ref
  }, others, {
    className: clses.item,
    onClick: function onItemClick(e) {
      if (onClick) {
        onClick(itemKey);
      }
      if (disableSelect) {
        return;
      }
      if (onItemSelect) {
        onItemSelect(itemKey, e);
      }
    }
  }), (0, _react.isValidElement)(icon) ? (0, _react.cloneElement)(icon, {
    className: (0, _classnames.default)(clses.icon, iconCls)
  }) : /*#__PURE__*/(0, _react.createElement)(_icon.default, _extends({}, others, iconProps, {
    className: clses.icon
  })), function renderLabel() {
    if (children) {
      return /*#__PURE__*/(0, _react.createElement)(_view.default, {
        className: clsPrefix + "-item-children-wrapper"
      }, _react.Children.map(children, function (child, index) {
        if ((0, _react.isValidElement)(child)) {
          return (0, _react.cloneElement)(child, {
            key: "mt_nav_item_" + index
          });
        } else if ((0, _utils.isString)(child) || (0, _utils.isNumber)(child)) {
          return /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
            className: clses.label,
            numberOfLines: 1
          }, child);
        }
        return null;
      }));
    } else if ((0, _react.isValidElement)(label)) {
      return /*#__PURE__*/(0, _react.createElement)(_view.default, {
        className: clses.label
      }, label);
    } else {
      return /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
        className: clses.label,
        numberOfLines: 1
      }, label);
    }
  }(), extra ? /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clses.extra
  }, extra) : null);
};
NavItem.displayName = 'Nav.Item';
var _default = /*#__PURE__*/(0, _react.forwardRef)(NavItem);
exports.default = _default;