"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

var _context = require("../config-provider/context");

var _ReactNodeValidate = require("../utils/ReactNodeValidate");

var _breadcrumbItem = _interopRequireDefault(require("./breadcrumb-item"));

var _BreadcrumbSeparator = _interopRequireDefault(require("./BreadcrumbSeparator"));

var _menu = _interopRequireDefault(require("../menu"));

function getBreadcrumbName(route, params) {
  if (!route.breadcrumbName) {
    return null;
  }

  var paramsKeys = Object.keys(params).join('|');
  var name = route.breadcrumbName.replace(new RegExp(":(".concat(paramsKeys, ")"), 'g'), function (replacement, key) {
    return params[key] || replacement;
  });
  return name;
}

function defaultItemRender(route, params, routes, paths) {
  var isLastItem = routes.indexOf(route) === routes.length - 1;
  var name = getBreadcrumbName(route, params);
  return isLastItem ? /*#__PURE__*/_react.default.createElement("span", null, name) : /*#__PURE__*/_react.default.createElement("a", {
    href: "#/".concat(paths.join('/'))
  }, name);
}

var getPath = function getPath(path, params) {
  var pt = (path || '').replace(/^\//, '');
  Object.keys(params).forEach(function (key) {
    pt = pt.replace(":".concat(key), params[key]);
  });
  return pt;
};

var addChildPath = function addChildPath(paths) {
  var childPath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var params = arguments.length > 2 ? arguments[2] : undefined;
  var lastPaths;
  var originalPaths = (0, _toConsumableArray2.default)(paths);
  var path = getPath(childPath, params);

  if (path) {
    originalPaths.push(path);
    lastPaths = originalPaths;
  }

  return lastPaths;
};

var Breadcrumb = function Breadcrumb(props) {
  var _props$separator = props.separator,
      separator = _props$separator === void 0 ? '/' : _props$separator,
      routes = props.routes,
      children = props.children,
      _props$itemRender = props.itemRender,
      itemRender = _props$itemRender === void 0 ? defaultItemRender : _props$itemRender,
      style = props.style,
      _props$params = props.params,
      params = _props$params === void 0 ? {} : _props$params,
      className = props.className,
      restProps = (0, _objectWithoutProperties2.default)(props, ["separator", "routes", "children", "itemRender", "style", "params", "className"]);

  var _useContext = (0, _react.useContext)(_context.ConfigContext),
      getPrefixClassName = _useContext.getPrefixClassName;

  var breadcrumbCla = getPrefixClassName('bread-crumb');
  var breadcrumbClassName = (0, _classnames.default)(breadcrumbCla, className);
  var breadcrumbNode;

  if (routes && routes.length > 0) {
    var paths = [];
    breadcrumbNode = routes.map(function (route) {
      var path = getPath(route.path, params);

      if (path) {
        paths.push(path);
      } // generated overlay by route.children


      var overlay;

      if (route.children && route.children.length) {
        overlay = /*#__PURE__*/_react.default.createElement(_menu.default, null, route.children.map(function (child) {
          return /*#__PURE__*/_react.default.createElement(_menu.default.Item, {
            key: child.path || child.breadcrumbName
          }, itemRender(child, params, routes, addChildPath(paths, child.path, params)));
        }));
      }

      return /*#__PURE__*/_react.default.createElement(_breadcrumbItem.default, {
        overlay: overlay,
        separator: separator,
        key: path || route.breadcrumbName
      }, itemRender(route, params, routes, paths));
    });
  } else if (children) {
    // 当只 children  只有一个元素的时候。只是一个对象； 不是数组；所以需要包装成一个数组
    var arrElement = children && !Array.isArray(children) ? [children] : children;
    breadcrumbNode = arrElement.map(function (element, index) {
      if (!element) {
        return element;
      }

      return (0, _ReactNodeValidate.cloneElement)(element, {
        separator: separator,
        key: index
      });
    });
  }

  return /*#__PURE__*/_react.default.createElement("div", Object.assign({
    className: breadcrumbClassName,
    style: style
  }, restProps), breadcrumbNode);
};

Breadcrumb.Item = _breadcrumbItem.default;
Breadcrumb.Separator = _BreadcrumbSeparator.default;
var _default = Breadcrumb;
exports.default = _default;