'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.cjs');
var index$1 = require('../../menu/index.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var index = require('../style/index.cjs');
var BreadcrumbItem = require('./BreadcrumbItem.cjs');
var props = require('./props.cjs');
var props$1 = require('../../../utils/vue/props.cjs');
var log = require('../../../utils/log.cjs');

function getBreadcrumbName(route, params) {
  if (!route.breadcrumbName)
    return null;
  const paramsKeys = Object.keys(params).join("|");
  const name = route.breadcrumbName.replace(new RegExp(`:(${paramsKeys})`, "g"), (replacement, key) => params[key] || replacement);
  return name;
}
function defaultItemRender(opt) {
  const {
    route,
    params,
    routes,
    paths
  } = opt;
  const isLastItem = routes.indexOf(route) === routes.length - 1;
  const name = getBreadcrumbName(route, params);
  return isLastItem ? vue.createVNode("span", null, [name]) : vue.createVNode("a", {
    "href": `#/${paths.join("/")}`
  }, [name]);
}
var ABreadcrumb = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ABreadcrumb",
  inheritAttrs: false,
  props: props.breadcrumbProps(),
  slots: Object,
  setup(props, {
    slots,
    attrs
  }) {
    const {
      prefixCls,
      direction
    } = useConfigInject.default("breadcrumb", props);
    const [wrapSSR, hashId] = index.default(prefixCls);
    const getPath = (path, params) => {
      path = (path || "").replace(/^\//, "");
      Object.keys(params).forEach((key) => {
        path = path.replace(`:${key}`, params[key]);
      });
      return path;
    };
    const addChildPath = (paths, childPath, params) => {
      const originalPaths = [...paths];
      const path = getPath(childPath || "", params);
      if (path)
        originalPaths.push(path);
      return originalPaths;
    };
    const genForRoutes = ({
      routes = [],
      params = {},
      separator,
      itemRender = defaultItemRender
    }) => {
      const paths = [];
      return routes.map((route) => {
        const path = getPath(route.path, params);
        if (path)
          paths.push(path);
        const tempPaths = [...paths];
        let overlay = null;
        if (route.children && route.children.length) {
          overlay = vue.createVNode(index$1.Menu, {
            "items": route.children.map((child) => ({
              key: child.path || child.breadcrumbName,
              label: itemRender({
                route: child,
                params,
                routes,
                paths: addChildPath(tempPaths, child.path, params)
              })
            }))
          }, null);
        }
        const itemProps = {
          separator
        };
        if (overlay)
          itemProps.overlay = overlay;
        return vue.createVNode(BreadcrumbItem.default, {
          ...itemProps,
          "key": path || route.breadcrumbName
        }, {
          default: () => [itemRender({
            route,
            params,
            routes,
            paths: tempPaths
          })]
        });
      });
    };
    return () => {
      var _a;
      let crumbs;
      const {
        routes,
        params = {}
      } = props;
      const children = props$1.flattenChildren(props$1.getPropsSlot(slots, props));
      const separator = (_a = props$1.getPropsSlot(slots, props, "separator")) != null ? _a : "/";
      const itemRender = props.itemRender || slots.itemRender || defaultItemRender;
      if (routes && routes.length > 0) {
        crumbs = genForRoutes({
          routes,
          params,
          separator,
          itemRender
        });
      } else if (children.length) {
        crumbs = children.map((element, index) => {
          log.devWarning(typeof element.type === "object" && (element.type.__ANT_BREADCRUMB_ITEM || element.type.__ANT_BREADCRUMB_SEPARATOR), "Breadcrumb", "Only accepts Breadcrumb.Item and Breadcrumb.Separator as it's children");
          return vue.cloneVNode(element, {
            separator,
            key: index
          });
        });
      }
      const breadcrumbClassName = {
        [prefixCls.value]: true,
        [`${prefixCls.value}-rtl`]: direction.value === "rtl",
        [`${attrs.class}`]: !!attrs.class,
        [hashId.value]: true
      };
      return wrapSSR(vue.createVNode("nav", {
        ...attrs,
        "class": breadcrumbClassName
      }, [vue.createVNode("ol", null, [crumbs])]));
    };
  }
});

exports.default = ABreadcrumb;
//# sourceMappingURL=Breadcrumb.cjs.map
