"use strict";

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

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

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

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

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

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

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

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

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

var _warning = _interopRequireDefault(require("../_util/warning"));

var _BreadcrumbItem = _interopRequireDefault(require("./BreadcrumbItem"));

var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));

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 Breadcrumb = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(Breadcrumb, _Component);

  var _super = (0, _createSuper2["default"])(Breadcrumb);

  function Breadcrumb() {
    (0, _classCallCheck2["default"])(this, Breadcrumb);
    return _super.apply(this, arguments);
  }

  (0, _createClass2["default"])(Breadcrumb, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var props = this.props;
      (0, _warning["default"])(!('linkRender' in props || 'nameRender' in props), '`linkRender` and `nameRender` are removed, please use `itemRender` instead');
    }
  }, {
    key: "render",
    value: function render() {
      var crumbs;
      var _this$props = this.props,
          separator = _this$props.separator,
          prefixCls = _this$props.prefixCls,
          style = _this$props.style,
          className = _this$props.className,
          routes = _this$props.routes,
          _this$props$params = _this$props.params,
          params = _this$props$params === void 0 ? {} : _this$props$params,
          children = _this$props.children,
          _this$props$itemRende = _this$props.itemRender,
          itemRender = _this$props$itemRende === void 0 ? defaultItemRender : _this$props$itemRende,
          dropdownProps = _this$props.dropdownProps,
          listProps = _this$props.listProps;
      var getPrefixCls = this.context.getPrefixCls;

      if (routes && routes.length > 0) {
        var paths = [];
        crumbs = routes.map(function (route) {
          route.path = route.path || '';
          var path = route.path.replace(/^\//, '');
          Object.keys(params).forEach(function (key) {
            path = path.replace(":".concat(key), params[key]);
          });

          if (path) {
            paths.push(path);
          }

          return /*#__PURE__*/_react["default"].createElement(_BreadcrumbItem["default"], {
            prefixCls: prefixCls,
            dropdownProps: dropdownProps,
            listProps: listProps,
            separator: separator,
            key: route.breadcrumbName || path
          }, itemRender(route, params, routes, paths));
        });
      } else if (children) {
        crumbs = _react.Children.map(children, function (element, index) {
          if (!element) {
            return element;
          }

          (0, _warning["default"])(element.type && element.type.__C7N_BREADCRUMB_ITEM, 'Breadcrumb only accepts Breadcrumb.Item as it\'s children');
          return /*#__PURE__*/(0, _react.cloneElement)(element, {
            prefixCls: prefixCls,
            dropdownProps: dropdownProps,
            listProps: listProps,
            separator: separator,
            key: index
          });
        });
      }

      return /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _classnames["default"])(className, getPrefixCls('breadcrumb', prefixCls)),
        style: style
      }, crumbs);
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }]);
  return Breadcrumb;
}(_react.Component);

exports["default"] = Breadcrumb;
Breadcrumb.displayName = 'Breadcrumb';
Breadcrumb.defaultProps = {
  separator: '/'
};
//# sourceMappingURL=Breadcrumb.js.map
