'use strict';

exports.__esModule = true;

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; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _helper = require('./Shared/helper.js');

var _reactRouterDom = require('react-router-dom');

var _CBreadcrumbCustom = require('./CBreadcrumbCustom');

var _CBreadcrumbCustom2 = _interopRequireDefault(_CBreadcrumbCustom);

var _CBreadcrumbItem = require('./CBreadcrumbItem');

var _CBreadcrumbItem2 = _interopRequireDefault(_CBreadcrumbItem);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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; }

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 (0, _reactRouterDom.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 ? _react2.default.createElement(
      _CBreadcrumbItem2.default,
      { active: true },
      routeName
    ) : _react2.default.createElement(
      _CBreadcrumbItem2.default,
      null,
      _react2.default.createElement(
        _reactRouterDom.Link,
        { to: match.url || '' },
        routeName
      )
    );
  }
  return null;
};

CBreadcrumbRouteItem.propTypes = process.env.NODE_ENV !== "production" ? {
  match: _propTypes2.default.shape({
    url: _propTypes2.default.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 _react2.default.createElement(_reactRouterDom.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 = (0, _classnames2.default)(className);

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

  return _react2.default.createElement(
    _CBreadcrumbCustom2.default,
    _extends({}, attributes, { listClassName: classes }),
    children
  );
};

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

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

exports.default = CBreadcrumb;
module.exports = exports['default'];