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 _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
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 _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
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 _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
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 from "react";
import { MOBILE_MAX_CHART_SIZE } from "./constants";
export var useMobile = function() {
    var _React_useState = _sliced_to_array(React.useState(false), 2), isMobile = _React_useState[0], setIsMobile = _React_useState[1];
    var _React_useState1 = _sliced_to_array(React.useState(typeof window !== 'undefined' ? window.innerWidth : 0), 2), windowWidth = _React_useState1[0], setWindowWidth = _React_useState1[1];
    React.useEffect(function() {
        var handleResize = function() {
            var width = window.innerWidth;
            setWindowWidth(width);
            setIsMobile(width <= 768);
        };
        handleResize();
        window.addEventListener('resize', handleResize);
        return function() {
            return window.removeEventListener('resize', handleResize);
        };
    }, []);
    return {
        isMobile: isMobile,
        windowWidth: windowWidth
    };
};
export var useResponsiveDimensions = function(isMobile, windowWidth, width, height) {
    if (isMobile) {
        var mobileWidth = Math.min(windowWidth - 40, Number(width), MOBILE_MAX_CHART_SIZE);
        var mobileHeight = Math.min(windowWidth - 40, Number(height), MOBILE_MAX_CHART_SIZE);
        return {
            width: mobileWidth,
            height: mobileHeight,
            chartWidth: mobileWidth,
            chartHeight: mobileHeight
        };
    }
    return {
        width: width,
        height: height,
        chartWidth: width,
        chartHeight: height
    };
};
export var useFilterLabels = function(data) {
    var validFilterLabels = React.useMemo(function() {
        return data.map(function(item) {
            return item.filterLabel;
        }).filter(function(filterLabel) {
            return filterLabel !== undefined;
        });
    }, [
        data
    ]);
    var filterLabels = React.useMemo(function() {
        return validFilterLabels.length > 0 ? _to_consumable_array(new Set(validFilterLabels)) : undefined;
    }, [
        validFilterLabels
    ]);
    var _React_useState = _sliced_to_array(React.useState(filterLabels && filterLabels.length > 0 ? filterLabels[0] : undefined), 2), selectedFilterLabel = _React_useState[0], setSelectedFilterLabel = _React_useState[1];
    // 当 data 变化导致 filterLabels 变化时，自动纠正选中项，避免残留无效选择
    React.useEffect(function() {
        if (!filterLabels || filterLabels.length === 0) {
            if (selectedFilterLabel !== undefined) {
                setSelectedFilterLabel(undefined);
            }
            return;
        }
        if (!selectedFilterLabel || !filterLabels.includes(selectedFilterLabel)) {
            setSelectedFilterLabel(filterLabels[0]);
        }
    }, [
        filterLabels
    ]);
    var filteredDataByFilterLabel = React.useMemo(function() {
        return filterLabels === null || filterLabels === void 0 ? void 0 : filterLabels.map(function(item) {
            return {
                key: item,
                label: item
            };
        });
    }, [
        filterLabels
    ]);
    return {
        filterLabels: filterLabels,
        filteredDataByFilterLabel: filteredDataByFilterLabel,
        selectedFilterLabel: selectedFilterLabel,
        setSelectedFilterLabel: setSelectedFilterLabel
    };
};
export var useAutoCategory = function(data, enableAutoCategory, externalSelectedFilter) {
    var autoCategoryData = React.useMemo(function() {
        if (!enableAutoCategory || !data) {
            return null;
        }
        var allData = data;
        var categories = _to_consumable_array(new Set(allData.map(function(item) {
            return item.category;
        }).filter(Boolean)));
        if (categories.length <= 1) {
            return null;
        }
        return {
            categories: categories,
            allData: allData
        };
    }, [
        data,
        enableAutoCategory
    ]);
    var _React_useState = _sliced_to_array(React.useState(''), 2), internalSelectedCategory = _React_useState[0], setInternalSelectedCategory = _React_useState[1];
    // 初始化：当存在多类目时，选中第一个有效类目
    React.useEffect(function() {
        if (autoCategoryData && !internalSelectedCategory) {
            setInternalSelectedCategory(autoCategoryData.categories.find(Boolean) || '');
        }
    }, [
        autoCategoryData,
        internalSelectedCategory
    ]);
    // 当自动分类关闭或类目不足（autoCategoryData 为空）时，清理内部选中项，避免残留影响
    React.useEffect(function() {
        if (!enableAutoCategory || !autoCategoryData) {
            if (internalSelectedCategory) {
                setInternalSelectedCategory('');
            }
        }
    }, [
        enableAutoCategory,
        autoCategoryData
    ]);
    var selectedCategory = externalSelectedFilter || internalSelectedCategory;
    return {
        autoCategoryData: autoCategoryData,
        internalSelectedCategory: internalSelectedCategory,
        setInternalSelectedCategory: setInternalSelectedCategory,
        selectedCategory: selectedCategory
    };
};
