"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TruncatedBreadcrumbs = void 0;
var _react = _interopRequireWildcard(require("react"));
var _faEllipsisH = require("@fortawesome/free-solid-svg-icons/faEllipsisH");
var _breadcrumbLinkComponent = require("./breadcrumb-link-component");
var _breadcrumbsItem = require("./breadcrumbs-item");
var _dropdownMenu = require("../dropdown-menu");
var _fontAwesomeIcon = require("../font-awesome-icon");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const TruncatedBreadcrumbs = ({
  crumbs,
  shouldRenderIconBack,
  LinkComponent = _breadcrumbLinkComponent.BreadcrumbLinkComponent,
  showItemsInBreadcrumbs
}) => {
  const firstItem = crumbs[0];
  const lastItemsCount = showItemsInBreadcrumbs - 1;
  const middleSetOfCrumbs = crumbs.slice(1, crumbs.length - lastItemsCount);
  const lastCrumbs = lastItemsCount > 0 ? crumbs.slice(-lastItemsCount) : [];
  const indexOfCrumb = crumb => crumbs.indexOf(crumb);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _react.createElement)(_breadcrumbsItem.BreadcrumbsItem, {
      ...firstItem,
      key: indexOfCrumb(firstItem),
      index: indexOfCrumb(firstItem),
      shouldRenderIconBack: shouldRenderIconBack,
      LinkComponent: LinkComponent
    }), middleSetOfCrumbs.length > 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)("li", {
      className: "breadcrumbs breadcrumb-item",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_dropdownMenu.DropdownMenu, {
        contentTestId: "truncated-breadcrumbs",
        triggerClassName: "breadcrumb-item breadcrumb-item-truncated",
        triggerHideChevron: true,
        triggerAriaLabelText: "...",
        triggerDynamicContent: () => /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
          icon: _faEllipsisH.faEllipsisH,
          "test-id": "breadcrumbs-ellipsis-icon-test-id"
        }),
        options: middleSetOfCrumbs.map(item => ({
          title: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: "drop-item",
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
              "test-id": `crumb-${indexOfCrumb(item)}`,
              children: item.label
            })
          }),
          key: indexOfCrumb(item)
        })),
        onSelect: ({
          key
        }) => {
          const selectedCrumb = crumbs[Number(key)];
          if (typeof selectedCrumb.click === 'function') {
            selectedCrumb.click({
              preventDefault: () => {},
              stopPropagation: () => {}
            });
            return;
          }
          const link = selectedCrumb.link;
          if (link) {
            window.location.href = link;
          }
        }
      })
    }), lastCrumbs.map(item => /*#__PURE__*/(0, _react.createElement)(_breadcrumbsItem.BreadcrumbsItem, {
      ...item,
      key: indexOfCrumb(item),
      index: indexOfCrumb(item),
      shouldRenderIconBack: shouldRenderIconBack,
      LinkComponent: LinkComponent
    }))]
  });
};
exports.TruncatedBreadcrumbs = TruncatedBreadcrumbs;