function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React, { useEffect, useState } from "react";
import { useLocale } from "../../../I18n";
/** 图例每页显示条数，超过则显示分页 */ var LEGEND_PAGE_SIZE = 12;
var Legend = function Legend(param) {
    var chartData = param.chartData, backgroundColors = param.backgroundColors, hiddenDataIndicesByChart = param.hiddenDataIndicesByChart, chartIndex = param.chartIndex, onLegendItemClick = param.onLegendItemClick, total = param.total, baseClassName = param.baseClassName, hashId = param.hashId, isMobile = param.isMobile;
    var locale = useLocale();
    var hiddenDataIndices = React.useMemo(function() {
        return hiddenDataIndicesByChart[chartIndex] || new Set();
    }, [
        hiddenDataIndicesByChart,
        chartIndex
    ]);
    var totalPages = Math.max(1, Math.ceil(chartData.length / LEGEND_PAGE_SIZE));
    var _useState = _sliced_to_array(useState(0), 2), currentPage = _useState[0], setCurrentPage = _useState[1];
    useEffect(function() {
        setCurrentPage(0);
    }, [
        chartData.length,
        chartIndex
    ]);
    var startIndex = currentPage * LEGEND_PAGE_SIZE;
    var displayedItems = chartData.slice(startIndex, startIndex + LEGEND_PAGE_SIZE);
    var handlePrevPage = function handlePrevPage() {
        setCurrentPage(function(p) {
            return Math.max(0, p - 1);
        });
    };
    var handleNextPage = function handleNextPage() {
        setCurrentPage(function(p) {
            return Math.min(totalPages - 1, p + 1);
        });
    };
    return /*#__PURE__*/ React.createElement("div", {
        className: [
            "".concat(baseClassName, "-legend"),
            hashId
        ].filter(Boolean).join(' '),
        style: _object_spread({
            marginLeft: isMobile ? 0 : 12
        }, isMobile ? {
            display: 'flex',
            flexDirection: 'column',
            maxHeight: '200px',
            minHeight: 0,
            alignSelf: 'center'
        } : {})
    }, /*#__PURE__*/ React.createElement("div", {
        style: _object_spread({}, isMobile ? {
            flex: 1,
            minHeight: 0,
            overflowY: 'auto',
            overflowX: 'hidden'
        } : {})
    }, displayedItems.map(function(d, i) {
        var dataIndex = startIndex + i;
        var isHidden = hiddenDataIndices.has(dataIndex);
        var _obj;
        return /*#__PURE__*/ React.createElement("div", {
            key: dataIndex,
            className: [
                "".concat(baseClassName, "-legend-item"),
                hashId
            ].filter(Boolean).join(' '),
            style: {
                cursor: 'pointer',
                padding: isMobile ? '4px 0' : '6px 0',
                fontSize: isMobile ? 11 : 12,
                minHeight: isMobile ? '24px' : '28px',
                textDecoration: isHidden ? 'line-through' : 'none'
            },
            onClick: function onClick() {
                return onLegendItemClick(dataIndex);
            },
            onKeyDown: function onKeyDown(e) {
                if (e.key === 'Enter' || e.key === ' ') {
                    e.preventDefault();
                    onLegendItemClick(dataIndex);
                }
            },
            tabIndex: 0,
            role: "button",
            "aria-label": "".concat(isHidden ? locale['chart.legend.show'] : locale['chart.legend.hide'], " ").concat(d.label)
        }, /*#__PURE__*/ React.createElement("span", {
            className: [
                "".concat(baseClassName, "-legend-color"),
                hashId
            ].filter(Boolean).join(' '),
            style: (_obj = {}, _define_property(_obj, '--donut-legend-color', backgroundColors[dataIndex] || '#ccc'), _define_property(_obj, "width", isMobile ? 10 : 12), _define_property(_obj, "height", isMobile ? 10 : 12), _define_property(_obj, "borderRadius", 4), _define_property(_obj, "marginRight", isMobile ? 4 : 6), _obj)
        }), /*#__PURE__*/ React.createElement("span", {
            className: [
                "".concat(baseClassName, "-legend-label"),
                hashId
            ].filter(Boolean).join(' '),
            style: {
                fontSize: isMobile ? 11 : 13,
                flex: isMobile ? '0 1 auto' : 1,
                minWidth: isMobile ? '60px' : 'auto'
            }
        }, d.label), /*#__PURE__*/ React.createElement("span", {
            className: [
                "".concat(baseClassName, "-legend-value"),
                hashId
            ].filter(Boolean).join(' '),
            style: {
                fontSize: isMobile ? 11 : 13,
                fontWeight: isMobile ? 400 : 500,
                marginLeft: isMobile ? 8 : 15,
                display: 'flex',
                flexDirection: 'row',
                alignItems: 'center'
            }
        }, /*#__PURE__*/ React.createElement("span", null, d.value), /*#__PURE__*/ React.createElement("span", {
            className: [
                "".concat(baseClassName, "-legend-percent"),
                hashId
            ].filter(Boolean).join(' '),
            style: {
                fontSize: isMobile ? 10 : 12,
                marginLeft: isMobile ? 6 : 8,
                marginTop: 0
            }
        }, function() {
            var v = typeof d.value === 'number' ? d.value : Number(d.value);
            return total > 0 && Number.isFinite(v) ? (v / total * 100).toFixed(0) : '0';
        }(), "%")));
    })), totalPages > 1 && /*#__PURE__*/ React.createElement("div", {
        className: [
            "".concat(baseClassName, "-legend-pagination"),
            hashId
        ].filter(Boolean).join(' '),
        style: isMobile ? {
            flexShrink: 0
        } : undefined
    }, /*#__PURE__*/ React.createElement("button", {
        type: "button",
        "aria-label": locale['chart.legend.prevPage'],
        disabled: currentPage <= 0,
        onClick: handlePrevPage,
        style: {
            padding: '2px 6px',
            fontSize: 12,
            cursor: currentPage <= 0 ? 'not-allowed' : 'pointer',
            opacity: currentPage <= 0 ? 0.5 : 1,
            border: '1px solid rgba(0,0,0,0.15)',
            borderRadius: 4,
            background: '#fff'
        }
    }, "<"), /*#__PURE__*/ React.createElement("span", {
        style: {
            fontSize: 12,
            color: '#767E8B'
        }
    }, currentPage + 1, "/", totalPages), /*#__PURE__*/ React.createElement("button", {
        type: "button",
        "aria-label": locale['chart.legend.nextPage'],
        disabled: currentPage >= totalPages - 1,
        onClick: handleNextPage,
        style: {
            padding: '2px 6px',
            fontSize: 12,
            cursor: currentPage >= totalPages - 1 ? 'not-allowed' : 'pointer',
            opacity: currentPage >= totalPages - 1 ? 0.5 : 1,
            border: '1px solid rgba(0,0,0,0.15)',
            borderRadius: 4,
            background: '#fff'
        }
    }, ">")));
};
export default Legend;
