import _extends from "@babel/runtime/helpers/esm/extends";
import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/esm/taggedTemplateLiteralLoose";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";

function _templateObject() {
  var data = _taggedTemplateLiteralLoose(["separator"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

import React, { useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import { useClassNames, useCustom } from '../utils';
import BreadcrumbItem from './BreadcrumbItem';
var defaultProps = {
  as: 'nav',
  classPrefix: 'breadcrumb',
  separator: '/',
  maxItems: 5
};
var Breadcrumb = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var Component = props.as,
      className = props.className,
      classPrefix = props.classPrefix,
      children = props.children,
      maxItems = props.maxItems,
      separator = props.separator,
      overrideLocale = props.locale,
      onExpand = props.onExpand,
      rest = _objectWithoutPropertiesLoose(props, ["as", "className", "classPrefix", "children", "maxItems", "separator", "locale", "onExpand"]);

  var _useClassNames = useClassNames(classPrefix),
      merge = _useClassNames.merge,
      prefix = _useClassNames.prefix,
      withClassPrefix = _useClassNames.withClassPrefix;

  var _useState = useState(true),
      ellipsis = _useState[0],
      setEllipsis = _useState[1];

  var _useCustom = useCustom('Breadcrumb', overrideLocale),
      locale = _useCustom.locale;

  var renderSeparator = function renderSeparator(key) {
    return /*#__PURE__*/React.createElement("span", {
      key: key,
      "aria-hidden": true,
      className: prefix(_templateObject())
    }, separator);
  };

  var handleClickEllipsis = useCallback(function (event) {
    setEllipsis(false);
    onExpand === null || onExpand === void 0 ? void 0 : onExpand(event);
  }, [onExpand]);
  var items = [];
  var count = React.Children.count(children);

  if (count) {
    React.Children.forEach(children, function (item, index) {
      items.push(item);

      if (index < count - 1) {
        items.push(renderSeparator(index));
      }
    });
  }

  var renderCollapseItems = function renderCollapseItems() {
    if (count > maxItems && count > 2 && ellipsis) {
      return [].concat(items.slice(0, 2), [[/*#__PURE__*/React.createElement(BreadcrumbItem, {
        role: "button",
        key: "ellipsis",
        title: locale.expandText,
        "aria-label": locale.expandText,
        onClick: handleClickEllipsis
      }, /*#__PURE__*/React.createElement("span", {
        "aria-hidden": true
      }, "..."))]], items.slice(items.length - 2, items.length));
    }

    return items;
  };

  var classes = merge(className, withClassPrefix());
  return /*#__PURE__*/React.createElement(Component, _extends({}, rest, {
    ref: ref,
    className: classes
  }), renderCollapseItems());
});
Breadcrumb.Item = BreadcrumbItem;
Breadcrumb.displayName = 'Breadcrumb';
Breadcrumb.defaultProps = defaultProps;
Breadcrumb.propTypes = {
  separator: PropTypes.node,
  as: PropTypes.elementType,
  children: PropTypes.node,
  className: PropTypes.string,
  classPrefix: PropTypes.string,
  maxItems: PropTypes.number,
  onExpand: PropTypes.func
};
export default Breadcrumb;