"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BreadcrumbSeparator = exports.BreadcrumbItem = exports.BreadcrumbLink = exports.Breadcrumb = void 0;

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _react = _interopRequireWildcard(require("react"));

var _Box = require("../Box");

var _Link = require("../Link");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __jsx = _react["default"].createElement;
var BreadcrumbSeparator = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  return __jsx(_Box.Box, (0, _extends2["default"])({
    ref: ref,
    role: "presentation",
    as: "span",
    mx: "spacing-xs"
  }, props), props.children);
});
exports.BreadcrumbSeparator = BreadcrumbSeparator;
var Span = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  return __jsx(_Box.Box, (0, _extends2["default"])({
    ref: ref,
    as: "span"
  }, props));
});
var BreadcrumbLink = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var isCurrentPage = _ref.isCurrentPage,
      props = (0, _objectWithoutProperties2["default"])(_ref, ["isCurrentPage"]);
  var Comp = isCurrentPage ? Span : _Link.Link;
  return __jsx(Comp, (0, _extends2["default"])({
    ref: ref,
    "aria-current": isCurrentPage ? 'page' : null
  }, props));
});
exports.BreadcrumbLink = BreadcrumbLink;

var BreadcrumbItem = function BreadcrumbItem(_ref2) {
  var isCurrentPage = _ref2.isCurrentPage,
      separator = _ref2.separator,
      isLastChild = _ref2.isLastChild,
      addSeparator = _ref2.addSeparator,
      spacing = _ref2.spacing,
      children = _ref2.children,
      rest = (0, _objectWithoutProperties2["default"])(_ref2, ["isCurrentPage", "separator", "isLastChild", "addSeparator", "spacing", "children"]);

  var clones = _react.Children.map(children, function (child) {
    if (! /*#__PURE__*/(0, _react.isValidElement)(child)) {
      return null;
    }

    if (child.type === BreadcrumbLink) {
      return /*#__PURE__*/(0, _react.cloneElement)(child, {
        isCurrentPage: isCurrentPage
      });
    }

    if (child.type === BreadcrumbSeparator) {
      return /*#__PURE__*/(0, _react.cloneElement)(child, {
        children: child.props.children || separator
      });
    }

    return child;
  });

  return __jsx(_Box.Box, (0, _extends2["default"])({
    display: "inline-flex",
    alignItems: "center",
    as: "li"
  }, rest), clones, !isLastChild && addSeparator && __jsx(BreadcrumbSeparator, null, separator));
};

exports.BreadcrumbItem = BreadcrumbItem;

var Breadcrumb = function Breadcrumb(_ref3) {
  var children = _ref3.children,
      _ref3$spacing = _ref3.spacing,
      spacing = _ref3$spacing === void 0 ? 2 : _ref3$spacing,
      _ref3$addSeparator = _ref3.addSeparator,
      addSeparator = _ref3$addSeparator === void 0 ? true : _ref3$addSeparator,
      _ref3$separator = _ref3.separator,
      separator = _ref3$separator === void 0 ? '/' : _ref3$separator,
      rest = (0, _objectWithoutProperties2["default"])(_ref3, ["children", "spacing", "addSeparator", "separator"]);

  var clones = _react.Children.map(children, function (child, index) {
    if (! /*#__PURE__*/(0, _react.isValidElement)(child)) {
      return null;
    }

    return /*#__PURE__*/(0, _react.cloneElement)(child, {
      addSeparator: addSeparator,
      separator: separator,
      spacing: spacing,
      isLastChild: _react.Children.count(children) === index + 1
    });
  });

  return __jsx(_Box.Box, (0, _extends2["default"])({
    as: "nav",
    "aria-label": "breadcrumb"
  }, rest), __jsx(_Box.Box, {
    as: "ol"
  }, clones));
};

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