"use strict";
exports.__esModule = true;
exports.Breadcrumbs = exports.cnBreadcrumbs = exports.breadcrumbPropSizeDefault = exports.breadcrumbPropSize = void 0;
var tslib_1 = require("tslib");
// @ts-nocheck
require("./Breadcrumbs.css");
var react_1 = (0, tslib_1.__importStar)(require("react"));
var IconSlash_1 = require("../../icons/IconSlash/IconSlash");
var bem_1 = require("../../utils/bem");
var getSizeByMap_1 = require("../../utils/getSizeByMap");
var Button_1 = require("../Button/Button");
exports.breadcrumbPropSize = ['m', 'xs', 's', 'l'];
exports.breadcrumbPropSizeDefault = exports.breadcrumbPropSize[0];
exports.cnBreadcrumbs = (0, bem_1.cn)('Breadcrumbs');
var sizeMap = {
    xs: 's',
    s: 's',
    m: 's',
    l: 'm'
};
// eslint-disable-next-line @typescript-eslint/no-redeclare
exports.Breadcrumbs = react_1["default"].forwardRef(function (props, ref) {
    var pages = props.pages, getLabel = props.getLabel, getIsActive = props.getIsActive, getLink = props.getLink, getIcon = props.getIcon, _a = props.size, size = _a === void 0 ? exports.breadcrumbPropSizeDefault : _a, _b = props.maxCount, maxCount = _b === void 0 ? 2 : _b, onClick = props.onClick, _c = props.onlyIconRoot, onlyIconRoot = _c === void 0 ? false : _c, className = props.className, history = props.history, inverted = props.inverted, restProps = (0, tslib_1.__rest)(props, ["pages", "getLabel", "getIsActive", "getLink", "getIcon", "size", "maxCount", "onClick", "onlyIconRoot", "className", "history", "inverted"]);
    var iconSize = (0, getSizeByMap_1.getSizeByMap)(sizeMap, size);
    var _d = (0, react_1.useMemo)(function () {
        var rest = pages.slice();
        var head = rest.splice(0, 1);
        var tail = rest.splice(2 - maxCount);
        return {
            head: head,
            tail: tail,
            rest: rest,
            totalLength: pages.length
        };
    }, [pages, maxCount]), head = _d.head, tail = _d.tail, rest = _d.rest, totalLength = _d.totalLength;
    var delimiter = (0, react_1.useMemo)(function () { return (react_1["default"].createElement(IconSlash_1.IconSlash, { size: iconSize, className: (0, exports.cnBreadcrumbs)('Delimiter', {
            size: iconSize
        }) })); }, [iconSize]);
    var clickHandler = function (page, e) {
        if (!onClick) {
            e.preventDefault();
            history.push(page.url);
        }
        else {
            onClick(page, e);
        }
    };
    var renderPages = function (pages, isFirst) {
        if (isFirst === void 0) { isFirst = false; }
        return pages.map(function (page) {
            var Icon = getIcon === null || getIcon === void 0 ? void 0 : getIcon(page);
            var label = getLabel(page);
            var link = getLink(page);
            var isActive = getIsActive === null || getIsActive === void 0 ? void 0 : getIsActive(page);
            return (react_1["default"].createElement("li", { key: "".concat(label, ":").concat(link), className: (0, exports.cnBreadcrumbs)('Item', {
                    isFirst: isFirst,
                    hideFirst: isFirst && totalLength >= 3
                }) },
                !isFirst && delimiter,
                react_1["default"].createElement("a", { className: (0, exports.cnBreadcrumbs)('Link', { active: isActive, inverted: inverted }), onClick: function (e) { return clickHandler(page, e); }, href: link },
                    Icon &&
                        (onlyIconRoot && isFirst ? (react_1["default"].createElement(Button_1.Button, { view: "clear", onlyIcon: true, iconLeft: Icon, size: iconSize })) : (react_1["default"].createElement(Icon, { className: (0, exports.cnBreadcrumbs)('Icon'), size: iconSize }))),
                    (!isFirst || !onlyIconRoot) && (react_1["default"].createElement("span", { className: (0, exports.cnBreadcrumbs)('Label') },
                        react_1["default"].createElement("span", null, label))))));
        });
    };
    return (react_1["default"].createElement("ul", (0, tslib_1.__assign)({ className: (0, exports.cnBreadcrumbs)({ size: size, inverted: inverted }, [className]), ref: ref }, restProps),
        renderPages(head, true),
        maxCount && rest.length > 1 ? (react_1["default"].createElement("li", { className: (0, exports.cnBreadcrumbs)('Item') },
            delimiter,
            react_1["default"].createElement("span", { className: (0, exports.cnBreadcrumbs)('More') }, "..."))) : (renderPages(rest)),
        renderPages(tail)));
});
