"use strict";

var __assign = void 0 && (void 0).__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 __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__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;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

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

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var classnames_1 = __importDefault(require("classnames"));

var icon_1 = __importDefault(require("../icon"));

var BreadcrumbItem_1 = __importDefault(require("./BreadcrumbItem"));

var BreadcrumbSeparator_1 = __importDefault(require("./BreadcrumbSeparator"));

require("./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_runtime_1.jsx("span", {
    children: name
  }, void 0) : jsx_runtime_1.jsx("a", __assign({
    href: "#/" + paths.join('/')
  }, {
    children: name
  }), void 0);
};

var filterFragment = function filterFragment(children) {
  return React.Children.toArray(children).map(function (element) {
    if (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_runtime_1.jsx(BreadcrumbItem_1["default"], __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 || jsx_runtime_1.jsxs("span", __assign({
    className: prefixCls + "-item " + prefixCls + "-item-prev"
  }, {
    children: [jsx_runtime_1.jsxs(HtmlTag, __assign({
      className: prefixCls + "-item-link",
      href: goPrevHref,
      onClick: goPrev
    }, {
      children: [jsx_runtime_1.jsx(icon_1["default"], {
        icon: "left-back"
      }, void 0), prefixText]
    }), void 0), !showPrevSeparator && jsx_runtime_1.jsx(BreadcrumbSeparator_1["default"], {
      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 React.cloneElement(element, {
        separator: separator,
        key: index
      });
    });
  }

  return jsx_runtime_1.jsxs("div", __assign({}, restProps, {
    className: classnames_1["default"](prefixCls, className)
  }, {
    children: [withPrev && prevNode, crumbs]
  }), void 0);
};

Breadcrumb.defaultProps = {
  separator: '/',
  withPrev: true
};
Breadcrumb.displayName = 'Breadcrumb';
exports["default"] = Breadcrumb;
var BreadcrumbMemo = React.memo(Breadcrumb);
exports.BreadcrumbMemo = BreadcrumbMemo;
BreadcrumbMemo.displayName = 'BreadcrumbMemo';

