"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getBreadcrumbProps = exports.getBreadcrumbFromProps = exports.getBreadcrumb = exports.genBreadcrumbProps = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.string.replace.js");
var _react = _interopRequireDefault(require("react"));
var _pathToRegexp = require("path-to-regexp");
var _utils = require("../../utils");
var _pathTools = require("./pathTools");
var _jsxRuntime = require("react/jsx-runtime");
// 渲染Breadcrumb 子节点
// Render the Breadcrumb child node
const defaultItemRender = ({
  breadcrumbName,
  path
}) => /*#__PURE__*/(0, _jsxRuntime.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;
};
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 ? ,不然会重复
    !(0, _utils.isUrl)(path) && (0, _pathToRegexp.pathToRegexp)(path.replace('?', '')).test(url));
    if (targetPath) {
      breadcrumbItem = breadcrumbMap.get(targetPath);
    }
  }
  return breadcrumbItem || {
    path: ''
  };
};
exports.getBreadcrumb = getBreadcrumb;
const getBreadcrumbFromProps = props => {
  const {
    location,
    breadcrumbMap
  } = props;
  return {
    location,
    breadcrumbMap
  };
};
exports.getBreadcrumbFromProps = getBreadcrumbFromProps;
const conversionFromLocation = (routerLocation = {
  pathname: '/'
}, breadcrumbMap, props) => {
  if (!routerLocation) {
    return [];
  }
  // Convertor the url to an array
  const pathSnippets = (0, _pathTools.urlToList)(routerLocation.pathname);
  // Loop data mosaic routing
  const extraBreadcrumbItems = pathSnippets.map(url => {
    // For application that has configured router base
    // @ts-ignore
    const {
      routerBase = '/'
    } = (0, _utils.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 */
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
exports.genBreadcrumbProps = genBreadcrumbProps;
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
  };
};
exports.getBreadcrumbProps = getBreadcrumbProps;