import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import React, { Children, cloneElement, Component } from 'react';
import classNames from 'classnames';
import warning from '../_util/warning';
import BreadcrumbItem from './BreadcrumbItem';
import ConfigContext from '../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.createElement("span", null, name) : /*#__PURE__*/React.createElement("a", {
    href: "#/".concat(paths.join('/'))
  }, name);
}

var Breadcrumb = /*#__PURE__*/function (_Component) {
  _inherits(Breadcrumb, _Component);

  var _super = _createSuper(Breadcrumb);

  function Breadcrumb() {
    _classCallCheck(this, Breadcrumb);

    return _super.apply(this, arguments);
  }

  _createClass(Breadcrumb, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var props = this.props;
      warning(!('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.createElement(BreadcrumbItem, {
            prefixCls: prefixCls,
            dropdownProps: dropdownProps,
            listProps: listProps,
            separator: separator,
            key: route.breadcrumbName || path
          }, itemRender(route, params, routes, paths));
        });
      } else if (children) {
        crumbs = Children.map(children, function (element, index) {
          if (!element) {
            return element;
          }

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

      return /*#__PURE__*/React.createElement("div", {
        className: classNames(className, getPrefixCls('breadcrumb', prefixCls)),
        style: style
      }, crumbs);
    }
  }], [{
    key: "contextType",
    get: function get() {
      return ConfigContext;
    }
  }]);

  return Breadcrumb;
}(Component);

export { Breadcrumb as default };
Breadcrumb.displayName = 'Breadcrumb';
Breadcrumb.defaultProps = {
  separator: '/'
};
//# sourceMappingURL=Breadcrumb.js.map
