import { createElement as _createElement } from "react";
import { jsx as _jsx } from "react/jsx-runtime";
/*
 * Copyright 2022 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import classNames from "classnames";
import { memo, useCallback } from "react";
import { Boundary, Classes, DISPLAYNAME_PREFIX, removeNonHTMLProps } from "../../common";
import { Menu } from "../menu/menu";
import { MenuItem } from "../menu/menuItem";
import { OverflowList } from "../overflow-list/overflowList";
import { Popover } from "../popover/popover";
import { Breadcrumb } from "./breadcrumb";
const EMPTY_ITEMS = [];
/**
 * Breadcrumbs component.
 *
 * @see https://blueprintjs.com/docs/#core/components/breadcrumbs
 */
export const Breadcrumbs = memo(props => {
    const { breadcrumbRenderer, className, collapseFrom = Boundary.START, currentBreadcrumbRenderer, items = EMPTY_ITEMS, minVisibleItems = 0, overflowButtonProps, overflowListProps = {}, popoverProps, } = props;
    const renderBreadcrumb = useCallback((breadcrumbProps, isCurrent) => {
        if (isCurrent && currentBreadcrumbRenderer != null) {
            return currentBreadcrumbRenderer(breadcrumbProps);
        }
        else if (breadcrumbRenderer != null) {
            return breadcrumbRenderer(breadcrumbProps);
        }
        else {
            // allow user to override 'current' prop
            return _jsx(Breadcrumb, { current: isCurrent, ...breadcrumbProps });
        }
    }, [breadcrumbRenderer, currentBreadcrumbRenderer]);
    const renderBreadcrumbWrapper = useCallback((breadcrumbProps, index) => {
        const isCurrent = items[items.length - 1] === breadcrumbProps;
        return _jsx("li", { children: renderBreadcrumb(breadcrumbProps, isCurrent) }, index);
    }, [items, renderBreadcrumb]);
    const renderOverflowBreadcrumb = useCallback((breadcrumbProps, index) => {
        const isClickable = breadcrumbProps.href != null || breadcrumbProps.onClick != null;
        const htmlProps = removeNonHTMLProps(breadcrumbProps);
        return _createElement(MenuItem, { disabled: !isClickable, ...htmlProps, text: breadcrumbProps.text, key: index });
    }, []);
    const renderOverflow = useCallback((overflowItems) => {
        let orderedItems = overflowItems;
        if (collapseFrom === Boundary.START) {
            // If we're collapsing from the start, the menu should be read from the bottom to the
            // top, continuing with the breadcrumbs to the right. Since this means the first
            // breadcrumb in the props must be the last in the menu, we need to reverse the overflow
            // order.
            orderedItems = overflowItems.slice().reverse();
        }
        return (_jsx("li", { children: _jsx(Popover, { content: _jsx(Menu, { children: orderedItems.map(renderOverflowBreadcrumb) }), disabled: orderedItems.length === 0, placement: collapseFrom === Boundary.END ? "bottom-end" : "bottom-start", ...popoverProps, children: _jsx("span", { "aria-label": "collapsed breadcrumbs", role: "button", tabIndex: 0, ...overflowButtonProps, className: classNames(Classes.BREADCRUMBS_COLLAPSED, overflowButtonProps?.className) }) }) }));
    }, [collapseFrom, overflowButtonProps, popoverProps, renderOverflowBreadcrumb]);
    return (_jsx(OverflowList, { collapseFrom: collapseFrom, minVisibleItems: minVisibleItems, tagName: "ol", navigable: true, navigationAriaLabel: "Breadcrumb", ...overflowListProps, className: classNames(Classes.BREADCRUMBS, overflowListProps.className, className), items: items, overflowRenderer: renderOverflow, visibleItemRenderer: renderBreadcrumbWrapper }));
});
Breadcrumbs.displayName = `${DISPLAYNAME_PREFIX}.Breadcrumbs`;
//# sourceMappingURL=breadcrumbs.js.map