"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Breadcrumbs = void 0;
const tslib_1 = require("tslib");
const react_1 = require("react");
const jsx_runtime_1 = require("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.
 */
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_2 = require("react");
const common_1 = require("../../common");
const menu_1 = require("../menu/menu");
const menuItem_1 = require("../menu/menuItem");
const overflowList_1 = require("../overflow-list/overflowList");
const popover_1 = require("../popover/popover");
const breadcrumb_1 = require("./breadcrumb");
const EMPTY_ITEMS = [];
/**
 * Breadcrumbs component.
 *
 * @see https://blueprintjs.com/docs/#core/components/breadcrumbs
 */
exports.Breadcrumbs = (0, react_2.memo)(props => {
    const { breadcrumbRenderer, className, collapseFrom = common_1.Boundary.START, currentBreadcrumbRenderer, items = EMPTY_ITEMS, minVisibleItems = 0, overflowButtonProps, overflowListProps = {}, popoverProps, } = props;
    const renderBreadcrumb = (0, react_2.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 (0, jsx_runtime_1.jsx)(breadcrumb_1.Breadcrumb, { current: isCurrent, ...breadcrumbProps });
        }
    }, [breadcrumbRenderer, currentBreadcrumbRenderer]);
    const renderBreadcrumbWrapper = (0, react_2.useCallback)((breadcrumbProps, index) => {
        const isCurrent = items[items.length - 1] === breadcrumbProps;
        return (0, jsx_runtime_1.jsx)("li", { children: renderBreadcrumb(breadcrumbProps, isCurrent) }, index);
    }, [items, renderBreadcrumb]);
    const renderOverflowBreadcrumb = (0, react_2.useCallback)((breadcrumbProps, index) => {
        const isClickable = breadcrumbProps.href != null || breadcrumbProps.onClick != null;
        const htmlProps = (0, common_1.removeNonHTMLProps)(breadcrumbProps);
        return (0, react_1.createElement)(menuItem_1.MenuItem, { disabled: !isClickable, ...htmlProps, text: breadcrumbProps.text, key: index });
    }, []);
    const renderOverflow = (0, react_2.useCallback)((overflowItems) => {
        let orderedItems = overflowItems;
        if (collapseFrom === common_1.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 ((0, jsx_runtime_1.jsx)("li", { children: (0, jsx_runtime_1.jsx)(popover_1.Popover, { content: (0, jsx_runtime_1.jsx)(menu_1.Menu, { children: orderedItems.map(renderOverflowBreadcrumb) }), disabled: orderedItems.length === 0, placement: collapseFrom === common_1.Boundary.END ? "bottom-end" : "bottom-start", ...popoverProps, children: (0, jsx_runtime_1.jsx)("span", { "aria-label": "collapsed breadcrumbs", role: "button", tabIndex: 0, ...overflowButtonProps, className: (0, classnames_1.default)(common_1.Classes.BREADCRUMBS_COLLAPSED, overflowButtonProps?.className) }) }) }));
    }, [collapseFrom, overflowButtonProps, popoverProps, renderOverflowBreadcrumb]);
    return ((0, jsx_runtime_1.jsx)(overflowList_1.OverflowList, { collapseFrom: collapseFrom, minVisibleItems: minVisibleItems, tagName: "ol", navigable: true, navigationAriaLabel: "Breadcrumb", ...overflowListProps, className: (0, classnames_1.default)(common_1.Classes.BREADCRUMBS, overflowListProps.className, className), items: items, overflowRenderer: renderOverflow, visibleItemRenderer: renderBreadcrumbWrapper }));
});
exports.Breadcrumbs.displayName = `${common_1.DISPLAYNAME_PREFIX}.Breadcrumbs`;
//# sourceMappingURL=breadcrumbs.js.map