"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const Breadcrumbs = ({ items, separator = ' / ', maxItems, className, onNavigate, collapsedLabel = '...', homeIcon, renderItem, lastItemActive = true, }) => {
    // Helper to truncate breadcrumbs when there are too many
    const truncateItems = (items, maxItems) => {
        if (!maxItems || items.length <= maxItems)
            return items;
        const ellipsisIndex = Math.ceil(maxItems / 2);
        const firstHalf = items.slice(0, ellipsisIndex - 1);
        const lastHalf = items.slice(items.length - (maxItems - ellipsisIndex));
        return [
            ...firstHalf,
            { label: collapsedLabel, path: null, collapsed: true },
            ...lastHalf
        ];
    };
    // Apply truncation if maxItems is provided
    const displayItems = maxItems ? truncateItems(items, maxItems) : items;
    const isLastIndex = (index) => index === displayItems.length - 1;
    const handleClick = (e, path, index) => {
        if (isLastIndex(index) && !path)
            return;
        if (onNavigate && path) {
            e.preventDefault();
            onNavigate(path, index);
        }
    };
    // Custom item renderer for flexibility
    const defaultRenderItem = (item, index, isLast) => {
        if (item.collapsed) {
            return ((0, jsx_runtime_1.jsx)("span", { className: "bf-breadcrumbs__item bf-breadcrumbs__item--collapsed", children: item.label }, "collapsed"));
        }
        const itemContent = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [index === 0 && homeIcon && (0, jsx_runtime_1.jsx)("span", { className: "bf-breadcrumbs__home-icon", children: homeIcon }), item.icon && (0, jsx_runtime_1.jsx)("span", { className: "bf-breadcrumbs__item-icon", children: item.icon }), (0, jsx_runtime_1.jsx)("span", { className: "bf-breadcrumbs__item-text", children: item.label })] }));
        const itemClasses = [
            'bf-breadcrumbs__item',
            isLast ? 'bf-breadcrumbs__item--active' : '',
            !item.path ? 'bf-breadcrumbs__item--no-link' : '',
        ].filter(Boolean).join(' ');
        if (isLast) {
            return ((0, jsx_runtime_1.jsx)("span", { className: itemClasses, "aria-current": "page", children: itemContent }, index));
        }
        if (!item.path) {
            return ((0, jsx_runtime_1.jsx)("span", { className: itemClasses, children: itemContent }, index));
        }
        return ((0, jsx_runtime_1.jsx)("a", { href: item.path || undefined, className: itemClasses, onClick: e => handleClick(e, item.path, index), children: itemContent }, index));
    };
    const breadcrumbsClasses = [
        'bf-breadcrumbs',
        className || '',
    ].filter(Boolean).join(' ');
    return ((0, jsx_runtime_1.jsx)("nav", { className: breadcrumbsClasses, "aria-label": "Breadcrumbs", children: (0, jsx_runtime_1.jsx)("ol", { className: "bf-breadcrumbs__list", children: displayItems.map((item, index) => {
                const isLast = isLastIndex(index);
                const isActive = isLast && lastItemActive;
                return ((0, jsx_runtime_1.jsxs)("li", { className: "bf-breadcrumbs__list-item", children: [renderItem
                            ? renderItem(item, index, isActive)
                            : defaultRenderItem(item, index, isActive), !isLast && ((0, jsx_runtime_1.jsx)("span", { className: "bf-breadcrumbs__separator", "aria-hidden": "true", children: separator }))] }, `breadcrumb-${index}`));
            }) }) }));
};
exports.default = Breadcrumbs;
//# sourceMappingURL=Breadcrumbs.js.map