"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LegendList = void 0;
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const constants_1 = require("./constants");
const label_1 = require("./label");
const legend_color_picker_1 = require("./legend_color_picker");
const use_action_focus_management_1 = require("./use_action_focus_management");
const utils_1 = require("./utils");
const legend_1 = require("../../common/legend");
const common_1 = require("../../utils/common");
const LegendList = (props) => {
    const { extraValues, item, legendValues, totalItems, action: Action, legendActionOnHover, positionConfig, labelOptions, isMostlyRTL, flatLegend, onClick, toggleDeselectSeriesAction, hiddenItems, onLegendItemMouseOver, onLegendItemMouseOut, isListLayout, maxFormattedValueWidth, } = props;
    const { color, isSeriesHidden, isItemHidden, seriesIdentifiers, label, depth, path, isToggleable } = item;
    const { isActive, actionRef, handlePointerDown, handleKeyDown } = (0, use_action_focus_management_1.useActionFocusManagement)();
    const itemClassNames = (0, classnames_1.default)('echLegendItem', {
        'echLegendItem--hidden': isSeriesHidden,
        'echLegendItem--vertical': positionConfig.direction === common_1.LayoutDirection.Vertical,
        'echLegendItem--isActive': isActive,
        'echLegendItem--actionOnHover': legendActionOnHover,
    });
    const preparedLegendValues = (0, utils_1.prepareLegendValues)(item, legendValues, totalItems, extraValues);
    const legendValueItems = preparedLegendValues.filter(common_1.isDefined);
    const style = {
        ...(flatLegend
            ? {}
            : {
                [isMostlyRTL ? 'marginRight' : 'marginLeft']: constants_1.LEGEND_HIERARCHY_MARGIN * (depth ?? 0),
            }),
    };
    const onLabelToggle = (0, react_1.useCallback)((legendItemIds) => (negate) => {
        if (totalItems <= 1 || (!isToggleable && !onClick)) {
            return;
        }
        if (onClick) {
            onClick(legendItemIds);
        }
        if (isToggleable) {
            toggleDeselectSeriesAction({ legendItemIds, metaKey: negate });
        }
    }, [onClick, toggleDeselectSeriesAction, isToggleable, totalItems]);
    const { renderItemColor, renderColorPickerPopup } = (0, legend_color_picker_1.useLegendColorPicker)(props);
    if (isItemHidden)
        return null;
    const valueElements = [];
    const shouldShowValues = !isSeriesHidden || Boolean(isListLayout);
    if (shouldShowValues) {
        const valueData = isListLayout
            ?
                legendValues.map((type, index) => ({ type, legendValueItem: preparedLegendValues[index], index }))
            : legendValueItems.map((legendValueItem, index) => ({
                type: legendValueItem.type,
                legendValueItem,
                index,
            }));
        for (const { type, legendValueItem, index } of valueData) {
            const showTitle = isListLayout;
            const title = showTitle ? legend_1.legendValueTitlesMap[type] : '';
            const isCurrentAndLastValue = type === legend_1.LegendValue.CurrentAndLastValue;
            const hasValue = Boolean(legendValueItem?.label);
            const showPlaceholder = Boolean(isListLayout && isCurrentAndLastValue && !hasValue);
            const displayedLabel = showPlaceholder ? '—' : legendValueItem?.label ?? '';
            if (displayedLabel === '')
                continue;
            valueElements.push(react_1.default.createElement("div", { key: isListLayout ? `${type}-${index}` : displayedLabel, className: "echLegendItem__legendValue", style: {
                    textAlign: isListLayout ? 'left' : undefined,
                    minWidth: isListLayout && maxFormattedValueWidth && isCurrentAndLastValue
                        ? `${maxFormattedValueWidth}px`
                        : undefined,
                } }, showTitle ? (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement("strong", null,
                    title.toUpperCase(),
                    ":"),
                " ",
                displayedLabel)) : (displayedLabel)));
        }
    }
    const actionElement = Action ? (react_1.default.createElement("div", { className: "echLegendItem__action", ref: actionRef, onPointerDownCapture: handlePointerDown, onKeyDown: handleKeyDown },
        react_1.default.createElement(Action, { series: seriesIdentifiers, color: color, label: label }))) : null;
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("li", { className: itemClassNames, onMouseEnter: () => onLegendItemMouseOver(seriesIdentifiers, path), onMouseLeave: onLegendItemMouseOut, style: style, dir: isMostlyRTL ? 'rtl' : 'ltr', "data-ech-series-name": label },
            react_1.default.createElement("div", { className: "background" }),
            isListLayout ? (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement("span", { className: "echLegendItem__colorLabel" },
                    react_1.default.createElement("div", { className: "echLegend__colorWrapper" }, renderItemColor()),
                    react_1.default.createElement(label_1.Label, { label: label, options: labelOptions, isToggleable: totalItems > 1 && item.isToggleable, onToggle: onLabelToggle(seriesIdentifiers), isSeriesHidden: isSeriesHidden, totalSeriesCount: totalItems, hiddenSeriesCount: hiddenItems, truncationMode: "px" }),
                    actionElement && valueElements.length === 0 && actionElement),
                actionElement && valueElements.length > 0 ? (react_1.default.createElement(react_1.default.Fragment, null,
                    valueElements.slice(0, -1),
                    react_1.default.createElement("span", { className: "echLegendItem__actionGroup" },
                        valueElements.at(-1),
                        actionElement))) : !actionElement ? (valueElements) : null)) : (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement("div", { className: "echLegend__colorWrapper" }, renderItemColor()),
                react_1.default.createElement(label_1.Label, { label: label, options: labelOptions, isToggleable: totalItems > 1 && item.isToggleable, onToggle: onLabelToggle(seriesIdentifiers), isSeriesHidden: isSeriesHidden, totalSeriesCount: totalItems, hiddenSeriesCount: hiddenItems, truncationMode: "line" }),
                valueElements,
                actionElement))),
        renderColorPickerPopup()));
};
exports.LegendList = LegendList;
//# sourceMappingURL=legend_list.js.map