var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

import React, { useState } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { tagPropType, mapToCssModules } from './Shared/helper.js';
import { Route, Link, matchPath } from 'react-router-dom';
import CBreadcrumbCustom from './CBreadcrumbCustom';
import CBreadcrumbItem from './CBreadcrumbItem';

var routes = void 0;

var getPaths = function getPaths(pathname) {
  var paths = ['/'];
  if (pathname === '/') return paths;
  pathname.split('/').reduce(function (prev, curr) {
    var currPath = prev + '/' + curr;
    paths.push(currPath);
    return currPath;
  });
  return paths;
};

var findRouteName = function findRouteName(url) {
  var aroute = routes.find(function (route) {
    return matchPath(url, { path: route.path, exact: route.exact });
  });
  return aroute && aroute.name ? aroute.name : null;
};

//component - CoreUI / CBreadcrumbRouteItem

var CBreadcrumbRouteItem = function CBreadcrumbRouteItem(_ref) {
  var match = _ref.match;

  var routeName = findRouteName(match.url);
  if (routeName) {
    return match.isExact ? React.createElement(
      CBreadcrumbItem,
      { active: true },
      routeName
    ) : React.createElement(
      CBreadcrumbItem,
      null,
      React.createElement(
        Link,
        { to: match.url || '' },
        routeName
      )
    );
  }
  return null;
};

CBreadcrumbRouteItem.propTypes = process.env.NODE_ENV !== "production" ? {
  match: PropTypes.shape({
    url: PropTypes.string
  })
} : {};

//component - CoreUI / CBreadcrumbRouter

var postItems = void 0;

var CBreadcrumbRouter = function CBreadcrumbRouter(args) {

  var paths = getPaths(args.location.pathname);
  var items = paths.map(function (path, i) {
    return React.createElement(Route, { key: i.toString(), path: path, component: CBreadcrumbRouteItem });
  });
  if (postItems) items.push(postItems);

  // render

  return items;
  /*
  return (
    <CBreadcrumbCustom>
      {items}
    </CBreadcrumbCustom>
  );
  */
};

//component - CoreUI / CBreadcrumb

var CBreadcrumb = function CBreadcrumb(props) {
  var Tag = props.tag,
      children = props.children,
      className = props.className,
      custom = props.custom,
      routesProps = props.routesProps,
      attributes = _objectWithoutProperties(props, ['tag', 'children', 'className', 'custom', 'routesProps']);

  if (children) postItems = children;else {
    postItems = null;
  }

  routes = props.appRoutes;

  //render

  delete attributes.appRoutes;
  var classes = classNames(className);

  if (!custom) {
    return React.createElement(
      CBreadcrumbCustom,
      _extends({}, attributes, { listClassName: classes }),
      React.createElement(Route, _extends({ path: '/:path', component: CBreadcrumbRouter }, routesProps))
    );
  }

  return React.createElement(
    CBreadcrumbCustom,
    _extends({}, attributes, { listClassName: classes }),
    children
  );
};

CBreadcrumb.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
  children: PropTypes.node,
  className: PropTypes.string,
  custom: PropTypes.bool,
  //
  innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
  appRoutes: PropTypes.any,
  routesProps: PropTypes.object
} : {};

CBreadcrumb.defaultProps = {
  tag: 'div',
  className: '',
  appRoutes: [{ path: '/', exact: true, name: 'Home', component: null }]
};

export default CBreadcrumb;