import React__default from 'react';
import { getId } from '../../utils/index.js';
import { BreadcrumbWrapper, BreadcrumbItem, BreadcrumbChevron, BreadcrumbCurrentPage, BreadcrumbBackChevron } from './styles.js';

function Breadcrumb(props) {
    const appendTestId = getId(props['data-testid']);
    if (props.breadcrumbs.length === 0) {
        throw new Error('Breadcrumbs need at least one item to render.');
    }
    function showLastChevron(index) {
        if (Boolean(props.currentPage)) {
            return true;
        }
        return index + 1 < props.breadcrumbs.length;
    }
    function getLastBreadcrumb() {
        const { length } = props.breadcrumbs;
        return props.breadcrumbs[length - 1];
    }
    switch (props.variant) {
        case 'back':
            const breadcrumb = getLastBreadcrumb();
            function onClick(e) {
                e.stopPropagation();
                if (breadcrumb && breadcrumb.onClick) {
                    breadcrumb.onClick(e);
                }
            }
            return (React__default.createElement(BreadcrumbWrapper, { onClick: onClick, scrim: props.scrim, variant: "back", "data-testid": props['data-testid'], "data-trackid": props['data-trackid'], "aria-label": "breadcrumbs" },
                React__default.createElement(BreadcrumbBackChevron, { role: "img", "data-testid": appendTestId('back-chevron') }),
                React__default.createElement(BreadcrumbItem, { onClick: onClick, "data-testid": appendTestId('back-button') }, breadcrumb.label)));
        case 'default':
        default:
            return (React__default.createElement(BreadcrumbWrapper, { scrim: props.scrim, variant: "default", "data-testid": props['data-testid'], "data-trackid": props['data-trackid'], "aria-label": "breadcrumbs" },
                props.breadcrumbs.map((breadcrumb, i) => (React__default.createElement(React__default.Fragment, { key: `bc-${i}-${breadcrumb.label}` },
                    React__default.createElement(BreadcrumbItem, { target: breadcrumb.href && props.target ? props.target : undefined, as: breadcrumb.href ? 'a' : 'button', href: breadcrumb.href, onClick: breadcrumb.href ? undefined : breadcrumb.onClick, "data-testid": appendTestId(`item-${i}`) }, breadcrumb.label),
                    showLastChevron(i) && (React__default.createElement(BreadcrumbChevron, { role: "img", "data-testid": appendTestId(`chevron-${i}`) }))))),
                props.currentPage && (React__default.createElement(BreadcrumbCurrentPage, { target: props.currentPageLink && props.target ? props.target : undefined, "aria-current": "page", "data-testid": appendTestId('current-page'), as: props.currentPageLink ? 'a' : undefined, href: props.currentPageLink }, props.currentPage))));
    }
}

export { Breadcrumb };
