import "core-js/modules/es.array.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.string.replace.js";
import React from 'react';
import { pathToRegexp } from 'path-to-regexp';
import { isBrowser, isUrl } from '../../utils';
import { urlToList } from './pathTools';
import { jsx as _jsx } from "react/jsx-runtime";
// 渲染Breadcrumb 子节点
// Render the Breadcrumb child node
const defaultItemRender = ({
  breadcrumbName,
  path
}) => /*#__PURE__*/_jsx("a", {
  href: path,
  children: breadcrumbName
});
const renderItemLocal = (item, props) => {
  const {
    formatMessage,
    menu = {
      locale: false
    }
  } = props;
  if (item.locale && formatMessage && menu.locale !== false) {
    return formatMessage({
      id: item.locale,
      defaultMessage: item.name
    });
  }
  return item.name;
};
export const getBreadcrumb = (breadcrumbMap, url) => {
  if (!breadcrumbMap) {
    return {
      path: ''
    };
  }
  let breadcrumbItem = breadcrumbMap.get(url);
  if (!breadcrumbItem) {
    // Find the first matching path in the order defined by route config
    // 按照 route config 定义的顺序找到第一个匹配的路径
    const targetPath = Array.from(breadcrumbMap.keys()).find(path =>
    // remove ? ,不然会重复
    !isUrl(path) && pathToRegexp(path.replace('?', '')).test(url));
    if (targetPath) {
      breadcrumbItem = breadcrumbMap.get(targetPath);
    }
  }
  return breadcrumbItem || {
    path: ''
  };
};
export const getBreadcrumbFromProps = props => {
  const {
    location,
    breadcrumbMap
  } = props;
  return {
    location,
    breadcrumbMap
  };
};
const conversionFromLocation = (routerLocation = {
  pathname: '/'
}, breadcrumbMap, props) => {
  if (!routerLocation) {
    return [];
  }
  // Convertor the url to an array
  const pathSnippets = urlToList(routerLocation.pathname);
  // Loop data mosaic routing
  const extraBreadcrumbItems = pathSnippets.map(url => {
    // For application that has configured router base
    // @ts-ignore
    const {
      routerBase = '/'
    } = isBrowser() ? window : {};
    const realPath = routerBase === '/' ? url : `${routerBase}${url}`;
    const currentBreadcrumb = getBreadcrumb(breadcrumbMap, url);
    if (currentBreadcrumb.inherited) {
      return {
        path: '',
        breadcrumbName: ''
      };
    }
    const name = renderItemLocal(currentBreadcrumb, props);
    const {
      hideInBreadcrumb
    } = currentBreadcrumb;
    return name && !hideInBreadcrumb ? {
      path: realPath,
      breadcrumbName: name,
      component: currentBreadcrumb.component
    } : {
      path: '',
      breadcrumbName: ''
    };
  }).filter(item => item && item.path);
  return extraBreadcrumbItems;
};
/** 将参数转化为面包屑 Convert parameters into breadcrumbs */
export const genBreadcrumbProps = props => {
  const {
    location,
    breadcrumbMap
  } = getBreadcrumbFromProps(props);

  // 根据 location 生成 面包屑
  // Generate breadcrumbs based on location
  if (location && location.pathname && breadcrumbMap) {
    return conversionFromLocation(location, breadcrumbMap, props);
  }
  return [];
};

// use breadcrumbRender to change routes
export const getBreadcrumbProps = props => {
  const {
    breadcrumbRender,
    itemRender: propsItemRender
  } = props;
  const routesArray = genBreadcrumbProps(props);
  const itemRender = propsItemRender || defaultItemRender;
  let routes = routesArray;
  // if routes.length =1, don't show it
  if (breadcrumbRender) {
    routes = breadcrumbRender(routes) || [];
  }
  if (routes && routes.length < 2 || breadcrumbRender === false) {
    routes = undefined;
  }
  return {
    routes,
    itemRender
  };
};