var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import Icon from "../icon";
import BreadcrumbItem from "./BreadcrumbItem";
import BreadcrumbSeparator from "./BreadcrumbSeparator";
import "./style/breadcrumb.css";
var prefixCls = 'dmc-breadcrumb';

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

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

var defaultItemRender = function defaultItemRender(route, params, routes, paths) {
  var isLastItem = routes.indexOf(route) === routes.length - 1;
  var name = getBreadcrumbName(route, params);
  return isLastItem ? _jsx("span", {
    children: name
  }, void 0) : _jsx("a", __assign({
    href: "#/" + paths.join('/')
  }, {
    children: name
  }), void 0);
};

var filterFragment = function filterFragment(children) {
  return React.Children.toArray(children).map(function (element) {
    if ( /*#__PURE__*/React.isValidElement(element) && element.type === React.Fragment) {
      var props = element.props;
      return props.children;
    }

    return element;
  });
};

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

var genForRoutes = function genForRoutes(routes, params, itemRender, separator) {
  if (routes === void 0) {
    routes = [];
  }

  if (params === void 0) {
    params = {};
  }

  if (itemRender === void 0) {
    itemRender = defaultItemRender;
  }

  var paths = [];
  return routes.map(function (route, index) {
    var path = getPath(route.path, params);

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

    var lastSeparator = index === routes.length - 1;
    return _jsx(BreadcrumbItem, __assign({
      lastSeparator: lastSeparator,
      separator: separator
    }, {
      children: itemRender(route, params, routes, paths)
    }), path || route.breadcrumbName);
  });
};

var Breadcrumb = function Breadcrumb(props) {
  var separator = props.separator,
      className = props.className,
      params = props.params,
      routes = props.routes,
      children = props.children,
      withPrev = props.withPrev,
      prevOptions = props.prevOptions,
      itemRender = props.itemRender,
      prefix = props.prefix,
      prefixText = props.prefixText,
      restProps = __rest(props, ["separator", "className", "params", "routes", "children", "withPrev", "prevOptions", "itemRender", "prefix", "prefixText"]);

  var goPrevHref = prevOptions === null || prevOptions === void 0 ? void 0 : prevOptions.goPrevHref;
  var goPrev = prevOptions === null || prevOptions === void 0 ? void 0 : prevOptions.goPrev;
  var HtmlTag = goPrevHref ? 'a' : 'span';
  var showPrevSeparator = !children && (!routes || routes && routes.length === 0);

  var prevNode = prefix || _jsxs("span", __assign({
    className: prefixCls + "-item " + prefixCls + "-item-prev"
  }, {
    children: [_jsxs(HtmlTag, __assign({
      className: prefixCls + "-item-link",
      href: goPrevHref,
      onClick: goPrev
    }, {
      children: [_jsx(Icon, {
        icon: "left-back"
      }, void 0), prefixText]
    }), void 0), !showPrevSeparator && _jsx(BreadcrumbSeparator, {
      children: "|"
    }, void 0)]
  }), void 0);

  var crumbs;

  if (routes && routes.length > 0) {
    crumbs = genForRoutes(routes, params, itemRender, separator);
  } else if (children) {
    crumbs = React.Children.map(filterFragment(children), function (element, index) {
      if (!element) {
        return element;
      }

      return /*#__PURE__*/React.cloneElement(element, {
        separator: separator,
        key: index
      });
    });
  }

  return _jsxs("div", __assign({}, restProps, {
    className: classNames(prefixCls, className)
  }, {
    children: [withPrev && prevNode, crumbs]
  }), void 0);
};

Breadcrumb.defaultProps = {
  separator: '/',
  withPrev: true
};
Breadcrumb.displayName = 'Breadcrumb';
export default Breadcrumb;
var BreadcrumbMemo = /*#__PURE__*/React.memo(Breadcrumb);
BreadcrumbMemo.displayName = 'BreadcrumbMemo';
export { BreadcrumbMemo };

