'use strict';

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

var vue = require('vue');
var index = require('../_util/vue-types/index.js');
var index$2 = require('../_util/props-util/index.js');
var warning = require('../_util/warning.js');
require('../menu/index.js');
require('../hooks/index.js');
var index$1 = require('./style/index.js');
var BreadcrumbItem = require('./BreadcrumbItem.js');
var useConfigInject = require('../config-provider/hooks/useConfigInject.js');
var Menu = require('../menu/src/Menu.js');

function _isSlot(s) {
  return typeof s === "function" || Object.prototype.toString.call(s) === "[object Object]" && !vue.isVNode(s);
}
const breadcrumbProps = () => ({
  prefixCls: String,
  routes: {
    type: Array
  },
  params: index.default.any,
  separator: index.default.any,
  itemRender: {
    type: Function
  }
});
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 Breadcrumb = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ABreadcrumb",
  inheritAttrs: false,
  props: breadcrumbProps(),
  slots: Object,
  setup(props, {
    slots,
    attrs
  }) {
    const {
      prefixCls,
      direction
    } = useConfigInject.default("breadcrumb", props);
    const [wrapSSR, hashId] = index$1.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) => {
        let _slot;
        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(Menu.default, {
            "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, vue.mergeProps(itemProps, {
          "key": path || route.breadcrumbName
        }), _isSlot(_slot = itemRender({
          route,
          params,
          routes,
          paths: tempPaths
        })) ? _slot : {
          default: () => [_slot]
        });
      });
    };
    return () => {
      var _a;
      let crumbs;
      const {
        routes,
        params = {}
      } = props;
      const children = index$2.flattenChildren(index$2.getPropsSlot(slots, props));
      const separator = (_a = index$2.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) => {
          warning.default(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", vue.mergeProps(attrs, {
        "class": breadcrumbClassName
      }), [vue.createVNode("ol", null, [crumbs])]));
    };
  }
});

exports.breadcrumbProps = breadcrumbProps;
exports.default = Breadcrumb;
//# sourceMappingURL=Breadcrumb.js.map
